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?
Is it a bought H Bridge motor driver, or one you've made yourself?
What sort of motors are you using? Voltage, current rating etc?
Also, might help if you post a schematic of your circuitry.
I'm still new to all this myself to be honest, but providing the above information will make it easier for folk to help...
One thing I would say, have you tested your setup with a simpler program e.g. one that just brings the logic levels high and low for a period of time? Then you would know if the problem is in the program or in the wiring...
I know the problem isn't my H-Bridge, because when I physically put the bridge logic to HIGH and LOW, then the motors spin in a certain direction and when I reverse to LOW and HIGH the motors spin in the opposite direction (as it's suppose to). The problem is in the ATmega328 isn't sending the signal as I would expect and want to know if it's a coding error.
Thanks, though. I shall attempt to throw a schematic up there, too.
Hmm - the only thing that I see odd with your code is the "+250" in the comparison of the for loop; normally you would just use "250":
for (int i = -250; i <= 250; i += 25 ) {
...I am not sure if that is necessarily causing the issue. What happens if you change the code to:
for (int i = -250; i <= 0; i += 25 ) {
...also:
for (int i = 0; i <= 250; i += 25 ) {
I guess I am wondering which side of the zero point the issue is happening on.
I had something like this happen to me once on a 2n3055 TO-3 transistor h-bridge I had made, but that was due to a faulty alligator clip (needed clips to connect the collectors together); this isn't the issue in your case, though - obviously...
You need to check your port to pin assignments. In Arduino land, PD1 is digital pin 1 and PD4 is digital pin 4 (rather than the physical package pins 5 and 6 as used in your sketch).
BenF:
You need to check your port to pin assignments. In Arduino land, PD1 is digital pin 1 and PD4 is digital pin 4 (rather than the physical package pins 5 and 6 as used in your sketch).
Yes, you were very much correct; the problem was my pins. apparently when declaring:
pinMode(5, OUTPUT);
the 5 refers to PD5 and not pin 5.
I'm shocked I made it this far without knowing this. Perhaps I did know it in the past and am just too stupid to remember. Either way, thank you very much for the help.
BenF:
You need to check your port to pin assignments. In Arduino land, PD1 is digital pin 1 and PD4 is digital pin 4 (rather than the physical package pins 5 and 6 as used in your sketch).
Yes, you were very much correct; the problem was my pins. apparently when declaring:
pinMode(5, OUTPUT);
the 5 refers to PD5 and not pin 5.
I'm shocked I made it this far without knowing this. Perhaps I did know it in the past and am just too stupid to remember. Either way, thank you very much for the help.
Glad you sorted it out, but I wasn't aware that the labels on the diagram had anything to do with the code; from your description, you seemed fairly confident it was the code, not the hardware, so I made the (wrong) assumption that your code was configure properly for the hardware, and the hardware was hooked up properly. Hmm - next time you post something like this, you should post your -entire- schematic (all parts and connections) included, along with your complete code (which you did); it would save all of us time.