int motorforward =8; int motorbackward=9; int limit =3; int button=2; int delaytime=2150; int torque=200; boolean retracted; void setup() { // initialize the arm to a state which we know, gearmotor doesnt provide feedback // we will do this by moving the arm back until it hits the limit switch. pinMode(limit, INPUT); pinMode(button, INPUT); pinMode(motorforward, OUTPUT); pinMode(motorbackward, OUTPUT); analogWrite(motorbackward,torque); while(digitalRead(limit)==0) { //loop until back limit switch is pressed delay(3); //wait 3ms and check again } digitalWrite(motorbackward,LOW); retracted=true; //now that the position is known, the main loop can begin. } void loop() { if(digitalRead(button)==1) { // enter this big loop if button is pressed if(retracted) { analogWrite(motorforward, torque); //move arm forward delay(delaytime); //run motor for delaytime/1000 sec digitalWrite(motorforward, LOW); //stop motor after switch is pressed retracted = false; //remember that arm is now out } else //if not retracted do the following { analogWrite(motorbackward, torque); //set motor to move arm backward while(digitalRead(limit)==0) //loop until inner limit switch is pressed { delay(3); //check switch every 3ms } digitalWrite(motorbackward, LOW); //srop motor after switch is pressed retracted = true; //remember that arm is now in } } delay(10); //wait 10ms, before checking button again. }