0
\$\begingroup\$

I have been working on an Arduino project: 4 LEDs connected on a breadboard, with series-parallel circuits, that are being controlled by push button. The desired behavior is to start in OFF mode, and upon button press to switch to each of the remaining 7 modes one after another, and then to loop around back to Mode 0 (OFF) after completing a full 8 mode loop.

There are 8 modes, including OFF, and I am using a 3-lead Keyes push button to switch through them, using switch statements and cases for each of the modes. The push button has a 10 kΩ resistor located between the middle and left (S) pin. I decided to use a pull-down resistor, and am using LOW as the initial buttonState variable.

5V is connected to the -, with S connected to pin 2 for control, and ground connected to the middle lead.

Here are my sources for that setup for the Keyes push-button:

https://startingelectronics.org/tutorials/arduino/modules/push-button/

https://startingelectronics.org/pinout/push-button/

Here is what happens when I upload the code to my Arduino:

  1. It starts OFF as it's meant to
  2. Push button, it switches to Mode 1, displays "Switching to mode 1"
  3. Push button, it switches to Mode 2, displays "Switching to mode 2"
  4. Push button, nothing happens, nothing is displayed, the program will not move forward

I have tried tons of different debug possibilities. The debounce logic you see commented out, I have tried both with and without and altered the debounce time, nothing changed.

I only saw a change once, and this is where it gets weird - if I put a print statement "Switching to Mode 3" right in the beginning of Mode 3's method, on the serial output I see "Switching to Mode 3", it switches to Mode 3, but it keeps displaying the text "Switching to Mode 3" over and over, and won't switch to Mode 4! Lastly, I have tested each mode separately and they all work fine.

Any ideas are welcome, I'm really stumped! Photos included, code found @ https://github.com/sw13tch-dio/Arduino_LEDproject

breadboard setup

Arduino setup

whole project view

\$\endgroup\$
12
  • \$\begingroup\$ Just to be sure, do you simply click the button, and nothing happens? Or did you keep it pushed for some time, how long? \$\endgroup\$
    – Justme
    Commented Feb 6 at 8:47
  • \$\begingroup\$ What are the values of the resistors in serie with the diode ? I don't see well. \$\endgroup\$
    – Antonio51
    Commented Feb 6 at 9:30
  • \$\begingroup\$ Note that the program on GitHub ... seems "incomplete" ... \$\endgroup\$
    – Antonio51
    Commented Feb 6 at 9:48
  • 1
    \$\begingroup\$ @DioB. See my answer. You need to press it at least up to 2 seconds because the MCU reads button once every 2 seconds! \$\endgroup\$
    – Justme
    Commented Feb 6 at 16:17
  • 1
    \$\begingroup\$ put three ``` on a blank line before the code and slso on a blank line after the code ... that will format it correctly \$\endgroup\$
    – jsotola
    Commented Feb 7 at 5:24

1 Answer 1

1
\$\begingroup\$

I don't think there is nothing wrong in the code.

Maybe you just expect it to work somehow differently and it does not work how you expect, and how you expect it to work should have been mentioned in the question.

Do realize that when you entered mode 2, you are spending there two seconds at a time each call, toggling LEDs and delaying, before returning so that the main loop code is again run and it can read the button.

So you need to keep the button pushed for until the code reads the button state, which can take up to 2 seconds.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.