Motor Hookup

 

Once I have Bluetooth communication, it’s time to make this thing move.  I’ve set it up with an Ardumoto motor shield which can allow for a maximum of 2A of power.

Using this code that I slapped together with a few tutorials, I can control the wheels via Bluetooth commands.  Now I noticed that the wheels wouldn’t turn when I had set the speed to 255/2.  This was due to the USB plug, having a maximum output of 500mA x 5V = 2.5W.  That is not enough power, so to increase the amount of available power, the barrel jack should be used to supplement with 4x AA alkaline batteries.

  • Next steps:  order new parts for more power!

#include <SoftwareSerial.h>
SoftwareSerial BT(4, 5);
// creates a “virtual” serial port/UART
// connect BT module TX to D4
// connect BT module RX to D5
// connect BT Vcc to 5V, GND to GND

// Clockwise and counter-clockwise definitions.
// Depending on how you wired your motors, you may need to swap.
#define CW 0
#define CCW 1

// Motor definitions to make life easier:
#define MOTOR_A 0
#define MOTOR_B 1

// Pin Assignments //
// Don’t change these! These pins are statically defined by shield layout
const byte PWMA = 3; // PWM control (speed) for motor A
const byte PWMB = 11; // PWM control (speed) for motor B
const byte DIRA = 12; // Direction control for motor A
const byte DIRB = 13; // Direction control for motor B

void setup()
{
setupArdumoto(); // Set all pins as outputs

// set digital pin to control as an output
pinMode(13, OUTPUT);
// set the data rate for the SoftwareSerial port
BT.begin(9600);
// Send test message to other device
BT.println(“Hello from Arduino”);
}

char a; // stores incoming character from other device

void loop()
{

if (BT.available())
// if text arrived in from BT serial…
{
a=(BT.read());
if (a==’1′)
{
digitalWrite(13, HIGH);
BT.println(“LED on”);
}
if (a==’2′)
{
digitalWrite(13, LOW);
BT.println(“LED off”);
}
if (a==’?’)
{
BT.println(“Send ‘1’ to turn LED on”);
BT.println(“Send ‘2’ to turn LED on”);
}
// you can add more “if” statements with other characters to add more commands

if (a==’q’)
{
// Drive motor A (and only motor A) at various speeds, then stop.
driveArdumoto(MOTOR_A, CCW, 255); // Set motor A to CCW at max
delay(3000); // Motor A will spin as set for 1 second
driveArdumoto(MOTOR_A, CW, 255); // Set motor A to CW at half
delay(3000); // Motor A will keep trucking for 1 second
stopArdumoto(MOTOR_A); // STOP motor A
}
if (a==’w’)
{
// Drive motor B (and only motor B) at various speeds, then stop.
driveArdumoto(MOTOR_B, CCW, 255); // Set motor B to CCW at max
delay(3000); // Motor B will spin as set for 1 second
driveArdumoto(MOTOR_B, CW, 255); // Set motor B to CW at half
delay(3000); // Motor B will keep trucking for 1 second
stopArdumoto(MOTOR_B); // STOP motor B
}
if (a==’e’)
{
// Now spin both!
driveArdumoto(MOTOR_A, CW, 255); // Motor A at max speed.
driveArdumoto(MOTOR_B, CW, 255); // Motor B at max speed.
delay(3000); // Drive forward for a second
stopArdumoto(MOTOR_A); // STOP motor A
stopArdumoto(MOTOR_B); // STOP motor B
}
if (a==’r’)
{
// Now go backwards at half that speed:
driveArdumoto(MOTOR_A, CCW, 255); // Motor A at max speed.
driveArdumoto(MOTOR_B, CCW, 255); // Motor B at max speed.
delay(3000); // Drive forward for a second
stopArdumoto(MOTOR_A); // STOP motor A
stopArdumoto(MOTOR_B); // STOP motor B
}

}
}

// driveArdumoto drives ‘motor’ in ‘dir’ direction at ‘spd’ speed
void driveArdumoto(byte motor, byte dir, byte spd)
{
if (motor == MOTOR_A)
{
digitalWrite(DIRA, dir);
analogWrite(PWMA, spd);
}
else if (motor == MOTOR_B)
{
digitalWrite(DIRB, dir);
analogWrite(PWMB, spd);
}
}

// stopArdumoto makes a motor stop
void stopArdumoto(byte motor)
{
driveArdumoto(motor, 0, 0);
}

// setupArdumoto initialize all pins
void setupArdumoto()
{
// All pins should be setup as outputs:
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(DIRA, OUTPUT);
pinMode(DIRB, OUTPUT);

// Initialize all pins as low:
digitalWrite(PWMA, LOW);
digitalWrite(PWMB, LOW);
digitalWrite(DIRA, LOW);
digitalWrite(DIRB, LOW);
}