Hello,
I'm using an Arduino (I'm a noob to Arduino coding - and excited about my project!) to power two motors but the motors never stay on for as long as I'm expecting them to stay on - in fact it's very inconsistent and definitely never as long as I'm programming them to stay on (programmed for 20 seconds, stays on for half a second or less -- sometimes programmed for 3 seconds and it stays on for 1 second). I am either not setting some sort of flag in the code or my circuitry is causing some problem. My code is pasted below (as you'll see it's extremely simple).
Does anyone know what may be the reason?
Thanks for your help.
#define leftWheel 5 // PIN for Left Motor Communication is 5
#define rightWheel 3 // PIN for Right Motor Communication is 3
#define motorOff 0 // Power is at min (off)
#define motorOn 125 // Power is at max
int ledPin = 13; // LED connected to digital pin 13
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop()
{
delay(5000);
flashLedLight();
attemptFullAhead(); // turn on both motors
}
void flashLedLight() {
digitalWrite(ledPin, HIGH); // sets the LED on
delay(100);
digitalWrite(ledPin, LOW); // sets the LED off
}
void attemptFullAhead() {
analogWrite(leftWheel, motorOn);
analogWrite(rightWheel, motorOn);
digitalWrite(ledPin, HIGH); // sets the LED on
delay(20000); // isn't this 20 seconds?
// so shouldn't the motor be on for 20 seconds?
digitalWrite(ledPin, LOW); // sets the LED off
analogWrite(leftWheel, motorOff);
analogWrite(rightWheel, motorOff);
}