switching between neopixel loops with button

Hi guys,

I’m having a bit of a hard time trying to figure some of this stuff out, pretty new to programming and arduinos… anyway I’d like to be able to switch between a couple different ‘animations’ (basically just switching between different colors fading in and out or ‘breathing’) using a strip of neopixels.

I’m using a modification of the ‘buttoncycler’ example, but I can only get it to either switch between the solid colors without the looping animation - or do the animation with one color that I want, but not react to the button signals when pressed.

Here is where my code is at, right now it’s not detecting the button input, just looping the first animation… any help would be greatly appreciated! thanks!

switchtbreathe.ino (1.31 KB)

  strip.show(); // Initialize all pixels to 'off'

That is NOT what that code does. It is the absence of code to set pixels to any state but off that makes the show() method appear to turn them all off.

You can do what you want, but you will have to COMPLETELY re-write your code, to have NO for loops and NO delay() calls.

A state machine is what you need.

Ok, so does that line of code have something to do with my problem? that was just borrowed from the buttoncycler example. I apologize for being dense, but is there anyway you could be more specific to help a newb out? I'm not really sure how to go about this.. thanks for the response!

I’m not really sure how to go about this… thanks for the response!

I gave you a clue. Google Arduino + state machine.

You can NOT use delay() anywhere/anytime if you want to respond quickly to switch presses.

Since you can’t use delay(), and loop() already loops, it doesn’t make sense to use any for loops, while loops, or do/while loops.

You have a very common problem that many noobies fall into. You used example code to try to create something real. The point of the example code is NOT to give you pre-packaged examples. It is to show you how to do various things. In the context of larger program it all falls apart because it uses blocking code.

The key words you are looking at are blocking code and non-blocking code. Right now your code blocks. It uses delays and for loops and doesn't let control return until the entire animation is finished. You need non-blocking code if you want it to be able to do something else. That means on each mass of loop you check to see if something needs to be done at that exact moment and you do just that one small step and return. Think more like a checklist picking the immediate action and not like a function that tells a story from beginning to end.

These is a common topic and if you look around you should find plenty of resources to help you learn how to think about this. It isn't something that someone should have to rewrite here just special for you.

Look here: http://forum.arduino.cc/index.php?topic=384198.0 and read all the topics under General Design. That should get you going.

thanks for the replies guys! I will do some looking around in the areas you suggested and try to wrap my head around this

Just remember, if you can rad a clock and follow a checklist then you can already do this. It's not hard. You just have to stop imagining it being hard and see it for what it really is.

If i said look at the clock and you did and it said it was 1:20pm and I said to meet me at the pub in 20 minutes. Could you do it? Would you be able to figure out what to do there? Congratulations, you have everything you need to understand blink without delay.

@Delta_G I really appreciate it