0

I was working on a project with an LED strip, where I'd like to cycle through various effects with just a press of a button. The problem is that I don't know how to detect the press of the button to change effect while in for() cycling colors to have a rainbow effect (so it's a multitasking problem, I think). Could you help me? :)

#include <Adafruit_NeoPixel.h>
#define pin 5
#define led 15
#define button 7
Adafruit_NeoPixel strip = Adafruit_NeoPixel (led,pin,NEO_RGB+NEO_KHZ800);
int doggo=0;
void setup(){
 strip.begin();
 pinMode(button,INPUT);
}
void loop(){
 int buttonVal=digitalRead(button);
 if(buttonVal==HIGH){
 doggo++;
 delay(300);
 }
 if (doggo>2){
 doggo=0;
 }
 while (doggo==0){
 //whole strip red
 for (int i=0;i<led;i++){
 strip.setPixelColor(i,255,0,0);
 strip.show();
 }
 }
 while (doggo==1){
 //rainbow effect
 uint16_t i, j;
 int rainbowSpeed=10;
 for(j=0; j<256*5; j++) { 
 for(i=0; i< strip.numPixels(); i++) {
 strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
 strip.show();
 }
 }
 delay(rainbowSpeed);
 }
 while (doggo==2){
 //whole strip blue
 for (int i=0;i<led;i++){
 strip.setPixelColor(i,0,0,255);
 strip.show();
 }
 }
}
uint32_t Wheel(byte WheelPos) {
 WheelPos = 255 - WheelPos;
 if(WheelPos < 85) {
 return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
 }
 if(WheelPos < 170) {
 WheelPos -= 85;
 return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
 }
 WheelPos -= 170;
 return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
asked Apr 12, 2018 at 19:37

3 Answers 3

1

You could use the Bounce2 library.

Call the update() method regularly and then check the state with read().

e.g.

for (...) {
 button.update();
 if (button.read() == HIGH) {
 //change pattern
 break;
 }
 // Do strip stuff.
}

As an added bonus the bounce library will debounce your button.

answered Apr 12, 2018 at 20:25
0

You should use a hardware interrupt. Depending on your microcontroller, you have different pins available.

Here is a reference for more details and syntax

answered Apr 12, 2018 at 19:39
1
  • Thanks, I correctly implemented the hardware interrupt since it works with single colors (there is no digitalRead in loop, just a attachInterrupt() in setup). Still, it doesn't work with the rainbow effect :( Commented Apr 12, 2018 at 20:35
0

I finally solved!

I tried the Bounce2 library, but it acted really strange (the effects passed from the first one, then the third one, then the first one again for a fraction of second and automatically to the second one, plus other things :/ )

It looks like I just needed to add a normal digitalRead inside for() and a return, I don't usually make those errors xD

answered Apr 12, 2018 at 21:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.