Neopixel buttoncyceler with 5 buttons

Hi, l’m a total new at this. Iam trying to make an toy with LEDs and 5 arcade buttons for my kid.

l got an RGB-shield with 40 LEDs, that l want different buttons to turn on different colours.

here is the code so far

#include <Adafruit_NeoPixel.h>

// Declare and initialise variable for Neopixel brightness
byte neoBright = 5;

#define BUTTON_PIN1   8    

#define BUTTON_PIN2   11
 
#define BUTTON_PIN3   12

#define BUTTON_PIN4   13

#define PIXEL_PIN    10    

#define PIXEL_COUNT 40



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

bool oldState = HIGH;
int showType = 0;

void setup() {
  pinMode(BUTTON_PIN1, INPUT_PULLUP);
  pinMode(BUTTON_PIN2, INPUT_PULLUP);
  pinMode(BUTTON_PIN3, INPUT_PULLUP);
  pinMode(BUTTON_PIN4, INPUT_PULLUP);
  strip.begin();
  strip.setBrightness(neoBright); 
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Get current button state.
  bool newState1 = digitalRead(BUTTON_PIN1);
  bool newState2 = digitalRead(BUTTON_PIN2);
  bool newState3 = digitalRead(BUTTON_PIN3);
  bool newState4 = digitalRead(BUTTON_PIN4);


  // Check if state changed from high to low (button press).
  if (newState1 == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
  }
  if (newState2 == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
  }
  if (newState3 == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
  }
  if (newState4 == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
  }
    // Check if button is still low after debounce.
    newState1 = digitalRead(BUTTON_PIN1);
    newState2 = digitalRead(BUTTON_PIN2);
    newState3 = digitalRead(BUTTON_PIN3);
    newState4 = digitalRead(BUTTON_PIN4);
    
    if (newState1 == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
      if (newState2 == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
    if (newState3 == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
      if (newState4 == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
  }

  // Set the last button state to the old state.
  oldState = newState1;
  oldState = newState2;
  oldState = newState3;
  oldState = newState4;
}  
 void startShow(int i) {
  switch(i){
    case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
            break;
    case 1: colorWipe(strip.Color(255, 0, 0), 50);  // Red
            break;
    case 2: colorWipe(strip.Color(0, 255, 0), 50);  // Green
            break;
    case 3: colorWipe(strip.Color(0, 0, 255), 50);  // 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;
  }
}

// Fill the dots one after the other with a color
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);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
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);
}

.
I get startShow is not decleared. And l have no clue how to fix it

What is this doing all by itself :wink:

// Set the last button state to the old state.
oldState = newState1;
oldState = newState2;
oldState = newState3;
oldState = newState4;
}

other question, how can l make red/blue/yellow/green and white button turn on same colour in the LEDS?

Read the Switches, when the RED switch is pressed set the color to RED etc.

Red = FF0000
Blue = 0000FF
Green = 00FF00
Yellow = F7FF00
White = F7F7F7

FYI
Helpful tool: Color Picker

.

Tryed to fix my code but still get same error. quess its tolate to focusXD

quess its tolate to focusXD

Say what :o

Koraleck:
Tryed to fix my code but still get same error.

Then post your latest code and tells us how the results differ from what you expect.

#include <Adafruit_NeoPixel.h>

// Declare and initialise variable for Neopixel brightness
byte neoBright = 5;

#define BUTTON_PIN1   8    

#define BUTTON_PIN2   11
 
#define BUTTON_PIN3   12

#define BUTTON_PIN4   13

#define PIXEL_PIN    10    

#define PIXEL_COUNT 40



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

bool oldState = HIGH;
int showType = 0;

void setup() {
  pinMode(BUTTON_PIN1, INPUT_PULLUP);
  pinMode(BUTTON_PIN2, INPUT_PULLUP);
  pinMode(BUTTON_PIN3, INPUT_PULLUP);
  pinMode(BUTTON_PIN4, INPUT_PULLUP);
  strip.begin();
  strip.setBrightness(neoBright); 
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Get current button state.
  bool newState1 = digitalRead(BUTTON_PIN1);
  bool newState2 = digitalRead(BUTTON_PIN2);
  bool newState3 = digitalRead(BUTTON_PIN3);
  bool newState4 = digitalRead(BUTTON_PIN4);

  if (newState1 == LOW && oldState == HIGH) {
    delay(20);
    newState1 = digitalRead(BUTTON_PIN1);
  if (newState1 == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
  }
     // Set the last button state to the old state.
  oldState = newState1;
}

  if (newState2 == LOW && oldState == HIGH) {
    delay(20);
    newState2 = digitalRead(BUTTON_PIN2);
  if (newState2 == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
  }
     // Set the last button state to the old state.
  oldState = newState2;
}

  if (newState3 == LOW && oldState == HIGH) {
    delay(20);
    newState3 = digitalRead(BUTTON_PIN3);
  if (newState3 == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
  }
    // Set the last button state to the old state.
  oldState = newState1;
}   
  if (newState4 == LOW && oldState == HIGH) {
    delay(20);
    newState4 = digitalRead(BUTTON_PIN4);
  if (newState4 == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
  }
  // Set the last button state to the old state.
  oldState = newState4;
}
 void startShow(int i) {
  switch(i){
    case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
            break;
    case 1: colorWipe(strip.Color(255, 0, 0), 50);  // Red
            break;
    case 2: colorWipe(strip.Color(0, 255, 0), 50);  // Green
            break;
    case 3: colorWipe(strip.Color(0, 0, 255), 50);  // 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;
  }
}

// Fill the dots one after the other with a color
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);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
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);

still get the same result as last time

Koraleck:
still get the same result as last time

sometimes the IDE screws up creating the function prototypes.

place this line:

 void startShow(int i);

at the top of your code under:

#include <Adafruit_NeoPixel.h>

Here is my new code, geting an new problem now.

exit status 1
a function-definition is not allowed here before ‘{’ token

At the
void startShow(int i) above the switchcase part of my code. what are lam missing now:S

#include <Adafruit_NeoPixel.h>

void startShow(int i);

// Declare and initialise variable for Neopixel brightness
byte neoBright = 5;

#define BUTTON_PIN1   2    

#define BUTTON_PIN2   3
 
#define BUTTON_PIN3   4

#define BUTTON_PIN4   5

#define BUTTON_PIN5   6

#define PIXEL_PIN    10    

#define PIXEL_COUNT 40



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

bool oldState = HIGH;
int showType = 0;

void setup() {
  pinMode(BUTTON_PIN1, INPUT_PULLUP);
  pinMode(BUTTON_PIN2, INPUT_PULLUP);
  pinMode(BUTTON_PIN3, INPUT_PULLUP);
  pinMode(BUTTON_PIN4, INPUT_PULLUP);
  pinMode(BUTTON_PIN5, INPUT_PULLUP);
  strip.begin();
  strip.setBrightness(neoBright); 
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Get current button state.
  bool newState1 = digitalRead(BUTTON_PIN1);
  bool newState2 = digitalRead(BUTTON_PIN2);
  bool newState3 = digitalRead(BUTTON_PIN3);
  bool newState4 = digitalRead(BUTTON_PIN4);
  bool newState5 = digitalRead(BUTTON_PIN5);

  if (newState1 == LOW && oldState == HIGH) {
    delay(20);
    newState1 = digitalRead(BUTTON_PIN1);
  if (newState1 == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
  }
     // Set the last button state to the old state.
  oldState = newState1;


  if (newState2 == LOW && oldState == HIGH) {
    delay(20);
    newState2 = digitalRead(BUTTON_PIN2);
  if (newState2 == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
  }
     // Set the last button state to the old state.
  oldState = newState2;


  if (newState3 == LOW && oldState == HIGH) {
    delay(20);
    newState3 = digitalRead(BUTTON_PIN3);
  if (newState3 == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
  }
    // Set the last button state to the old state.
  oldState = newState1;
   
  if (newState4 == LOW && oldState == HIGH) {
    delay(20);
    newState4 = digitalRead(BUTTON_PIN4);
  if (newState4 == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
  }
  // Set the last button state to the old state.
  oldState = newState4;

  if (newState5 == LOW && oldState == HIGH) {
    delay(20);
    newState5 = digitalRead(BUTTON_PIN5);
  if (newState5 == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
  }
  // Set the last button state to the old state.
  oldState = newState5;

 void startShow(int i) 
  switch(i){
    case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
            break;
    case 1: colorWipe(strip.Color(255, 0, 0), 50);  // Red
            break;
    case 2: colorWipe(strip.Color(0, 255, 0), 50);  // Green
            break;
    case 3: colorWipe(strip.Color(0, 0, 255), 50);  // 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;
  }
}

// Fill the dots one after the other with a color
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);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
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);
}