HI
I have just tried running two dc motors on the same l298n motor driver using my arduino uno, however when I upload the code, only one dc motor will move. Here is the code:
//Motor A
const int motorPin1 = 9;
const int motorPin2 = 10;
//Motor B
const int motorPin3 = 6;
const int motorPin4 = 5;
void setup(){
//Set pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
analogWrite(motorPin1, 0);
analogWrite(motorPin2, 1000);
analogWrite(motorPin3, 1000);
analogWrite(motorPin4, 0);
analogWrite(motorPin1, 1000);
analogWrite(motorPin2, 0);
analogWrite(motorPin3, 1000);
analogWrite(motorPin4, 0);
}
void loop(){
analogWrite(motorPin1, 0);
analogWrite(motorPin2, 1000);
analogWrite(motorPin3, 1000);
analogWrite(motorPin4, 0);
analogWrite(motorPin1, 1000);
analogWrite(motorPin2, 0);
analogWrite(motorPin3, 1000);
analogWrite(motorPin4, 0);
}
Help would be much appreciated.
analogWrite(motorPin2, 1000);
Have a look at the analogWrite() function reference. The analogWrite() function takes a number from 0 to 255.
In loop() you turn one motor on forward and the other in reverse then some microseconds later reverse the first and run the second forward.
What happens when you try this?
void loop(){
analogWrite(motorPin1, 0);
analogWrite(motorPin2, 255);
analogWrite(motorPin3, 255);
analogWrite(motorPin4, 0);
delay(10000); // run for 10 seconds
analogWrite(motorPin1, 0); // stop
analogWrite(motorPin2, 0);
analogWrite(motorPin3, 0);
analogWrite(motorPin4, 0);
delay(2000); // stop for 2 seconds
analogWrite(motorPin1, 255);
analogWrite(motorPin2, 0);
analogWrite(motorPin3, 255);
analogWrite(motorPin4, 0);
delay(10000); // run for 10 seconds
analogWrite(motorPin1, 0); // stop
analogWrite(motorPin2, 0);
analogWrite(motorPin3, 0);
analogWrite(motorPin4, 0);
delay(2000); // stop for 2 seconds
}
How would I make them run forever without stopping
Hi,
Welcome to the forum.
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Thanks.. Tom...