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();
}
