Linear Actuator Control

Hi All,

I am working on a project for school that involves have a linear actuator move in and out repeatedly.

I what the actuator to extend out to a set position and once it has reached the set point imminently start retracting to the start point and then keep repeating this until i can push a button to stop it.

I have no experience with coding so i am finding it really difficult to get it going! I have found a tutorial that shows you how to used two buttons, one that when pressed the actuator will extend to a set point and then when the other is pressed the actuator will retract. I will put the code below.

How can i adapt this code to do what i want to do?

*/

// constants won't change. They're used here to set pin numbers:
const int button1Pin = 2; // the number of the pushbutton1 pin
const int button2Pin = 4; // the number of the pushbutton2 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 button2State = 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);
pinMode(button2Pin, 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);
button2State = digitalRead(button2Pin);

if (button1State == HIGH) {
// set new goal position (where it start point is)
goalPosition = 0;

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

if (button2State == HIGH) {
// set new goal position
goalPosition = 150;

if (goalPosition > CurrentPosition) {
Retracting = false;
Extending = true;
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, LOW);
Serial.println("Extending");
}
else 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("REPEAT");
}

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

}

Please visit here. - Scotty

Hi All,

I am working on a project for school that involves have a linear actuator move in and out repeatedly.

I what the actuator to extend out to a set position and once it has reached the set point imminently start retracting to the start point and then keep repeating this until i can push a button to stop it.

I have no experience with coding so i am finding it really difficult to get it going! I have found a tutorial that shows you how to used two buttons, one that when pressed the actuator will extend to a set point and then when the other is pressed the actuator will retract. I will put the code below.

How can i adapt this code to do what i want to do?

Thanks in advance for any help!

 */

// constants won't change. They're used here to set pin numbers:
const int button1Pin = 2;     // the number of the pushbutton1 pin
const int button2Pin = 4;     // the number of the pushbutton2 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 button2State = 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);     
  pinMode(button2Pin, 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);
  button2State = digitalRead(button2Pin);

  if (button1State == HIGH) {     
    // set new goal position (where it start point is)
    goalPosition = 0; 
    
    if (goalPosition > CurrentPosition) {
        Retracting = false;
        Extending = true;
        digitalWrite(relay1Pin, HIGH);  
        digitalWrite(relay2Pin, LOW);  
        Serial.println("Extending");     
    }      
    else if (goalPosition < CurrentPosition) {
        Retracting = true;
        Extending = false;
        digitalWrite(relay1Pin, LOW);  
        digitalWrite(relay2Pin, HIGH); 
        Serial.println("Retracting");         
    }  
  }

  if (button2State == HIGH) {     
    // set new goal position
    goalPosition = 150; 
    
    if (goalPosition > CurrentPosition) {
        Retracting = false;
        Extending = true;
        digitalWrite(relay1Pin, HIGH);  
        digitalWrite(relay2Pin, LOW);  
        Serial.println("Extending");   
    }        
    else 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("REPEAT");  
  }
  
  if (Retracting = true && CurrentPosition < goalPosition){
    //we have reached our goal, shut the relay off
    digitalWrite(relay2Pin, LOW); 
    boolean Retracting = false; 
    Serial.println("REPEAT");  
  }


  
  
}

(deleted)