I'm trying to follow Nick Gammon's tutorial
http://www.gammon.com.au/forum/?id=11411 to get motors to turn and to light a LED. Here is my code:
const byte PWMA = 5;
const byte AIN2 = 2;
const byte AIN1 = 3;
const byte STBY = 4;
const byte BIN1 = 7;
const byte BIN2 = A4;
const byte PWMB = 6;
const byte RedLED = A5;
// Time periods of blinks in milliseconds (1000 to a second).
const unsigned long Motorsinterval = 5000;
const unsigned long RedLEDinterval = 5000;
// Variable holding the timer value so far. One for each "Timer"
unsigned long Motorstimer;
unsigned long RedLEDtimer;
void setup()
{
Motorstimer = millis ();
RedLEDtimer = millis ();
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode (STBY, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(RedLED, OUTPUT);
}
void toggleMotors ()
{
if (digitalRead (STBY) == LOW)
{
digitalWrite (STBY, HIGH);
digitalWrite(AIN1, LOW); //FORWARD ****
digitalWrite(AIN2, HIGH); //REVERSE IF HIGH
analogWrite(PWMA,255);
digitalWrite(BIN1, LOW); //FORWARD
digitalWrite(BIN2,HIGH); //REVERSE IF HIGH
analogWrite(PWMB,255);
}
else
{
digitalWrite (STBY, LOW);
}
// remember when we toggled it
Motorstimer = millis ();
} // end of toggleGreenRedLED
void toggleRedLED ()
{
if (digitalRead (RedLED) == LOW)
{
digitalWrite (RedLED, HIGH);
}
else
{
digitalWrite (RedLED, LOW);
}
// remember when we toggled it
RedLEDtimer = millis ();
} // end of toggleRedRedLED
void loop()
{
if ( (millis () - Motorstimer) >= Motorsinterval)
{
toggleMotors ();
}
if ( (millis () - RedLEDtimer) >= RedLEDinterval)
{
toggleRedLED ();
}
}
The red LED turns on and off as expected, but the motors never fully turn on. They only twitch about 1/8 of a turn when the red led turns on and again when it turns off.
If I just use a test code to turn the motors on without any sort of timer or delays, they turn as expected.
I'm using a TB6612FNG motor driver.
http://www.pololu.com/catalog/product/713standalone ATmega 328 on soldered perf board:
http://arduino.cc/en/Main/StandaloneMotord:
http://www.pololu.com/catalog/product/77Power:
I'm using 6 AA rechargeable NiMH batteries for the motors (input to the motor driver) and a 9v battery to a 7805 regulator decoupled with .1uF and 22uF on the input and 100uF and 10uF on the output.
Any suggestions would be greatly appreciated.