Control Neopixel strip from keypad?
Control Neopixel strip from keypad?
Am currently shoehorning 2 small programs together, one is a series of loops controlling a neopixel strip with different types of patterns and colours, the other is a basic 4 x 4 matrix keypad controller. I want to be able to select which patterns and which colours via a press of the keypad buttons, but am not sure how to interrupt the neopixel loops to check if/when a key is pressed and redirect the neopixels to change colour or pattern?
- Grumpy_Mike
- Posts: 8
- Joined: Sun Feb 14, 2021 8:04 pm
Re: Control Neopixel strip from keypad?
We get this question almost every day on the Arduino forum. What you need to do is to rewrite your code that does the patterns in a way known as a state machine. This is so the code can look at the change button every time the pattern makes just one small step.
This means you have to :-
1) remove all the use of delays unless they are very short, say less than 10mS
2) unroll all the for loops. That is remove the for loop and have it implemented "by hand". So set up a loop variable which is global, and on each step of the loop increment it to see if the loop is finished and if not do the one step of your pattern and then return.
At the top level of your code you have a simple infinite loop using a while statement. This simply looks at your change button to see if it has become pressed since the last time you looked at it and if it has increment a counter that will tell the rest of the code what pattern to do. Then the next thing in the while loop is to look at the timer and see if enough time has passes such that you want to do the next step in your pattern, if not go round the while loop again, but if it has then look at the pattern number with a sequence of if statements and call the function that provides the next bit of the pattern.
Sorry this is complex but there you are a seemingly simple change can cause havoc with what was so far a simple piece of code.
For an example in Python see the Neopixel_Bounce.py code in my Magnetic Bounce article in the Mag Pi #69, although that does only one pattern.
Seeing this is Python and not C then there are other ways of doing this by using threads.
For an example of this see my article in the Mag Pi 87 & 88 on the Gravitrax marble race system.
This means you have to :-
1) remove all the use of delays unless they are very short, say less than 10mS
2) unroll all the for loops. That is remove the for loop and have it implemented "by hand". So set up a loop variable which is global, and on each step of the loop increment it to see if the loop is finished and if not do the one step of your pattern and then return.
At the top level of your code you have a simple infinite loop using a while statement. This simply looks at your change button to see if it has become pressed since the last time you looked at it and if it has increment a counter that will tell the rest of the code what pattern to do. Then the next thing in the while loop is to look at the timer and see if enough time has passes such that you want to do the next step in your pattern, if not go round the while loop again, but if it has then look at the pattern number with a sequence of if statements and call the function that provides the next bit of the pattern.
Sorry this is complex but there you are a seemingly simple change can cause havoc with what was so far a simple piece of code.
For an example in Python see the Neopixel_Bounce.py code in my Magnetic Bounce article in the Mag Pi #69, although that does only one pattern.
Seeing this is Python and not C then there are other ways of doing this by using threads.
For an example of this see my article in the Mag Pi 87 & 88 on the Gravitrax marble race system.