4 Buttons - How to change activity based on which one is pressed

Greetings - only asking because time critical - built a great costume for the little guy but decided to give him 4 buttons to change the LED patterns. My question is what is the best (or any!) way to run a loop and wait for button inputs to change to the next one. I made it 4 buttons so that it looked "cooler" and each one is correctly attached to a pin waiting to be read.

I just don't know the best method for running the LED loops but still waiting for a button change to jump out and into another function. WHILE? BREAK? ahhhh Im such a perpetual noob!!

I wrote 3 simple LED function, one for each button. The fourth I want to control the LCD to change the message.

Thanks!!

Keyword is state machine (OK, key words..).

Check the buttons and set a value corresponding to the button pressed.
Also check to see what button was last pressed, so you can determine a change of pressed button.
You can now use that value to do whatever you want those LEDs to do.

LoopBack127:
I wrote 3 simple LED function, one for each button. The fourth I want to control the LCD to change the message.

Best to post your code. We can't improve code we can't see.

Please use code tags.

Read this before posting a programming question

How to use this forum

Thanks!! I actually was only looking for the correct path to START to head down (use x, y or z) I had not written anything because I didn't even know where to start. Turns out what I wanted to do is probably to complex at this point to be done for tomorrow.

I was looking to run 3 functions (button 1 would run function 1 in a loop until another button was pressed) - Button 2 for function 2 ... Button 4 was just going to change a LCD Message (got that one I think!)

Function 1) Fade 3 LED's at the same time but at different rates for lighting effect
(I now understand there are issues with that since the normal method uses delays which block button detection)

Function 2) 3 LED's chaser 1 -> 2 -> 3 -> 2 -> 1 -> 2 -> 3 etc... I can easily figure it out with a basic loop but again the delay function would have messed up my ability to look for another button press

Function 3) 3 Random blinking lights at same time - again I saw the issue where you can only blink one after the other. I need to research and understand the millis function better...

So I gave up FOR TONIGHT- I will re-address this next week for a personal challenge!

I have another programming question for which I added as a new topic - going to be a long night :slight_smile:

Design each function to be called repeatedly i.e. so that the looping is done outside the function rather than inside it.
Associate a number with each function.
Define a global variable holding the number of the currently-selected function.
In loop, use a switch statement to execute the function associated with the global variable value.
In loop, test for any of the 'function select' buttons having been pressed and if so change the global variable to the corresponding value.

You'll want to look into the the concepts of finite state machines and blocking vs non-blocking.