H Bridge PWM issue

You guys were instrumental in helping me see the obvious yesterday, so perhaps the same will happen today.

I have a linear actuator with a pot for feedback. I have a second pot I'm using to control the position of the actuator. The program reads the linear actuator pot, then reads the control pot, compares the two, and moves the actuator if needed. I have a +-5 null (on the 0-255 scale).

The actuator moves about 2 inches per second at full blast, so I'm using PWM to vary the speed. I have a H-Bridge IC controlling the motor. When it extends, it works perfectly. It moves full blast until it is 50 units away from the target, then slows down to about 75% speed, then to 50% speed until it gets to the 5 value null. It is supposed to do that when it retracts, but it doesn't. It's full blast or nothing.

I set up a variable updown to show what mode it is supposed to be in. As I write this, it is stuck in 22, or it should be retracting (first 2), second speed (175%), but it won't budge. My gap between pot readings is 22.

It does this when retracting no matter what- once the value of the gap is less than 50, it stops moving. The serial monitor shows that it is in mode 22, but it won't move. Anyone have any ideas please? I would greatly appreciate it! Thanks.

int motorpin1 = 8;
int motorpin2 = 9;
int pwmspeed = 0;
int updown = 1;
int target = 0;
int sensorvalue = 0;
int gap = 0;

void setup() {
  // initialize serial communications:
  Serial.begin(9600);
pinMode(motorpin1,OUTPUT);
pinMode(motorpin2,OUTPUT);
analogWrite(motorpin1,0);
analogWrite(motorpin2,0);
}

void loop() {

  sensorvalue = analogRead(A1);   //get value from the actuator pot
  target = analogRead(A2);            //get value from control input pot that tells it how far to extend
  
  if (sensorvalue < target) {                    //if the actuator sensor is less than control, extend the actuator
  gap = target - sensorvalue;                //calculate the difference between the two pots 
  if (gap>5) {                                             //if the gap between the pots is small, move the actuator slowly
  analogWrite(8,0);
  analogWrite(9,125);
updown=11;}
  if (gap>15) {                               //if the gap is pretty close, move it medium speed
    analogWrite(8,0);
  analogWrite(9,200);
updown=21;}                             //if the gap is far between the two pots, move fast
  if (gap>50) {
    analogWrite(8,0);
  analogWrite(9,255);
updown=31;}
  delay(100);                        //pause briefly so the actuator can move
  }
  
  if (sensorvalue > target) {             //if the value of the actuator is greater than the desired position, retract the actuator
  gap = sensorvalue - target;         // slow retract
  if (gap>5) {
    analogWrite(8,125);
    analogWrite(9,0);
    updown=12;
  }
  if (gap>15) {                          //medium retract
    analogWrite(8,200);
    analogWrite(9,0);
    updown=22;
  }
  if (gap>50) {                             //fast retract
    analogWrite(8,255);
    analogWrite(9,0);
    updown=32;
  }
  delay(100);                         //pause to let it move
  }
  
  
   // print the values
  Serial.print(sensorvalue);
  Serial.print("\t");
  Serial.print(target);
   Serial.print("\t");
   Serial.print(gap);
   Serial.print("\t");
   Serial.print(updown);
  
  Serial.println(); 
}

I have an idea. Why don't you draw a schematic with pen and paper and take a photo with your cell phone and post it so we can see exactly how it is wired. It would be nice if we could see a photo of the circuit wiring but I don't know if this is possible.

Also, if you want us to help you with the code, why don't you help us by editing your code (using the MODIFY button) and adding comments explaining every line of code. (I see no mention of RETRACT or EXTEND in the code , probably because there are NO comments. If you want our help, provide the information we need to do that.

Will do. sorry about that.

You don't specify which arduino that you are using, but if a Uno pin 8 (motorpin1) is not PWM.

BINGO !

I'm using the Uno, but I plan to use an ATtiny85 eventually.

So I need to change the pins you think? That would explain it.

Will 9 and 10 work then? (I guess I'll find out shortly..)

YES.
d3,5,6,9,10 & 11 are PWM.

Works perfectly. Thanks guys!

You can use a one digital pin and one PWM pin to control an h-bridge. The digital pin is direction and the PWM pin is speed. If the direction pin is a 0 the PWM 0 is stop and 255 is full speed. If the direction pin is a 1 control is inverted, so PWM 0 is full speed and 255 is stop.

Makes sense. Thanks. I was doing the opposite in the case of retracting the actuator.

On many motor contollers, these signals go by the names DIR, & EN, or INn, & EN

Sorry, I should have been more specific. I meant the two input h-bridges like the 754110 or 293. I have no experience with motor control shields and they may work differently.

754110

I think you mean 754410

Right. Thanks.