How can i control LED with mini on/off push button?

Hi,
I'm working on a project where a mini on/off push button would change the LED strip color each time the button is pressed. For now I would like to indicate red, green and blue to understand how it works. I'm using:

  • an Arduino Nano Every
  • Addressable LED strip
  • Mini on/off push button
  • 330 ohm resistor
  • 100 uF capacitor

Hoping somebody can help me with this.
Thank you.

What help are you looking for?

1 Like

What exact strip do you have? Post a link to where you got it, please.

Look at the state change detection example. Use the buttonPushCounter variable to select a color using a switch case structure.

See also my state change for active low switches tutorial.

Start with this tutorial:

StateChangeDetection

Maybe this example code will be of use. It uses the state change detection method applied to a pushbutton (momentary) switch. Each press increments a counter up to a maximum number above which the counter is reset to 0. A switch case structure changes the strip color based on the counter value.

/*
   The circuit:
   - pushbutton attached to pin 12 from ground
   - the internal pullup on pin 12 is enabled
   - LED attached from pin 13 to ground (or use the built-in LED on most
    Arduino boards)
   - A WS2812 LED strip is connected to pin 6
*/

#include <FastLED.h>

// this constant won't change:
const byte  buttonPin = 12;    // the pin that the pushbutton is attached to
const byte ledPin = 13;       // the pin that the LED is attached to
const byte DATA_PIN_1 = 6; // led strip data pin

const byte NUM_LEDS_1 = 20;

CRGB leds_1[NUM_LEDS_1];

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses


void setup()
{
   // initialize the button pin as a input:
   pinMode(buttonPin, INPUT_PULLUP);
   // initialize the LED as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communication:
   LEDS.addLeds<WS2812, DATA_PIN_1, GRB>(leds_1, NUM_LEDS_1);
   Serial.begin(115200);
}


void loop()
{
   static bool lastButtonState = 1;     // previous state of the button
   static unsigned long timer = 0;
   unsigned long interval = 50;
   if (millis() - timer >= interval)
   {
      timer = millis();

      // read the pushbutton input pin:
      bool buttonState = digitalRead(buttonPin);

      // compare the buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         // if the state has changed, increment the counter
         if (buttonState == LOW)
         {
            // if the current state is LOW then the button went from off to on:
            buttonPushCounter++;
            if (buttonPushCounter > 4)
            {
               buttonPushCounter = 0;
            }
         }
         // save the current state as the last state, for next time through the loop
         lastButtonState = buttonState;
      }

      // select strip color with pushButtonCounter value
      //Serial.print("buttonPushCounter: ");
      //Serial.println(buttonPushCounter);
      switch (buttonPushCounter)
      {
         case 0:
            for (int n = 0; n < NUM_LEDS_1; n++)
            {
               leds_1[n] = CRGB::Red;
            }
            break;
         case 1:
            for (int n = 0; n < NUM_LEDS_1; n++)
            {
               leds_1[n] = CRGB::Purple;
            }
            break;
         case 2:
            for (int n = 0; n < NUM_LEDS_1; n++)
            {
               leds_1[n] = CRGB::Blue;
            }
            break;
         case 3:
            for (int n = 0; n < NUM_LEDS_1; n++)
            {
               leds_1[n] = CRGB::Green;
            }
            break;
         case 4:
            for (int n = 0; n < NUM_LEDS_1; n++)
            {
               leds_1[n] = CRGB::Cyan;
            }
            break;
         default:
            for (int n = 0; n < NUM_LEDS_1; n++)
            {
               leds_1[n] = CRGB::Black;
            }
      }
   }
   FastLED.show();
}

Hello luminerd
Post your sketch, well formated, with comments and in so called code tags "</>" and schematic to see how we can help.
Have a nice day and enjoy coding in C++.

thank you, these tutorials are really helpful to make understand how switch works

Wow this really gave me a solution, works seamlessly. I'm glad that i'm able to change the variants of color by itself. Thank you so much :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.