HI, i would like to start at motor after a switch has been high, low then high again.
How do i do that?
You use the 'while' structure to wait until the switch is in the position you want. You will have to do this three times to catch the sequence you want.
while( digitalRead(switchPin) == LOW) { } // wait until switch is HIGH
while( digitalRead(switchPin) == HIGH) { } // wait until switch is LOW
while( digitalRead(switchPin) == LOW) { } // wait until switch is HIGH
// now do stuff
Note this depends on how your switches are wired up as to what you get when you read them
But note that this code will block anything else in the program until the all conditions are met.
This is really a very basic case of a finite state machine.
State: waiting for Switch_First_High
then, when detected:
State: waiting for Switch_Low
then, when detected:
State: waiting for Switch_Second_High
then, when detected: immediately start motor.
It should be implemented without blocking.
Indeed it should, but that was not the question.
I think it is important that someone actually needs non blocking code before exposing them to what is probably so advanced it is incomprehensible. Allow beginners to be beginners and have the joy of discovery.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.