How to add push buttons to this code?

Hello,
I am confused with the code I am making. I am new to Arduino and am trying to have different states either with a push button or potentiometer while using neo pixels?

Hope you can help.

#include <Adafruit_NeoPixel.h>

//define NeoPixel Pin and Number of LEDs
#define PIN 2
#define NUM_LEDS 23

//create a NeoPixel strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);


void setup() {
 
  // start the strip and blank it out
  strip.begin();
  strip.show();
}
// my different states I want 
//( blue, white, yellow) 
//( grey blue ) 
// (green, blue, grey) 
// ( green yellow, green) 
// (blue, white) 

void loop() {

  //SUNNY
  // set pixel to red, delay(1000)
  strip.setPixelColor(0, 52, 183, 235);
  strip.setPixelColor(1, 52, 183, 235);
  strip.setPixelColor(2, 52, 183, 235);
  strip.setPixelColor(3, 52, 183, 235);
  strip.setPixelColor(4, 127, 127, 127);
  strip.setPixelColor(5, 127, 127, 127);
  strip.setPixelColor(6, 127, 127, 127);
  strip.setPixelColor(7, 127, 127, 127);
  strip.setPixelColor(8,252, 244, 8);
  strip.setPixelColor(9, 252, 244, 8);
  strip.setPixelColor(10, 252, 244, 8);
  strip.setPixelColor(11, 252, 244, 8);
  strip.show();
  delay(3000);
  // set pixel to off, delay(1000)
  strip.setPixelColor(0, 0, 0, 0);
  strip.setPixelColor(1, 0, 0, 0);
  strip.setPixelColor(2, 0, 0, 0);
  strip.setPixelColor(3, 0, 0, 0);
  strip.setPixelColor(4, 0, 0, 0);
  strip.setPixelColor(5, 0, 0, 0);
  strip.setPixelColor(6, 0, 0, 0);
  strip.setPixelColor(7, 0, 0, 0);
  strip.setPixelColor(8, 0, 0, 0);
  strip.setPixelColor(9, 0, 0, 0);
  strip.setPixelColor(10, 0, 0, 0);
  strip.setPixelColor(11, 0, 0, 0);
  strip.show();
  delay(1000);
  

}

You can’t reliably power the LED strip from the Arduino.

It needs more current than the onboard regulator can provide,

Looks like power comes from USB plug....

Hello
What would you like to do by using either a button or potentiometer?

I calculate that is good for about eight WS2812/ NeoPixels.

I had a feeling that OP is overloading things.

Using the button or potentiometer I would like to change the state of the leds to change colours. Either one I dont mind using just wondering what could be the easiest and how I would do it

Im working with Neopixel strip light in Tindercad to see how my project is going to work. Im trying to use the Adafruit library to be able to control these leds with different states such as the first state would be the colour combination of: ( blue, white, yellow) then, ( grey blue , then (green, blue, grey) , then ( green yellow, green) and then windy (blue, white). I just want to be able to do this which a push of a push button. Here is my code but how do I incorporate a button system to do these change of state?

#include <Adafruit_NeoPixel.h>

//define NeoPixel Pin and Number of LEDs
#define NEO 2
#define NUM_LEDS 12
#define button 8

//create a NeoPixel strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, 2, NEO_GRB + NEO_KHZ800);


void setup() {
  
  // start the strip and blank it out
  strip.begin();
  strip.show();
}


//sunny ( blue, white, yellow) 
//cloudy ( grey blue ) 
//rainy (green, blue, grey) 
//thundery ( green yellow, green) 
//windy (blue, white) 

void loop() {
  
  //SUNNY
  
  strip.setPixelColor(0, 52, 183, 235);
  strip.setPixelColor(1, 52, 183, 235);
  strip.setPixelColor(2, 52, 183, 235);
  strip.setPixelColor(3, 52, 183, 235);
  strip.setPixelColor(4, 127, 127, 127);
  strip.setPixelColor(5, 127, 127, 127);
  strip.setPixelColor(6, 127, 127, 127);
  strip.setPixelColor(7, 127, 127, 127);
  strip.setPixelColor(8,252, 244, 8);
  strip.setPixelColor(9, 252, 244, 8);
  strip.setPixelColor(10, 252, 244, 8);
  strip.setPixelColor(11, 252, 244, 8);
  strip.show();
  delay(3000);
  
  strip.setPixelColor(0, 0, 0, 0);
  strip.setPixelColor(1, 0, 0, 0);
  strip.setPixelColor(2, 0, 0, 0);
  strip.setPixelColor(3, 0, 0, 0);
  strip.setPixelColor(4, 0, 0, 0);
  strip.setPixelColor(5, 0, 0, 0);
  strip.setPixelColor(6, 0, 0, 0);
  strip.setPixelColor(7, 0, 0, 0);
  strip.setPixelColor(8, 0, 0, 0);
  strip.setPixelColor(9, 0, 0, 0);
  strip.setPixelColor(10, 0, 0, 0);
  strip.setPixelColor(11, 0, 0, 0);
  strip.show();
  delay(1000);
}

  1. Read a button at the beginning of loop().
  2. When the button becomes pressed (not: is pressed), increment a counter that indicates which pattern is active. See the state change detection example in the IDE.
  3. Based on the counter value, set the colours; use e.g. a switch/case. There are better ways than switch/case if you use arrays where the counter will be the index into an array.
  4. Get rid of delay in favour of a millis() based approach if you don't want to wait 3 seconds
1 Like

Your 2 topics on the same project have been merged so that members can see the background to your latest question

I have played around with this all night and come along with this. But for some reason the code has no errors but the lights dont wanna show. I know I have the same colour twice in the first two patterns but I can't see anything happening
Hope you can help :slight_smile:

#include <Adafruit_NeoPixel.h>

    int buttonPin = 0;    // momentary push button on pin 0
    int oldButtonVal = 0;
    
    #define PIN 1    // Parameter 1 = number of pixels in strip
                          // Parameter 2 = pin number (most are valid)
                          // Parameter 3 = pixel type flags, add together as needed:
                          // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
                          // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
                          // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
                          // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
   Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);
    
    int nPatterns = 5;
    int lightPattern = 1;

// the setup routine runs once when you press reset:
    void setup() {
    strip.begin();
    strip.show();                // initialize all pixels to 'off'    
// initialize the BUTTON pin as an input
    pinMode(buttonPin, INPUT);
    digitalWrite(buttonPin, HIGH);  // button pin is HIGH, so it drops to 0 if pressed
}


// Pattern 1 - White light, all LEDs in the strip are white
    void pattern1() {
        strip.setPixelColor(0, strip.Color(255, 0, 0)); // red
        strip.setPixelColor(1, strip.Color(255, 0, 0));
        strip.setPixelColor(2, strip.Color(255, 0, 0));
        strip.setPixelColor(3, strip.Color(255, 0, 0));
        strip.setPixelColor(4, strip.Color(255, 0, 0));
      	strip.setPixelColor(5, strip.Color(255, 0, 0));
      	strip.setPixelColor(6, strip.Color(255, 0, 0));
      	strip.setPixelColor(7, strip.Color(255, 0, 0));
      	strip.setPixelColor(8, strip.Color(255, 0, 0));
      	strip.setPixelColor(9, strip.Color(255, 0, 0));
      	strip.setPixelColor(10, strip.Color(255, 0, 0));
     	strip.setPixelColor(11, strip.Color(255, 0, 0));
      	strip.setPixelColor(12, strip.Color(255, 0, 0));
    strip.show();
    }
    
// Pattern 2 - Red light, all LEDs in the strip are red
    void pattern2() {
        strip.setPixelColor(0, strip.Color(255, 0, 0)); // Red
        strip.setPixelColor(1, strip.Color(255, 0, 0));
        strip.setPixelColor(2, strip.Color(255, 0, 0));
        strip.setPixelColor(3, strip.Color(255, 0, 0));
        strip.setPixelColor(4, strip.Color(255, 0, 0));
    strip.show();
    }
    
// Pattern 3 - Orange light, all LEDs in the strip are orange
    void pattern3() {
        strip.setPixelColor(0, strip.Color(255, 128, 0)); // Orange
        strip.setPixelColor(1, strip.Color(255, 128, 0));
        strip.setPixelColor(2, strip.Color(255, 128, 0));
        strip.setPixelColor(3, strip.Color(255, 128, 0));
        strip.setPixelColor(4, strip.Color(255, 128, 0));
    strip.show();
    }

// Pattern 4 - Yellow light, all LEDs in the strip are yellow
    void pattern4() {
        strip.setPixelColor(0, strip.Color(255, 255, 0)); // Yellow
        strip.setPixelColor(1, strip.Color(255, 255, 0));
        strip.setPixelColor(2, strip.Color(255, 255, 0));
        strip.setPixelColor(3, strip.Color(255, 255, 0));
        strip.setPixelColor(4, strip.Color(255, 255, 0));
    strip.show();
    }
    
// Pattern 5 - Green light, all LEDs in the strip are green
    void pattern5() {
        strip.setPixelColor(0, strip.Color(0, 255, 0)); // Green
        strip.setPixelColor(1, strip.Color(0, 255, 0));
        strip.setPixelColor(2, strip.Color(0, 255, 0));
        strip.setPixelColor(3, strip.Color(0, 255, 0));
        strip.setPixelColor(4, strip.Color(0, 255, 0));
    strip.show();
    }



// the loop routine runs over and over again forever;
void loop() {
  // read that state of the pushbutton value;
  int buttonVal = digitalRead(buttonPin);
  if (buttonVal == LOW && oldButtonVal == HIGH) {// button has just been pressed
    lightPattern = lightPattern + 1;
  }
  if (lightPattern > nPatterns) lightPattern = 1;
  oldButtonVal = buttonVal;
  
  switch(lightPattern) {
    case 1:
      pattern1();
      break;
    case 2:
      pattern2();
      break;
    case 3:
      pattern3();
      break;
    case 4:
      pattern4();
      break;
    case 5:
      pattern5();
      break;
  }
}

Move your button to another pin (not 0 and 1) so you can use Serial for debugging. Add Serial.begin(some baudrate) to setup.

Add Serial.println statements at relevant places in loop() to see what your code is doing.

Check the wiring of the button using a sketch to see if it's wired correctly; e.g. switch the built-in led on if the button is pressed and switch it off if the button is released.

Don't move on until you understand what is happening in your modified sketch below:

#include <Adafruit_NeoPixel.h>

#define PIN                12

Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);

const byte nPatterns     = 5;
const byte buttonPin     = 8;

byte oldButtonVal        = HIGH;
byte lightPattern        = 0;

//Timing suff
unsigned long switchMillis;


//****************************************************************************
void setup()
{
  strip.begin();
  strip.show();
  strip.setBrightness(50);

  pinMode(buttonPin, INPUT_PULLUP);

} //END of setup()


//****************************************************************************
void loop()
{
  //*******************************************
  //is it time to check the switches ?
  if (millis() - switchMillis >= 50)
  {
    //restart the TIMER
    switchMillis = millis();

    checkSwitches();

  }

} //END of loop()


//****************************************************************************
void checkSwitches()
{
  //read the current state of this switch
  int buttonVal = digitalRead(buttonPin);


  //*******************************************
  //has ther been a change in state ?
  if (oldButtonVal != buttonVal)
  {
    //update to the new state
    oldButtonVal = buttonVal;

    //*******************************
    //is the switch pushed now ?
    if (buttonVal == HIGH)
    {
      lightPattern++;

      //*******************************
      if (lightPattern > nPatterns)
      {
        lightPattern = 0;
      }

      //*******************************
      switch (lightPattern)
      {
        case 0:
          pattern0();
          break;

        case 1:
          pattern1();
          break;

        case 2:
          pattern2();
          break;

        case 3:
          pattern3();
          break;

        case 4:
          pattern4();
          break;

        case 5:
          pattern5();
          break;
      }
    }
  }

} //END of checkSwitches()


//****************************************************************************
// Pattern 0 - all LEDs in the strip are black
void pattern0()
{
  strip.setPixelColor(0, strip.Color(0, 0, 0)); // black
  strip.setPixelColor(1, strip.Color(0, 0, 0));
  strip.setPixelColor(2, strip.Color(0, 0, 0));
  strip.setPixelColor(3, strip.Color(0, 0, 0));
  strip.setPixelColor(4, strip.Color(0, 0, 0));
  strip.setPixelColor(5, strip.Color(0, 0, 0));
  strip.setPixelColor(6, strip.Color(0, 0, 0));
  strip.setPixelColor(7, strip.Color(0, 0, 0));
  strip.setPixelColor(8, strip.Color(0, 0, 0));
  strip.setPixelColor(9, strip.Color(0, 0, 0));
  strip.setPixelColor(10, strip.Color(0, 0, 0));
  strip.setPixelColor(11, strip.Color(0, 0, 0));
  strip.setPixelColor(12, strip.Color(0, 0, 0));

  strip.show();
  
}


//****************************************************************************
// Pattern 1 - White light, all LEDs in the strip are white
void pattern1()
{
  strip.setPixelColor(0, strip.Color(0, 0, 255)); // blue
  strip.setPixelColor(1, strip.Color(0, 0, 255));
  strip.setPixelColor(2, strip.Color(0, 0, 255));
  strip.setPixelColor(3, strip.Color(0, 0, 255));
  strip.setPixelColor(4, strip.Color(0, 0, 255));
  strip.setPixelColor(5, strip.Color(0, 0, 255));
  strip.setPixelColor(6, strip.Color(0, 0, 255));
  strip.setPixelColor(7, strip.Color(0, 0, 255));
  strip.setPixelColor(8, strip.Color(0, 0, 255));
  strip.setPixelColor(9, strip.Color(0, 0, 255));
  strip.setPixelColor(10, strip.Color(0, 0, 255));
  strip.setPixelColor(11, strip.Color(0, 0, 255));
  strip.setPixelColor(12, strip.Color(0, 0, 255));

  strip.show();
  
}


//****************************************************************************
// Pattern 2 - Red light, all LEDs in the strip are red
void pattern2()
{
  strip.setPixelColor(0, strip.Color(255, 0, 0)); // Red
  strip.setPixelColor(1, strip.Color(255, 0, 0));
  strip.setPixelColor(2, strip.Color(255, 0, 0));
  strip.setPixelColor(3, strip.Color(255, 0, 0));
  strip.setPixelColor(4, strip.Color(255, 0, 0));

  strip.show();
  
}


//****************************************************************************
// Pattern 3 - Orange light, all LEDs in the strip are orange
void pattern3()
{
  strip.setPixelColor(0, strip.Color(255, 128, 0)); // Orange
  strip.setPixelColor(1, strip.Color(255, 128, 0));
  strip.setPixelColor(2, strip.Color(255, 128, 0));
  strip.setPixelColor(3, strip.Color(255, 128, 0));
  strip.setPixelColor(4, strip.Color(255, 128, 0));

  strip.show();
  
}


//****************************************************************************
// Pattern 4 - Yellow light, all LEDs in the strip are yellow
void pattern4()
{
  strip.setPixelColor(0, strip.Color(255, 255, 0)); // Yellow
  strip.setPixelColor(1, strip.Color(255, 255, 0));
  strip.setPixelColor(2, strip.Color(255, 255, 0));
  strip.setPixelColor(3, strip.Color(255, 255, 0));
  strip.setPixelColor(4, strip.Color(255, 255, 0));

  strip.show();
  
}


//****************************************************************************
// Pattern 5 - Green light, all LEDs in the strip are green
void pattern5()
{
  strip.setPixelColor(0, strip.Color(0, 255, 0)); // Green
  strip.setPixelColor(1, strip.Color(0, 255, 0));
  strip.setPixelColor(2, strip.Color(0, 255, 0));
  strip.setPixelColor(3, strip.Color(0, 255, 0));
  strip.setPixelColor(4, strip.Color(0, 255, 0));

  strip.show();
  
}

Sweet thank you! I have had some time with this and sorted everything out! And got it working. The only issue I have now is when the first pattern the white flickers red. Would you have any suggestion on why this would happen??

Did you try the sketch in post #13 ?