Hello, I use arduino for a few years, and so far I could use it without big issues, but there is one which is causing me headakes.
I'm controlling to motors using h bridge, so I need to control 4 pwm, two will be zero and the other two between zero and 255.
Each motor uses two pwm, each pwm controls the speed of a direction, so I can never have values grater than zero for the same motor. I have testes and confirmed it works.
I'm using arduino nano, 328. I tried many different ports, by the manual I can use 3, 5, 6, 9, 10, and 11.
In header:
#define MOTOR_A_PWM1 3
#define MOTOR_A_PWM2 5
#define MOTOR_B_PWM1 6
#define MOTOR_B_PWM2 9
In setup:
pinMode(MOTOR_A_PWM11, OUTPUT);
pinMode(MOTOR_A_PWM2, OUTPUT);
pinMode(MOTOR_B_PWM1, OUTPUT);
pinMode(MOTOR_B_PWM2, OUTPUT);
analogWrite(MOTOR_A_PWM1, LOW);
analogWrite(MOTOR_A_PWM2, LOW);
analogWrite(MOTOR_B_PWM1, LOW);
analogWrite(MOTOR_B_PWM2, LOW);
And the speed, I work from -250 to 250, to know what direction the motor must go. When the speed changes I call:
int absSpeed = abs(speed);
Serial.println(absSpeed);
if (speed > 0)
{
analogWrite(MOTOR_A_PWM1, 0);
analogWrite(MOTOR_B_PWM2, 0);
analogWrite(MOTOR_A_PWM2, absSpeed);
analogWrite(MOTOR_B_PWM1, absSpeed);
delay(100); //I added, changed values and removed this line to try but no lucky
}
else
{
analogWrite(MOTOR_A_PWM2, 0);
analogWrite(MOTOR_B_PWM1, 0);
analogWrite(MOTOR_A_PWM1, absSpeed);
analogWrite(MOTOR_B_PWM2, absSpeed);
delay(100);
}
So, let's go to the problem. When the speed is grater than zero (driving forward) both pwm's work. When I try to go backwards it goes for about 3 steps and briks the arduino.
I took everything out, let the arduino without any connection, and just tested the pwm output with a led, I still have the same issue.
I tried another arduino, brand new, the same happens. So it must be a programming issue. A limitation I never faced before, I really don't know...
Thank you very much for reading this far, if you have any ideas of what it could be, please reply.
Thiago