Controlling motor with limit switch inside if statements

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

}

Did you forget to use an INPUT_PULLUP pinMode(...) for LimR ?

Did you forget to use code tags and the autoformat function of the Arduino IDE before posting your code ?

That's a common symptom of using delay() in a program with two or more independent processes. You need to use two separate timing parts of your loop(). Do these using millis() comparison, and not delay().

@vaj4088, I think the OP may have been distracted from posting code correctly by that guy standing behind her.

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

PS ... To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum