I have 6 linear actuators connected to 3 CanaKit H bridges attached to a 2560. Each dual h bridge has two sets of IN1, IN2, and EN. I raise the either one of the IN* pins to set motor direction and then send a PWM on EN to control motor speed. The IN* pins are connected to arduino pins 40-52. The PWM pins are connected to 8-13. The H Bridges have a 12v3a power supply that also drives their logic. The arduino is getting 5v from the USB.
The following code works and the motors go either way:
for(i=0;i<6;++i) {
// ReadActuatorPos() reads the potentiometer on the actuator. Analog pins 0...6
while(ReadActuatorPos(PWM1+i)>0.5) SetActuatorSpeed(PWM1+i,-1); // retract
while(ReadActuatorPos(PWM1+i)<0.5) SetActuatorSpeed(PWM1+i,1); // extend
SetActuatorSpeed(PWM1+i,0);
}
However: in the following code the motors will only extend if they're too close. They refuse to retract when they're too far.
do {
j=0;
for(i=0;i<6;++i) {
f=ReadActuatorPos(PWM1+i);
if(f>0.51) SetActuatorSpeed(PWM1+i,-1); // this is being called, so why isn't it working?
else if(f<0.49) SetActuatorSpeed(PWM1+i,1);
else {
SetActuatorSpeed(PWM1+i,0);
++j;
}
}
} while(j<6);
Since the motors are all able to extend at the same time I don't think it's some kind of voltage drop. The H bridges have LEDs indicating which way the motors are moving. When the system is working the appropriate lights are on. When I run the non-working code the wrong lights are lit - it looks as if both INs are raised, leading me to think the arduino is somehow sending the wrong signals.
Maybe it's something else I don't see, so here's SetActuatorSpeed.
void SetActuatorSpeed(int pwm,float newspeed) {
int pin1, pin2, mode1, mode2;
// these are according to the CanaKit instruction manual that came with the boards.
if( newspeed > 0 ) {
mode1=HIGH;
mode2=LOW;
} else if( newspeed < 0 ) {
mode1=LOW;
mode2=HIGH;
newspeed=-newspeed; // because we can't supply a negative value to analogWrite()
} else {
// mode1=HIGH, mode2=HIGH is the same as mode1=LOW, mode2=LOW
mode1=HIGH;
mode2=HIGH;
}
// PWM1 is pin 8. The rest are sequential.
// IN1 is pin 40. The rest are sequential.
switch(pwm) {
case PWM1: pin1=IN1; pin2=IN2; break;
case PWM2: pin1=IN3; pin2=IN4; break;
case PWM3: pin1=IN5; pin2=IN6; break;
case PWM4: pin1=IN7; pin2=IN8; break;
case PWM5: pin1=IN9; pin2=IN10; break;
case PWM6: pin1=IN11; pin2=IN12; break;
default: return;
}
// set direction
digitalWrite(pin1,mode1);
digitalWrite(pin2,mode2);
// set speed
analogWrite(pwm,newspeed*SERVO_SCALE); // SERVO_SCALE is 255.newspeed is always 0.0 .... 1.0
}
I've been fighting this thing for two days with a lot of Serial.print() to diagnose and so far I've found nothing.
What am I doing wrong?