Right now I’m trying to create a vertical lamp, square, with 4 columns of 10 lights on each colum going around. It’s going to be a Christmas present, so I hope I can figure this out soon!
The plan is to use the Noise code and have a bunch of different palettes to choose from. And rather than a delay for each palette, I want a push-button control. But I’m having a hell of a time getting the buttons to work.
Here is the simple keypad I ordered and have plugged in:
Here is a copy of my code:
Here is what’s happening:
Right now, it appears that it’s just blasting through the loop between On (all LEDs White) and Off (all LEDs Black). When I press K1 (labeled “lampon”) the lights all turn white – so long as I’m HOLDING the button. If I press K2 (labeled “lampoff”) the lights all turn black – so long as I’m HOLDING the button. Otherwise it just blinks back and forth so fast I feel like I’m going to have a seizure.
So what am I missing here? What I want is to be able to click the button ONCE (and let go of it), and it stay at that option indefinitely – until I press another button. Eventually I will add in buttons K3 and K4 as “lampup” and “lampdown”, to scroll through the palette color choices, which I think I already have figured out using the Switch/Case coding. I can share the other code later, but for now I really just want the simple button pressing to work.
What am I missing? I’ve also tried a few Arduino Libraries I’ve found that are designed for this sort of thing. Unfortunately I haven’t gotten any of them to work.
Is there a simple code I don’t know to add in for this to work?
Also, is there a Button Library I maybe haven’t tried yet that works really well? Any recommendations?
The first thing you need understand is that the Do…While loop is going to perform the contents of the loop once before checking the condition – that’s why the While comes at the end. If you were to use the While statement alone (which starts the loop) then the code won’t execute until the condition is met.
The other thing is that I don’t see any reason for using a loop at all – a simple If condition for each button will suffice. Also, don’t continuously read the button state.
When it comes time to code the cycle up/down through the patterns, you’re going to need some sort of debounce code that waits a second or so between “valid” activations, or you will just cycle through them at lightning speed. The Arduino can process the main loop a lot faster than you can press and release the button once.
Thanks! I just tried putting that loop code inside the Setup code and closing the loop with nothing in it. Now nothing seems to work. No lights, buttons don’t do anything.
I’m going to try and figure out the while statement a bit better. Some of of the online resources were very confusing.
I’ll be completely honest, I really don’t know much about C Programming. I just “get” computers and software (always have) and math is easy to me. So, that being said, there’s probably lots of simple little understandings about this coding I really don’t get. Like not needing the void loop function, for example.
I’ve also looked at some debounce code and libraries. I’m also still trying to find a good button library so I can use “IsPushed” so that the button state isn’t continuously read and I don’t have to hold it in – I just want a click. Unfortunately either the libraries created an error within the header itself while compiling, or the IsPushed code wouldn’t be read. Tricky business this button thing…
Sorry, I was unclear. When I referred to “loop” I was talking about your Do…While loops, and the use of a simple While loop.
The only code that should reside in the Setup function is one-time operations that establish the working environment: defining the leds array, initializing FastLED, setting initial values for global variables. Anything that is to be checked iteratively should be in the Loop function, or a function that gets called from Loop.
By the way, “void” simply defines the function as not returning a value, it’s not part of the name of the function.
Are you familiar with the official Arduino support site (http://arduino.cc/)? If not, check out the “learning” and “forum” sections.
Thanks for clarifying, I understand now. I have looked at the learning sections, where I got most of my libraries from to get the buttons to work. It’s mostly been google searches that led me there though – I’ll do more browsing when I have time.
I still never did get the damn buttons to work, so I just used the timer function that came with Mark’s Noise demo, and added in my own custom palettes. For the lamp, it’s actually kind of nice to be able to just let it sit there and cycle through a new color spectrum every few minutes. But for some projects I have planned for customizing my home, I’ll definitely want those buttons to work, but I have all the time in the world to figure it out now. For fun, here’s my code, as well as a demo video.