I've got problems with connecting two DC motors to an arduino with an additional 9v battery. I've built an example of it in fritzing and attatched it to this post. While this scheme used to work, it is really unreliable lately, sometimes the motors will spin for a few seconds before they wont spin again. This seems to happen randomly. Does anyone know how to fix this problem so that the motors will spin consistently?
This is my source code
int motorPin = 9;
int motorPin2 = 3;
void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(motorPin2, OUTPUT);
}
void loop()
{
if (Serial.available())
{
analogWrite(motorPin, 50);
analogWrite(motorPin2, 50);
delay(2000);
}
{
analogWrite(motorPin, 100);
analogWrite(motorPin2, 100);
delay(2000);
} {
analogWrite(motorPin, 150);
analogWrite(motorPin2, 150);
delay(2000);
} {
analogWrite(motorPin, 200);
analogWrite(motorPin2, 200);
delay(2000);
}
{
analogWrite(motorPin, 255);
analogWrite(motorPin2, 255);
delay(2000);
}
}
