> by the way I'm not a programmer... I'm a wannabe who's a EE
Then you must have heard of state machines right?
Some pseudo code
Code:
state = RUNNING;
while ( state != DONE ) {
if ( digitalRead(relaypin) )
state = BAILOUT
switch ( state )
case MOVE_LEFT
// bump motor one step left
// if goal reached, state = MOVE_RIGHT
case MOVE_RIGHT
// bump motor one step right
// if goal reached, state = DONE
case BAILOUT
// bump motor one step wherever
// if goal reached, state = DONE
// end case
delay()
}
The code doesn't "get stuck" in some deeply nested loop. There is just one loop with a state, and you decide what to do in each state, and when to move to a new state.
> it doesn't leave
Code:
relaystate=digitalRead(relaypin);
while (relaystate == HIGH)
{
// if Relay for the motor is high (5V) then run the pickup program)
if (relaystate == LOW){ goto bailout;}
It doesn't leave because you never call digitalRead(relaypin); inside the loop.