The Problem: I have students building an Arduino Car with two motors using an H-Bridge. We should be able to adjust the speed of each motor individually, but it seems that recently when we upload the code it is only allowing the following two scenarios:
- One pin is set to 255 and the other pin can be whatever value we set it to
- Both pins are set to exactly the same value
If both motors are a different value (one of which is not 255) then one or both show 0 voltage. Here is the code we are uploading:
//Motor 1
const int EN1 = 2;
const int MC1A = 3;
const int MC2A = 4;
//Motor 2
const int EN2 = 11;
const int MC3A = 9;
const int MC4A = 8;
void setup()
{
pinMode(EN1, OUTPUT);
pinMode(MC1A, OUTPUT);
pinMode(MC2A, OUTPUT);
pinMode(EN2, OUTPUT);
pinMode(MC3A, OUTPUT);
pinMode(MC4A, OUTPUT);
brake();
}
void loop()
{
forward();
delay(5000);
brake();
delay(1000);
reverse();
delay(5000);
brake();
delay(1000);
}
void forward ()
{
digitalWrite(MC1A, HIGH);
digitalWrite(MC2A, LOW);
analogWrite(EN1, 255);
digitalWrite(MC3A, HIGH);
digitalWrite(MC4A, LOW);
analogWrite(EN2, 255);
}
void reverse ()
{
digitalWrite(MC1A, LOW);
digitalWrite(MC2A, HIGH);
analogWrite(EN1, 255);
digitalWrite(MC3A, LOW);
digitalWrite(MC4A, HIGH);
analogWrite(EN2, 255);
}
void brake ()
{
digitalWrite(EN1, LOW);
digitalWrite(MC1A, LOW);
digitalWrite(MC2A, LOW);
digitalWrite(EN2, LOW);
digitalWrite(MC3A, LOW);
digitalWrite(MC4A, LOW);
}
We have tried this on an Arduion MEGA 2560, on an UNO, and on some generic boards like a Keyestudio Mega.
Any help would be appreciated.