Quick question on the technical side for anybody interested in helping with code
So with my current setup, I have the Arduino hooked up to the lights, and a push buttons as an input.
Apart from the setup and loop, I have three other functions. The first function rainbow() makes the strip of lights scroll through rainbow colors. The second function random() makes a random light turn on for .5 seconds, turn off, and then repeats itself. The final function is button() and uses a variation of this code https://www.arduino.cc/en/Tutorial/StateChangeDetection to work. As of now, this function only returns a value of true or false, where true lights up the whole strip and false turns it off.
I want to accomplish something simple, but I am not sure how to use nested functions to get this to work, as I have little experience with arduino and the C++ language.
What I want to do is have the button() function be a master command of sorts, where I can make the arduino switch between rainbow() or random() at the push of the button without delay. How would I go about using the button to select which function to run, where 1 push = rainbow(), 2 pushes = random(), n pushes = //functionname(), and after the last push the counter resets itself.
Something like this (not the real code obviously)
loop()
{
button()
{
if button == 1
rainbow()
if button == 2
random()
if button == etc…
functionname()
if button == total # of functions
button = 1
}
}
if you need some proper code, Ill see what I can do, but the basis would be something like so:
When the button is pressed the state of the button goes from being unpressed(LOW) to pressed(HIGH) to unpressed again(LOW).
You know every time the button enters a high state the button has been pressed. Therefore, you can make a counter which counts the number of times the button enters a high state, with the counter increasing by one every press.
You then link the main body of your code to what happens when it is pressed ‘x’ number of times.
If you have, lets say 7 things that you want it to rotate between, after the counter gets to seven you say the next time it is pressed return to 0, and keep on doing that.
In this example I am just turning on and off, but you can follow what I am doing with the button
@Tejkaran_Samra has given you great advice! But if you are not dead set on a button, you might also consider using a potentiometer for input instead.
The logic is straightforward–first divide the analog input range (1024) by the number of functions you have in order to come up with a series of “slots” (number ranges), then add some straight ‘if’ statements to see which slot the analog input is in, and finally call the appropriate function–it also has the advantage (?) of saving “state” after a power cycle.
Theoretically, you could map just over a thousand functions to a single potentiometer, but between noise and twitchy fingers, the practical limit is much lower. Still, 7-36 slots would be quite easily manageable.
I like to use rotary encoders, then you can click them like a button, turn them when pressed, turn them without pressing them etc. Loads of flexibility with just one knob