Hi,
I am currently trying to code for a linear actuator that retracts when a button is pressed, I then want it to wait, and a motor to turn on until a limit switch is hit, once that limit switch is hit, the motor will stop and the actuator will return to its normal position. I can get the actuator and motor to move separately using an h bridge, however, am unable for this to work in the order that I want. Attached is my code
#define button 2
#define pull 5
#define push 6
#define power_up 9
#define power_down 8
#define LimR 4
int buttonstate;
int limitR;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(button, INPUT_PULLUP);
pinMode(pull, OUTPUT);
pinMode(push,OUTPUT);
pinMode(power_up, OUTPUT);
pinMode(power_down,OUTPUT);
pinMode(LimR, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
buttonstate = digitalRead(button);
limitR = digitalRead(LimR);
//Serial.println(buttonstate);
delay(200);
if (buttonstate == LOW)
{
Serial.println("ON");
digitalWrite(push,LOW);
digitalWrite(pull,HIGH);
delay(2000);
//digitalWrite(push,HIGH);
//digitalWrite(pull,LOW);
if (limitR == 1)
{
digitalWrite(push,HIGH);
digitalWrite(pull,LOW);
digitalWrite(power_up,LOW);
digitalWrite(power_down,LOW);
delay(100);
digitalWrite(push,LOW);
digitalWrite(pull,LOW);
}
if (limitR == 0)
{
digitalWrite(power_up,HIGH);
digitalWrite(power_down,LOW);
}
}
}