Alright, here is a simple program that isn't working as it should and I have no idea why.
I have hooked a motor to an H-Bridge and placed pins 5 (PD3) and 6 (PD4) to my ATmega328 (from the UNO) to the logic of the H-Bridge and placed pin 3 (PD1) to the enable of the H-Bridge. Now, the idea is to PWM the enable of the H-Bridge while the Arduino pins 5 & 6 control the direction the motor will spin.
The PWM works fine (the motor spins faster and slower over time), but it spins only in
one direction and I have no idea why. Here is a simple program that I made for testing:
//Motor Through H-Bridge Test
void driveMotor(int);
void setup() {
Serial.begin(9600);
delay(100);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}//end setup()
void loop() {
for (int i = -250; i <= +250; i += 25 ) {
driveMotor(i);
delay(100);
}//end for
}//end loop()
void driveMotor(int torque) {
//pins 5 and 6 go to logic of H-Bridge.
if (torque >= 0) {// drive motors forward
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
} else if (torque < 0) { // drive motors backward
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
torque = abs(torque); //turn value pos.
}//end if
analogWrite(3,torque); //PWM to enable of H-Bridge
}//end driveMotor()
This is part of a balancing robot that I'm working on, so... help?
Thanks.
Brian