- Fri Feb 27, 2015 7:42 pm
#180097
Salivations!
With the code below I'm trying to make my robot wheels spin individually clockwise, then counter clockwise.
The first wheel spins clockwise only. Second wheel spins as intended. See https://www.youtube.com/watch?v=yM2lqy_vczU
Most likely doing something dumb, but can't find the problem and thought I'd post up just to make sure I don't have a bad shield. Appreciate any help!
With the code below I'm trying to make my robot wheels spin individually clockwise, then counter clockwise.
The first wheel spins clockwise only. Second wheel spins as intended. See https://www.youtube.com/watch?v=yM2lqy_vczU
Most likely doing something dumb, but can't find the problem and thought I'd post up just to make sure I don't have a bad shield. Appreciate any help!
Code: Select all
#define CW 1
#define CCW 0
#define MOTOR_A 0
#define MOTOR_B 1
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
// Delay execution to give time to get out of the way
delay(1000);
}
void loop() {
driveArdumoto(MOTOR_A, CW, 100);
delay(3000);
stopArdumoto(MOTOR_A);
delay(3000);
driveArdumoto(MOTOR_A, CCW, 100);
delay(3000);
stopArdumoto(MOTOR_A);
delay(3000);
driveArdumoto(MOTOR_B, CW, 100);
delay(3000);
stopArdumoto(MOTOR_B);
delay(3000);
driveArdumoto(MOTOR_B, CCW, 100);
delay(3000);
stopArdumoto(MOTOR_B);
delay(3000);
}
// 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);
}