Go Down

Topic: trying to use buttons to switch between Led Strip functions  (Read 480 times) previous topic - next topic

sog3th

hello,

i'm new to Arduino. i'm trying to use three different buttons to change the animations of a led strip.

button 1  to fade in and out specific color
button 2  to change the led strip animation
button 3 to turn it off 

but can't seem to figure out to make this work

any help would greatly appreciated

tried to repurpose this sketch to fit my needs

Code: [Select]
#include <Adafruit_NeoPixel.h>
#define BLUE_BUTTON_PIN  2
#define RED_BUTTON_PIN   3
#define OFF_BUTTON_PIN   4
#define PIXEL_PIN    6
#define PIXEL_COUNT 10

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType = 0;

void setup() {
  pinMode(BLUE_BUTTON_PIN, INPUT_PULLUP);
  pinMode(RED_BUTTON_PIN, INPUT_PULLUP);
  pinMode(OFF_BUTTON_PIN, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

}

void Red() {
  // Get current button state.
  bool newState = digitalRead(RED_BUTTON_PIN);

  // Check if state changed from high to low (button press).
  if (newState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(RED_BUTTON_PIN);
    if (newState == LOW) {
      showType++;
      if (showType > 2)
        showType=0;
      startShow(showType);
    }
  }

  // Set the last button state to the old state.
  oldState = newState;
}


//
//void Blue() {
//  // Get current button state.
//  bool newState = digitalRead(BLUE_BUTTON_PIN);
//
//  // Check if state changed from high to low (button press).
//  if (newState == LOW && oldState == HIGH) {
//    // Short delay to debounce button.
//    delay(020);
//    // Check if button is still low after debounce.
//    newState = digitalRead(BLUE_BUTTON_PIN);
//    if (newState == LOW) {
//      showType++;
//      if (showType > 3)
//        showType=0;
//      startShow(showType);
//    }
//  }
//
//  // Set the last button state to the old state.
//  oldState = newState;
//}


void startShow(int i) {
  switch(i){
    case 0: colorWipe(strip.Color(0, 0, 0), 0);    // Black/off
            break;
    case 1: colorWipe(strip.Color(255, 0, 0), 0);  // Red
            break;
    case 2: colorWipe(strip.Color(0, 0, 255), 0);  // Green
            break;
//    case 3: colorWipe(strip.Color(0, 0, 255), 0);  // Blue
//            break;
//    case 4: theaterChase(strip.Color(127, 127, 127), 50); // White
//            break;
//    case 5: theaterChase(strip.Color(127,   0,   0), 50); // Red
//            break;
//    case 6: theaterChase(strip.Color(  0,   0, 127), 50); // Blue
//            break;
//    case 7: rainbow(20);
//            break;
//    case 8: rainbowCycle(20);
//            break;
//    case 9: theaterChaseRainbow(50);
//            break;
  }
}


void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

PaulRB

#1
Feb 22, 2018, 08:13 am Last Edit: Feb 22, 2018, 08:20 am by PaulRB
This is probably the most common question asked on the forum about led strips. Search the forum and you will find a dozen examples. The advice is the same each time.

Please read the forum guidelines in the sticky post about how to post code and ask for help properly.

sog3th

Thank you. i did serch for it but didn't see what i was looking for i will look again.

PaulRB

Look for posts talking about "blocking code", criticising the use of delay() and referring to the "blink without delay"example sketch. Those are the ones.

Qdeathstar

Sog3th,

If you google and you still need help, i can help.

What you should do first is create a sketch that reads one button press without using delay. You need to create a state machine.

You can use the serial monitor to aid in debugging.
A creaking creeping shadow
stiff against the freezing fog
glares at a tickless watch.

Time has failed him -- all things shall pass.

Go Up