Skip to main content
Arduino

Return to Answer

replaced http://arduino.stackexchange.com/ with https://arduino.stackexchange.com/
Source Link

How can I use millis() to blink forever?

The algorithm is essentially as follows:

if (it_is_time_to_toggle_the_led()) {
 toggle_the_led();
}

The details are thoroughly covered in the Blink Without Delay Arduino tutorial. It is essential to understand this technique: as soon as you have more than one thing to do in your program (here: blink an LED and monitor button presses), delay() is toxic because it blocks all the program. You should really ban delay() from your vocabulary, except for the simpler single-task programs.

Or just blink twice then stay on?

For this and your other questions, the canonical answer is "implement a finite state machine". I recommend the Finite State Machine tutorial by Majenko.

In this particular instance, I would probably implement two FSMs, one for each LED. The first one would have three states: (OFF, BLINKING, ON), and a counter for the number of blinks. The transition rules:

  1. If button 1 is pressed and the state is not BLINKING go to the BLINKING state
  2. If it is BLINKING and it's time for it to toggle, do it
  3. If it is BLINKING and it is switching on for the third time, go to the ON state
  4. If '#' has been pressed, go to the OFF state.

Or maybe you could split the state BLINKING into BLINKING_ON and BLINKING_OFF.

The second LED would be essentially the same, except for lacking the ON state and rule number 3.

I am not writing the code, but you may want to take a look at this code for automatic door code for automatic door. It is an answer to a seemingly unrelated question, but it gives a good example on how to combine millis()-based timing with a finite state machine. Your program could be written mostly along the same lines.

How can I use millis() to blink forever?

The algorithm is essentially as follows:

if (it_is_time_to_toggle_the_led()) {
 toggle_the_led();
}

The details are thoroughly covered in the Blink Without Delay Arduino tutorial. It is essential to understand this technique: as soon as you have more than one thing to do in your program (here: blink an LED and monitor button presses), delay() is toxic because it blocks all the program. You should really ban delay() from your vocabulary, except for the simpler single-task programs.

Or just blink twice then stay on?

For this and your other questions, the canonical answer is "implement a finite state machine". I recommend the Finite State Machine tutorial by Majenko.

In this particular instance, I would probably implement two FSMs, one for each LED. The first one would have three states: (OFF, BLINKING, ON), and a counter for the number of blinks. The transition rules:

  1. If button 1 is pressed and the state is not BLINKING go to the BLINKING state
  2. If it is BLINKING and it's time for it to toggle, do it
  3. If it is BLINKING and it is switching on for the third time, go to the ON state
  4. If '#' has been pressed, go to the OFF state.

Or maybe you could split the state BLINKING into BLINKING_ON and BLINKING_OFF.

The second LED would be essentially the same, except for lacking the ON state and rule number 3.

I am not writing the code, but you may want to take a look at this code for automatic door. It is an answer to a seemingly unrelated question, but it gives a good example on how to combine millis()-based timing with a finite state machine. Your program could be written mostly along the same lines.

How can I use millis() to blink forever?

The algorithm is essentially as follows:

if (it_is_time_to_toggle_the_led()) {
 toggle_the_led();
}

The details are thoroughly covered in the Blink Without Delay Arduino tutorial. It is essential to understand this technique: as soon as you have more than one thing to do in your program (here: blink an LED and monitor button presses), delay() is toxic because it blocks all the program. You should really ban delay() from your vocabulary, except for the simpler single-task programs.

Or just blink twice then stay on?

For this and your other questions, the canonical answer is "implement a finite state machine". I recommend the Finite State Machine tutorial by Majenko.

In this particular instance, I would probably implement two FSMs, one for each LED. The first one would have three states: (OFF, BLINKING, ON), and a counter for the number of blinks. The transition rules:

  1. If button 1 is pressed and the state is not BLINKING go to the BLINKING state
  2. If it is BLINKING and it's time for it to toggle, do it
  3. If it is BLINKING and it is switching on for the third time, go to the ON state
  4. If '#' has been pressed, go to the OFF state.

Or maybe you could split the state BLINKING into BLINKING_ON and BLINKING_OFF.

The second LED would be essentially the same, except for lacking the ON state and rule number 3.

I am not writing the code, but you may want to take a look at this code for automatic door. It is an answer to a seemingly unrelated question, but it gives a good example on how to combine millis()-based timing with a finite state machine. Your program could be written mostly along the same lines.

Source Link
Edgar Bonet
  • 45.1k
  • 4
  • 42
  • 81

How can I use millis() to blink forever?

The algorithm is essentially as follows:

if (it_is_time_to_toggle_the_led()) {
 toggle_the_led();
}

The details are thoroughly covered in the Blink Without Delay Arduino tutorial. It is essential to understand this technique: as soon as you have more than one thing to do in your program (here: blink an LED and monitor button presses), delay() is toxic because it blocks all the program. You should really ban delay() from your vocabulary, except for the simpler single-task programs.

Or just blink twice then stay on?

For this and your other questions, the canonical answer is "implement a finite state machine". I recommend the Finite State Machine tutorial by Majenko.

In this particular instance, I would probably implement two FSMs, one for each LED. The first one would have three states: (OFF, BLINKING, ON), and a counter for the number of blinks. The transition rules:

  1. If button 1 is pressed and the state is not BLINKING go to the BLINKING state
  2. If it is BLINKING and it's time for it to toggle, do it
  3. If it is BLINKING and it is switching on for the third time, go to the ON state
  4. If '#' has been pressed, go to the OFF state.

Or maybe you could split the state BLINKING into BLINKING_ON and BLINKING_OFF.

The second LED would be essentially the same, except for lacking the ON state and rule number 3.

I am not writing the code, but you may want to take a look at this code for automatic door. It is an answer to a seemingly unrelated question, but it gives a good example on how to combine millis()-based timing with a finite state machine. Your program could be written mostly along the same lines.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /