Hi,
I am having some trouble getting my project working completely. I am trying to control two motors with a hbridge over a serial connection. I am hoping to have a differential drive robot that I can control over usb and eventually wirelessly over Bluetooth. But at the moment I am just inputting the commands into the serial monitor. So problems I am having are as follows:
Problem 1:
The motor connected to the right side of the hbridge begins spinning at full speed when it is powered on regardless of the programming. I initially though this was a programming error but even when loading the AnalogReadSerial example program which has nothing to do with motors it still happens, it doesn’t happen with the bare minimum example program however. And it also jitters slightly when loading any program (before going to full speed) while the other motor does not.
Problem 2:
I have gotten the robot to go forwards and to the left but can’t figure out how to make it go back or to the right. I’m thinking this has something to do with problem one.
Problem 3:
Pretty sure I’m meant to have some capacitors and resistors in there, any advice on that would be welcome.
I’m not completely inexperienced with Arduino but still have lots to learn so anything to help me would be appreciated. Code + Fritzing below.
const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
const int motor3Pin = 1; // H-bridge leg 1 (pin 2, 1A)
const int motor4Pin = 2; // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 7; // H-bridge enable pin
const int enablePin2 = 8; // H-bridge enable pin
void setup() {
Serial.begin(9600); // set serial speed
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(motor3Pin, OUTPUT);
pinMode(motor4Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(enablePin2, OUTPUT);
digitalWrite(enablePin, HIGH);
digitalWrite(enablePin2, HIGH);
}
void loop(){
while (Serial.available() == 0); // do nothing if nothing sent
int val = Serial.read() - '0'; // deduct ascii value of '0' to find numeric value of sent number
Serial.println(val); //prints serial input to serial monitor
if (val == 0) // Go Forward
{
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
digitalWrite(motor3Pin, HIGH);
digitalWrite(motor4Pin, LOW);
}
else if (val == 1) // Turn robot left
{
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
digitalWrite(motor3Pin, HIGH);
digitalWrite(motor4Pin, LOW);
}
else if (val == 2) // Go Back DOESN'T WORK motor connected to right side of hbridge doesn't move
{
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
digitalWrite(motor3Pin, LOW);
digitalWrite(motor4Pin, HIGH);
}
else if (val == 3) { // Turn robot right DOESN'T WORK motor connected to right side of hbridge doesn't move
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
digitalWrite(motor3Pin, LOW);
digitalWrite(motor4Pin, HIGH);
}
else if (val == 4) //force stop
{
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, HIGH);
digitalWrite(motor3Pin, HIGH);
digitalWrite(motor4Pin, HIGH);
}
else
{
}
while(Serial.available()>0) //flush serial port
{
Serial.read();
}
}