[SOLVED] Large linear actuators

Hello! I am new to Arduino, and this is my first project. I have been reading a lot, and learning as I go, but I'm having trouble with control of a large linear actuator using the built-in feedback potentiometer. I am using this tutorial as a base guideline:

http://learn.robotgeek.com/demo-code/123-arduino-linear-actuator-tutorial-preset-position-button-control.html

I have the Arduino Uno, SensorShield, servo, two relays, one button, and power supply.

This is my goal:

I want to push a button, have a delay, retract the servo to a specific position, have a delay, then extend the servo to a specific position, and turn off (go idle).

This seems pretty simple logical, I'm just getting a bit lost with the code. Any help would be greatly appreciated.

Thanks!

Hi Jacob4130,

As far as i understand, only difference from the example that you have posted, is one button action and 2 actuator moves right? You want to push the button and desire to actuator move, lets say, first 200 then wait for 5 seconds, then move to 600 and stop the actuator.

Then what you need to do different is,

 if (button1State == HIGH) {     
    // set new goal position
    goalPosition = 200; 
    
    if (goalPosition > CurrentPosition) {
        Retracting = false;
        Extending = true;
        digitalWrite(relay1Pin, HIGH);  
        digitalWrite(relay2Pin, LOW);  
        Serial.println("Extending");
        delay(5000);
    
    }      
    else if (goalPosition < CurrentPosition) {
        Retracting = true;
        Extending = false;
        digitalWrite(relay1Pin, LOW);  
        digitalWrite(relay2Pin, HIGH); 
        Serial.println("Retracting");
        delay(5000);         
    }
   goalPosition = 600; 
    
    if (goalPosition > CurrentPosition) {
        Retracting = false;
        Extending = true;
        digitalWrite(relay1Pin, HIGH);  
        digitalWrite(relay2Pin, LOW);  
        Serial.println("Extending");
        delay(5000);
    
    }      
    else if (goalPosition < CurrentPosition) {
        Retracting = true;
        Extending = false;
        digitalWrite(relay1Pin, LOW);  
        digitalWrite(relay2Pin, HIGH); 
        Serial.println("Retracting");
        delay(5000);         
    }
  }

Remove button 1 and 2 code out and replace with this code. You need first set the goal position to desired pos, then wait as you like, and consequent goal position as you desire. In loop, program will read the code consequently. My suggestion is as i understand. Correct me if i am missing sth.

Regards.

Yes, I want one button to move the actuator twice (with a delay in between).

I upload the code you sent me and this is what happens:

Button press, 5 sec delay, actuator moves out..... stays there

button press, actuator moves in (without delay) and stops

Any ideas??

This is what I have currently:

// constants won't change. They're used here to set pin numbers:
const int button1Pin = 2; // the number of the pushbutton1 pin
const int relay1Pin = 7; // the number of the Realy1 pin
const int relay2Pin = 8; // the number of the Relay2 pin
const int sensorPin = 0; // select the input pin for the potentiometer

// variables will change:
int button1State = 0; // variable for reading the pushbutton status
int sensorValue = 0; // variable to store the value coming from the sensor

int goalPosition = 350;
int CurrentPosition = 0;
boolean Extending = false;
boolean Retracting = false;

void setup() {

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

// initialize the pushbutton pin as an input:
pinMode(button1Pin, INPUT);

// initialize the relay pin as an output:
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);

//preset the relays to LOW
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, LOW);

}

void loop(){

// read the value from the sensor:
CurrentPosition = analogRead(sensorPin);

// print the results to the serial monitor:
Serial.print("Current = " );
Serial.print(CurrentPosition);
Serial.print("\t Goal = ");
Serial.println(goalPosition);

// read the state of the pushbutton values:
button1State = digitalRead(button1Pin);

if (button1State == HIGH) {
// set new goal position
goalPosition = 200;

if (goalPosition > CurrentPosition) {
Retracting = false;
Extending = true;
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, LOW);
Serial.println("Extending");
delay(5000);

}
else if (goalPosition < CurrentPosition) {
Retracting = true;
Extending = false;
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, HIGH);
Serial.println("Retracting");
delay(5000);
}
goalPosition = 600;

if (goalPosition > CurrentPosition) {
Retracting = false;
Extending = true;
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, LOW);
Serial.println("Extending");
delay(5000);

}
else if (goalPosition < CurrentPosition) {
Retracting = true;
Extending = false;
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, HIGH);
Serial.println("Retracting");
delay(5000);
}
}
}

I should note: the servo is currently moving it's full length, not stopping short of fully extended or fully retracted.

This is the servo I am using:

https://www.servocity.com/html/560_lbs__thrust_linear_actuato.html#.Vdkfoig_kmw

I have the 8" stroke

One more note:

According to the serial monitor, the actuator has a stroke from 27 to 931

Can you see retracting text while retracting and extending text while it extending? Also there must be the rest of the code did you add them also?

Yes, the serial monitor says "extending" and "retracting" while the actuator is moving.

I don't understand what you mean by the rest of the code...

Do you mean this part:

  if (Extending = true && CurrentPosition > goalPosition) {
    //we have reached our goal, shut the relay off
    digitalWrite(relay1Pin, LOW); 
    boolean Extending = false; 
    Serial.println("IDLE");  
  }
  
  if (Retracting = true && CurrentPosition < goalPosition){
    //we have reached our goal, shut the relay off
    digitalWrite(relay2Pin, LOW); 
    boolean Retracting = false; 
    Serial.println("IDLE");  
  }


  
  
}

I will put that back in and see what happens

Okay, here's what we have currently:

Button press, actuator extends, 5 sec delay, relay turns off.

Button press, actuator retracts, 5 sec delay, relay turns off

// constants won't change. They're used here to set pin numbers:
const int button1Pin = 2;      // the number of the pushbutton1 pin
const int relay1Pin =  7;      // the number of the Realy1 pin
const int relay2Pin =  8;      // the number of the Relay2 pin
const int sensorPin = 0;       // select the input pin for the potentiometer

// variables will change:
int button1State = 0;          // variable for reading the pushbutton status
int sensorValue = 0;           // variable to store the value coming from the sensor

int goalPosition = 300; 
int CurrentPosition = 0; 
boolean Extending = false;
boolean Retracting = false;

void setup() { 


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

  // initialize the pushbutton pin as an input:
  pinMode(button1Pin, INPUT);     
   
  // initialize the relay pin as an output:
  pinMode(relay1Pin, OUTPUT);    
  pinMode(relay2Pin, OUTPUT);    
  
  //preset the relays to LOW
  digitalWrite(relay1Pin, LOW); 
  digitalWrite(relay2Pin, LOW); 

  
}

void loop(){
  
  // read the value from the sensor:
  CurrentPosition = analogRead(sensorPin); 

  
  // print the results to the serial monitor:
  Serial.print("Current = " );                       
  Serial.print(CurrentPosition);      
  Serial.print("\t Goal = ");      
  Serial.println(goalPosition);  
  
  // read the state of the pushbutton values:
  button1State = digitalRead(button1Pin);


 if (button1State == HIGH) {     
    // set new goal position
    goalPosition = 800; 
    
    if (goalPosition > CurrentPosition) {
        Retracting = false;
        Extending = true;
        digitalWrite(relay1Pin, HIGH);  
        digitalWrite(relay2Pin, LOW);  
        Serial.println("Extending");
        delay(5000);
    
    }      
    else if (goalPosition < CurrentPosition) {
        Retracting = true;
        Extending = false;
        digitalWrite(relay1Pin, LOW);  
        digitalWrite(relay2Pin, HIGH); 
        Serial.println("Retracting");
        delay(5000);         
    }
   goalPosition = 100; 
    
    if (goalPosition > CurrentPosition) {
        Retracting = false;
        Extending = true;
        digitalWrite(relay1Pin, HIGH);  
        digitalWrite(relay2Pin, LOW);  
        Serial.println("Extending");
        delay(5000);
    
    }      
    else if (goalPosition < CurrentPosition) {
        Retracting = true;
        Extending = false;
        digitalWrite(relay1Pin, LOW);  
        digitalWrite(relay2Pin, HIGH); 
        Serial.println("Retracting");
        delay(5000);         
    }
  }


  if (Extending = true && CurrentPosition > goalPosition) {
    //we have reached our goal, shut the relay off
    digitalWrite(relay1Pin, LOW); 
    boolean Extending = false; 
    Serial.println("IDLE");  
  }
  
  if (Retracting = true && CurrentPosition < goalPosition){
    //we have reached our goal, shut the relay off
    digitalWrite(relay2Pin, LOW); 
    boolean Retracting = false; 
    Serial.println("IDLE");  
  }


  
  
}

Any ideas??

Okay... after messing with the code all day, I have made some great progress. I have achieved everything I want, except for one flaw.

When I press the button, I get a 3 sec delay, actuator extends to 800, 3 sec delay, actuator retracts.

All great, except when the actuator retracts it goes all the way in (~27) and does not stop at it's goal position of 100.

Any ideas??

// constants won't change. They're used here to set pin numbers:
const int button1Pin = 2;      // the number of the pushbutton1 pin
const int relay1Pin =  7;      // the number of the Realy1 pin
const int relay2Pin =  8;      // the number of the Relay2 pin
const int sensorPin = 0;       // select the input pin for the potentiometer

// variables will change:
int button1State = 0;          // variable for reading the pushbutton status
int sensorValue = 0;           // variable to store the value coming from the sensor

int goalPosition = 100; 
int CurrentPosition = 0; 
boolean Extending = false;
boolean Retracting = false;

void setup() { 


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

  // initialize the pushbutton pin as an input:
  pinMode(button1Pin, INPUT);     
   
  // initialize the relay pin as an output:
  pinMode(relay1Pin, OUTPUT);    
  pinMode(relay2Pin, OUTPUT);    
  
  //preset the relays to LOW
  digitalWrite(relay1Pin, LOW); 
  digitalWrite(relay2Pin, LOW); 

  // read the value from the sensor:
  CurrentPosition = analogRead(sensorPin); 

  
  // print the results to the serial monitor:
  Serial.print("Current = " );                       
  Serial.print(CurrentPosition);      
  Serial.print("\t Goal = ");      
  Serial.println(goalPosition);
  
  
}

void loop(){
  
  // read the value from the sensor:
  CurrentPosition = analogRead(sensorPin); 

  
  // print the results to the serial monitor:
  Serial.print("Current = " );                       
  Serial.print(CurrentPosition);      
  Serial.print("\t Goal = ");      
  Serial.println(goalPosition);  

  // read the state of the pushbutton values:
  button1State = digitalRead(button1Pin);


  if (button1State == HIGH) {     
    // set new goal position
    goalPosition = 800; 
    delay(3000);
    
    if (goalPosition > CurrentPosition) {
      Retracting = false;
      Extending = true;
      digitalWrite(relay1Pin, HIGH);  
      digitalWrite(relay2Pin, LOW);  
      Serial.println("Extending");
    }
  }
  
  
  if (CurrentPosition >= 800) {
    goalPosition = 100;
    delay(3000);
        
    if (goalPosition < CurrentPosition) {
      Retracting = true;
      Extending = false;
      digitalWrite(relay1Pin, LOW);  
      digitalWrite(relay2Pin, HIGH); 
      Serial.println("Retracting");
    }
  }             



  if (Extending = true && CurrentPosition > goalPosition) {
    //we have reached our goal, shut the relay off
    digitalWrite(relay1Pin, LOW); 
    boolean Extending = false; 
    Serial.println("IDLE");  
  }
  
  if (Retracting = true && CurrentPosition < goalPosition) {
    //we have reached our goal, shut the relay off
    digitalWrite(relay2Pin, LOW); 
    boolean Retracting = false; 
    Serial.println("IDLE");  
  } 
}

SOLVED!!!

I figured it out! In an effort to clean up my loop section (in order for it to run faster) I made new functions called "extend" and "retract" and called them up when conditions were met.

NOTE: I was initially writing the code to extend-retract for ease of conceptual thinking. I wanted the final product to retract-extend, so that is how this most recent code is written.

const int button1Pin = 2;
const int relay1Pin =  7;
const int relay2Pin =  8;
const int sensorPin = 0;

int button1State = 0;
int sensorValue = 0;

int goalPosition = 800; 
int currentPosition = 0;

boolean extending = false;
boolean retracting = false;

void setup() { 

  Serial.begin(9600);

  pinMode(button1Pin, INPUT);     
   
  pinMode(relay1Pin, OUTPUT);    
  pinMode(relay2Pin, OUTPUT);    
  
  digitalWrite(relay1Pin, LOW); 
  digitalWrite(relay2Pin, LOW); 

}

void loop(){
  
  currentPosition = analogRead(sensorPin);
  button1State = digitalRead(button1Pin);

  Serial.print("Current = " );                       
  Serial.print(currentPosition);      
  Serial.print("\t Goal = ");      
  Serial.println(goalPosition);  


  if (button1State == HIGH) {
    retract();
  }


  if (retracting = true && currentPosition <= 200) {
    digitalWrite(relay2Pin, LOW); 
    boolean retracting = false;
  }
  
  if (currentPosition <= 200) {
    extend();
  }
  
  if (extending = true && currentPosition >= 800) {
    digitalWrite(relay1Pin, LOW);
    boolean extending = false;
  }
    
}

  
void retract() {
  delay(5000);  
  goalPosition = 200;


  retracting = true;
  extending = false;
  digitalWrite(relay1Pin, LOW); 
  digitalWrite(relay2Pin, HIGH); 
  Serial.println("Retracting");

}

void extend() {  
  delay(1000);
  goalPosition = 800;

  
  extending = true;
  retracting = false;
  digitalWrite(relay1Pin, HIGH);
  digitalWrite(relay2Pin, LOW);
  Serial.println("Extending");
  
}