Skip to main content
Arduino

Return to Answer

Added Wokwi simulation with LED protection diodes.
Source Link
tim
  • 699
  • 6
  • 15

Wokwi simulates button bounce by default to more accurately model the real world, so it's advisable to have some form of button debouncing.

Wokwi simulates button bounce by default to more accurately model the real world, so it's advisable to have some form of button debouncing.

Added Wokwi simulation with LED protection diodes.
Source Link
tim
  • 699
  • 6
  • 15

Here's the Wokwi simulation of the lightshow with 250 Ω protection resistors for the LEDs.

Here's the Wokwi simulation of the lightshow with 250 Ω protection resistors for the LEDs.

Added `static` to `bool doLightshow` at the beginning of `loop()`.
Source Link
tim
  • 699
  • 6
  • 15
void loop ()
{
 static bool doLightshow = false;
 static Debouncer buttonOn(buttonON);
 static Debouncer buttonOff(buttonOFF);
 buttonOn.Update();
 buttonOff.Update();
 if (buttonOn.Fall())
 {
 Serial.println("buttonOn falling edge.");
 doLightshow = true;
 }
 else if (buttonOff.Fall())
 {
 Serial.println("buttonOff falling edge.");
 doLightshow = false;
 }
 if (doLightshow)
 {
 // Do lightshow.
 . . .
 }
 else
 {
 digitalWrite(LED1, LOW);
 digitalWrite(LED2, LOW);
 digitalWrite(LED3, LOW);
 digitalWrite(LED4, LOW);
 digitalWrite(LED5, LOW);
 }
}
void loop ()
{
 bool doLightshow = false;
 static Debouncer buttonOn(buttonON);
 static Debouncer buttonOff(buttonOFF);
 buttonOn.Update();
 buttonOff.Update();
 if (buttonOn.Fall())
 {
 Serial.println("buttonOn falling edge.");
 doLightshow = true;
 }
 else if (buttonOff.Fall())
 {
 Serial.println("buttonOff falling edge.");
 doLightshow = false;
 }
 if (doLightshow)
 {
 // Do lightshow.
 . . .
 }
 else
 {
 digitalWrite(LED1, LOW);
 digitalWrite(LED2, LOW);
 digitalWrite(LED3, LOW);
 digitalWrite(LED4, LOW);
 digitalWrite(LED5, LOW);
 }
}
void loop ()
{
 static bool doLightshow = false;
 static Debouncer buttonOn(buttonON);
 static Debouncer buttonOff(buttonOFF);
 buttonOn.Update();
 buttonOff.Update();
 if (buttonOn.Fall())
 {
 Serial.println("buttonOn falling edge.");
 doLightshow = true;
 }
 else if (buttonOff.Fall())
 {
 Serial.println("buttonOff falling edge.");
 doLightshow = false;
 }
 if (doLightshow)
 {
 // Do lightshow.
 . . .
 }
 else
 {
 digitalWrite(LED1, LOW);
 digitalWrite(LED2, LOW);
 digitalWrite(LED3, LOW);
 digitalWrite(LED4, LOW);
 digitalWrite(LED5, LOW);
 }
}
added 19 characters in body
Source Link
tim
  • 699
  • 6
  • 15
Loading
Source Link
tim
  • 699
  • 6
  • 15
Loading
lang-cpp

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