Actuator Position Control Program

Hi,

I have written a code that moves an actuator back and fourth to defined positions for 60 cycles. The linear actuator has a built in potentiometer.

The process is:
-waits for user input of “1” to start code
-starts at 0 position 0
-extends to position 2
-pauses for two seconds
-retracts to 0 position
-pauses for two seconds
-repeats 60 times.

For reference, I have a linear actuator with built in potentiometer.
I have an Arduino Uno, and H Bridge. .

const int enablepin=13; //Set the Enable jumper to D13 (Megamoto Shield to arduino)variable, digital pin
const int PWMA = 11; // Set PWMA to pin 11, Actuator connnects here, digital pin
const int PWMB = 3;//Set PWMB to pin 3, Actuator connects here, digital pin
const int PotPin =1; //Sets the input pin [1] for the potentiometer, Analog pin

int cyclecount=0; //counts how many cycles have been completed
int pausetimeext = 2*1000; //5 seconds, times 1000 to convert to milliseconds
int pausetimeret = 2*1000; //5 seconds, times 1000 to convert to milliseconds
int currentposition =0; // Defines current position
int position0 =1; // Analog input range of 0 to 1023
int position2= 171.5; // Analog input range of 0 to 1023 (1.5 inch??)
int startvar = 0; // variable to be assigned to start program via serial monitor
char rx_byte =0; 

void MoveTo(int position){
  if(position < 0 || position > 170.5)
 return; // ignore out of range values

  if(analogRead(PotPin)<position){
    //extend code
    Serial.println ("Extending"); // Prints message
    Serial.print ("Cycle Count: "); // Prints message
    Serial.println (cyclecount); // Prints message
    digitalWrite (PWMA, LOW); //Set PWMA to activate low
    digitalWrite(PWMB, HIGH); //Set PWMB to activate high
    digitalWrite(enablepin,HIGH); //Enable the motor. Actuator should begin to extend
    while (analogRead(PotPin)<position){/* JUST WAITING*/}
    stopActuator (); //disable motor
  }
  else if (analogRead(PotPin)<position) {
    //retract code
    Serial.println ("Retracting"); // Prints message
    Serial.print ("Current Position ");
    Serial.print (analogRead(PotPin));
    digitalWrite(PWMA,HIGH); //Set PWMA to activate high
    digitalWrite(PWMB, LOW); //Set PWMB to activate low
     Serial.println ("2"); // Prints message
    digitalWrite (enablepin, HIGH); // ENable the motor, actuator should begin to retract
     Serial.println ("3"); // Prints message
    cyclecount=cyclecount+1; // Add one to cycle count 
    while(analogRead(PotPin)>position) {/* JUST WAITING*/}
   stopActuator (); //disable motor
  }
}

void setup() {

  Serial.begin(9600); //start serial connection

  pinMode (enablepin,OUTPUT); //Enable the board, sets output
  pinMode (PWMA, OUTPUT); //Set Motor Ouputs
  pinMode (PWMB, OUTPUT); //Set Motor Ouputs
  pinMode (PotPin, INPUT); //sets potentiometer sensor as an input, feedback from actuator
  
  digitalWrite (PWMA, LOW); //Presets the outputs to low
  digitalWrite (PWMB, LOW); //Presets the outputs to low
  cyclecount=0; // Set Cycle Counter to 0 before test starts

      // Repeat 60 times
}
void loop() {
  //main code here, to run repeatedly:
  if ( Serial.available ()>0) {     //See if input avalible
    rx_byte = Serial.read();        // retrieves input character provided by user

  if((rx_byte > '0')&& (rx_byte < '2'))  {    //Checks if input recieved to start. 1 is required entry
    Serial.println ("Input Recieved, Starting"); // singals program is starting, commence loop
    Serial.print ("Current Position ");
    Serial.print (analogRead(PotPin));
    
    for (int i =0;  i < 60; i++) {
        MoveTo(position2);
        delay(pausetimeext);
        MoveTo(position0);
        delay(pausetimeret);
    }
  }
}
}
void stopActuator () {
  digitalWrite (PWMA, LOW); //Set PWMA to activate low
  digitalWrite(PWMB, LOW); //Set PWMB to activate low
}

The code starts to extend, but does not retract. It appears to enter an endless loop, with the display output being “extracting” continuously. I have attached a screenshot.

What is wrong and how can I fix this?

THANK YOU!!

Actuator_Position_Control_Rev_C.ino (3.31 KB)

int position2= 171.5; // Analog input range of 0 to 1023 (1.5 inch??)

You have a fundamental misunderstanding here. An integer can never hold the value 171.5. It can hold 171 or 172 but never 171.5. The compiler will pick one integer (do you know which one?) and it will never be equal to the value 171.5.

Later on, you compare an integer to 170.5. That's not going to work like you expect either.

Do I need to continue or have you found the problem?

Thanks, that makes sense.

I am still having trouble getting the code to execute as intended after making the adjustment. Any ideas?

What does the new code (that you did not post) do?

Post the revised code.

kamaj5:

    while (analogRead(PotPin)<position){/* JUST WAITING*/}

stopActuator (); //disable motor

I’m much less knowledgeable than most around here but, it looks as if you have a while loop which does nothing. I could be wrong tho’.

dougp:
I'm much less knowledgeable than most around here but, it looks as if you have a while loop which does nothing. I could be wrong tho'.

Not quite. It calls stopActuator() repeatedly. There is a difference between "doing nothing" and "doing anything useful".