Hi there,
I have a l293d motor driver. 2 motors are attached to motor driver. I want to control speed of both motors individually through 2 potentiometer. I wrote the following code to achieve this output.
#define E1 10 // Enable Pin for motor 1
#define E2 11 // Enable Pin for motor 2
#define I1 8 // Control pin 1 for motor 1
#define I2 9 // Control pin 2 for motor 1
#define I3 12 // Control pin 1 for motor 2
#define I4 13 // Control pin 2 for motor 2
#define pot1 0 //Potentiometer1
#define pot2 1 //Potentiometer2
int val1;
int val2;
void setup() {
pinMode(E1, OUTPUT);
pinMode(E2, OUTPUT);
pinMode(I1, OUTPUT);
pinMode(I2, OUTPUT);
pinMode(I3, OUTPUT);
pinMode(I4, OUTPUT);
digitalWrite(I1,LOW);
digitalWrite(I2,HIGH);
digitalWrite(I3,LOW);
digitalWrite(I4,HIGH);
}
void loop()
{
val1 = analogRead(pot1) / 4;
analogWrite(E1, val1);
delay(10);
val2 = analogRead(pot2) / 4;
analogWrite(E2, val2);
delay(10);
}
Only one motor is running at full speed. when I reduce the speed of motor1 through pot1 to 0 and again try to increase the speed of motor1 through pot1, motor2 starts spinning instead of motor1 and motor1 does not move at all. I think the problem is with the code because it is working fine with one motor. please help me to solve this problem.
Also, why so stingy with the symbolic constants? Their purpose is to make the code easier to read. Which is easier; your constants or:
#define ENABLEPINMOTOR1 10 // Enable Pin for motor 1
#define ENABLEPINMOTOR2 11 // Enable Pin for motor 2
#define CONTROLPIN1MOTOR1 8 // Control pin 1 for motor 1
#define CONTROLPIN2MOTOR1 9 // Control pin 2 for motor 1
#define CONTROLPIN1MOTOR2 12 // Control pin 1 for motor 2
#define CONTROLPIN2MOTOR2 13 // Control pin 2 for motor 2
#define POT1 0 //Potentiometer1
#define POT2 1 //Potentiometer2
Should any of the pins have the pin mode set to INPUT?
What I was trying to point out is that the pot pins that you are trying to read are 0 and 1, which are digital pins normally used by the Serial object. See if this change makes any difference:
How about the datasheet alicebj?
Block diagram on page 2. Left side is controlling a motor in 2 directions.
Right side is controlling 2 motors, with speed control.
How about posting an image of what you have connected up?