Skip to main content
added 385 characters in body
Source Link
chrisl
  • 16.6k
  • 2
  • 18
  • 27

You need non-blocking code in combination with a finite state machine:

  1. Non-blocking code: You should not use delay(), since it is just busy waiting. Instead use the millis() function to measure the time distance between the last execution of the code part and the current time. If it has reached a certain level (like 1500ms), execute the code and save the current time in a timestamp as a reference for the next execution. Refer to the BlinkWithoutDelay example of the Arduino IDE. There are also many tutorials for that on the internet, so I will not explain this further here.

    Non-blocking code: You should not use delay(), since it is just busy waiting. Instead use the millis() function to measure the time distance between the last execution of the code part and the current time. If it has reached a certain level (like 1500ms), execute the code and save the current time in a timestamp as a reference for the next execution. Refer to the BlinkWithoutDelay example of the Arduino IDE. There are also many tutorials for that on the internet, so I will not explain this further here.

  2. Finite state machine: Here your loop() function contains of a switch statement with a (mostly numerical) state variable as parameter. Every case contains only the code for its special case. To get from one state to another you would just change the state variable to the corresponding value. You can also do this in an ISR (if you set the state variable as volatile). If you non-blocking code as described above, the loop() function is executed very fast, so that the change of the state variable inside the ISR takes effect almost immediately (delay will not be sensable by a human).

    Finite state machine: Here your loop() function contains of a switch statement with a (mostly numerical) state variable as parameter. Every case contains only the code for its special case. To get from one state to another you would just change the state variable to the corresponding value. You can also do this in an ISR (if you set the state variable as volatile). If you non-blocking code as described above, the loop() function is executed very fast, so that the change of the state variable inside the ISR takes effect almost immediately (delay will not be sensable by a human). Something like this:

    volatile int state=0;

    void isr_function(){ state++; if(state>1) state=0; }

    void loop(){ switch(state){ case 0: // state 0 do_something(); break; case 1: // state 1 do_another_thing(); break; } }

Note: Depending in your target you may want to add code for button debouncing, so that one button press is really sensed as one press, not more. This is relevant most times you have non-blocking code. Debouncing has been described many times in the internet, so just Google it

You need non-blocking code in combination with a finite state machine:

  1. Non-blocking code: You should not use delay(), since it is just busy waiting. Instead use the millis() function to measure the time distance between the last execution of the code part and the current time. If it has reached a certain level (like 1500ms), execute the code and save the current time in a timestamp as a reference for the next execution. Refer to the BlinkWithoutDelay example of the Arduino IDE. There are also many tutorials for that on the internet, so I will not explain this further here.
  2. Finite state machine: Here your loop() function contains of a switch statement with a (mostly numerical) state variable as parameter. Every case contains only the code for its special case. To get from one state to another you would just change the state variable to the corresponding value. You can also do this in an ISR (if you set the state variable as volatile). If you non-blocking code as described above, the loop() function is executed very fast, so that the change of the state variable inside the ISR takes effect almost immediately (delay will not be sensable by a human).

Note: Depending in your target you may want to add code for button debouncing, so that one button press is really sensed as one press, not more. This is relevant most times you have non-blocking code. Debouncing has been described many times in the internet, so just Google it

You need non-blocking code in combination with a finite state machine:

  1. Non-blocking code: You should not use delay(), since it is just busy waiting. Instead use the millis() function to measure the time distance between the last execution of the code part and the current time. If it has reached a certain level (like 1500ms), execute the code and save the current time in a timestamp as a reference for the next execution. Refer to the BlinkWithoutDelay example of the Arduino IDE. There are also many tutorials for that on the internet, so I will not explain this further here.

  2. Finite state machine: Here your loop() function contains of a switch statement with a (mostly numerical) state variable as parameter. Every case contains only the code for its special case. To get from one state to another you would just change the state variable to the corresponding value. You can also do this in an ISR (if you set the state variable as volatile). If you non-blocking code as described above, the loop() function is executed very fast, so that the change of the state variable inside the ISR takes effect almost immediately (delay will not be sensable by a human). Something like this:

    volatile int state=0;

    void isr_function(){ state++; if(state>1) state=0; }

    void loop(){ switch(state){ case 0: // state 0 do_something(); break; case 1: // state 1 do_another_thing(); break; } }

Note: Depending in your target you may want to add code for button debouncing, so that one button press is really sensed as one press, not more. This is relevant most times you have non-blocking code. Debouncing has been described many times in the internet, so just Google it

Source Link
chrisl
  • 16.6k
  • 2
  • 18
  • 27

You need non-blocking code in combination with a finite state machine:

  1. Non-blocking code: You should not use delay(), since it is just busy waiting. Instead use the millis() function to measure the time distance between the last execution of the code part and the current time. If it has reached a certain level (like 1500ms), execute the code and save the current time in a timestamp as a reference for the next execution. Refer to the BlinkWithoutDelay example of the Arduino IDE. There are also many tutorials for that on the internet, so I will not explain this further here.
  2. Finite state machine: Here your loop() function contains of a switch statement with a (mostly numerical) state variable as parameter. Every case contains only the code for its special case. To get from one state to another you would just change the state variable to the corresponding value. You can also do this in an ISR (if you set the state variable as volatile). If you non-blocking code as described above, the loop() function is executed very fast, so that the change of the state variable inside the ISR takes effect almost immediately (delay will not be sensable by a human).

Note: Depending in your target you may want to add code for button debouncing, so that one button press is really sensed as one press, not more. This is relevant most times you have non-blocking code. Debouncing has been described many times in the internet, so just Google it