I'm using an Arduino Nano with a breadboard and a Gikfun tactile momentary push button with 10k resistor added. I was able to get the pushbutton to work with sample OneButton library examples and am trying to alter the sample code to work for my situation. I'm also using the FastLED library that currently works for my outdoor LED lights.
I want to cycle through LED pattern functions with a single click on the button. I eventually want to add more features to the program, but can't seem to crack my current problem.
I think I probably am having issues with my selection of ReturnTypes? Either way, I've tried to get it to Print the running function name, but it continues to Print "Off".
I must have done something wrong to post my code. I'm using the Web editor and thought it would share my code in a frame, but I failed. I added a link but for the future I'd like to post the code as others have, any ideas what I did wrong here?
The best result is when You start in the Arduino IDE by using the "Autoformat', or Ctrl + T. Then mark and copy the code.
In this windiw, the Quick Reply, there is a symbol up to the left. Click on that and then paste the code.
At the moment, I'm trying to get the Serial Monitor to cycle between Off, Christmas, Superbowl_Chiefs, Superbowl_Broncos, and Valentines changing on to the next with each button click. That should, then, select a different color pattern based on the like named function.
First, debounce the switch - there are myriad threads on this and an example in IDE-> file/examples/digital. Take the debounced switch state (boolean) and feed it to a bit of code which senses a change of state (example in same place in IDE). Take the state change, also boolean, and increment a counter. Reset the counter when it's past the last pattern. Use the counter value to a: select from an array; select one of 'n' in a switch/case construct.
The switch is debounced within OneButton and I've extended from 50 to 80 ms within the setup.
Pushing the button does not cycle the println from off to Christams to Superbowl_Chiefs to Superbowl_Broncos to Valentine to indicate which function is active. It continuously prints "off" indefinitely.
The myClickFunction function should be changing the serial monitor output above through the nextPattern value. It will not change. This is standard programming from the OneButton library samples that I've tried to change to work. It is not and I'm not sure why.
The lighting functions are working just fine on their own. My problem, once again, is that trying to set the button to cycle between these lighting functions does not seem to be working.
porosis:
The lighting functions are working just fine on their own. My problem, once again, is that trying to set the button to cycle between these lighting functions does not seem to be working.
Your code runs like a lunartic in the memory, as I see it.
That gives unpredictable execution disturbance. It's fully possible that the code You show ruins the stack, other variables like the button variable for example.
Correct the way Your code access the arrays. Until then, all other work is useless.
Okay, to bypass the lunatic run I've commented out the LED functions. Now it start (in the serial monitor) with alternating "off" and "Valentine", when I press the button it then prints "Valentine" and "Christmas", another press prints "Valentine" and "Superbowl_Chiefs". What in the code is making it insert "Valentine" in there before nextPattern is set to VALENTINE on the forth button press? On the forth press it prints "Valentine" and "Valentine"
Can You post a copy of Serial Monitor?
I don't understand what You tell about the printouts. Printing "this" and that. What do You mean by the "and"? I don't find how in the code.
Each printout is a .println, printing one text on one line, "Off", "CHRISTMAS", "SUPERBOWL_CHIEFS", "SUPERBOWL_BRONCOS" or "VALENTINE".
Would You need the action "Off" in the code below? I don't see how the definition of enums is connected to myClickFunction.
I tried commenting out the typedef enum lines and the compiler tells me that the Actions are not defined in the scope. Could I save a lot of the confusion by assigning int for these actions?
As far as the LED patterns; this is the only way I've found to run a repeating pattern to my LED string lights so far, any suggestions on how to limit the data confusion for these?