How to Change light with a button

I want to press a button and have it change the light of my LED strip but cant seem to get it to work, I used this video as a reference. Control an RGB LED with a Button | Beginner Arduino Project - YouTube

#include <FastLED.h>
#define LED_PIN 6
#define NUM_LEDS 100
#define button 10

CRGB leds[NUM_LEDS];


int mode = 0;
void setup() {
  FastLED.addLeds<WS2811, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
  FastLED.clear();
  FastLED.show();

  pinMode(button, INPUT_PULLUP);
  


}

void loop() {

  if (digitalRead(button) == LOW) {
    mode = mode + 1;
    delay(200);
    Serial.begin(9600);
    Serial.println(mode);
    }
 
 if (mode = 0){
    leds[1] = CRGB(0, 0, 0);
    
  }
 
 if (mode == 1){
    leds[1] = CRGB(255, 0, 0);
    
  }
  
 if (mode == 2){
    leds[1] = CRGB(0, 255, 0);
    
  }

 if (mode == 3){
    leds[1] = CRGB(0, 0, 255);
    
  }

if (mode == 4){
    leds[1] = CRGB(100, 0, 200);
    
  }
  
if (mode == 5){
    leds[1] = CRGB(35, 40, 210);
    
  }
  
if (mode == 6){
    mode = 0;
  }
  FastLED.show();

}

“ if (digitalRead(button) == LOW) “

Since loop() loops forever, mode will very soon get larger than 6.

Look at switch change in state example.

Whenever I press the button the serial monitor the "mode" does not increase but rather just repeat the same number over and over again

if (mode = 0){

= (assignment) is not the same as == (comparison)

You need ==, comparison
if (mode == 0){

Anyway, run Serial.begin() only once in setup().

Hello and good morning
I have took a look into your sketch. Your sketch is addressing one LED inside the stripe only. Is this your intention? If not you have to modify the sketch to do this.
What did I do:

  1. inserted a state change detector for the button
  2. inserted a function makeStripe() to process the changes of colours
  3. changed the zoo of if-instructions into a switch/case structure.

Try it and have a nice day.

#include <FastLED.h>
#define LED_PIN 6
#define NUM_LEDS 100
#define button 10
CRGB leds[NUM_LEDS];
int mode = 0;

void makeStripe() {
  switch (mode) {
    case 0: leds[1] = CRGB(0, 0, 0); break;
    case 1: leds[1] = CRGB(255, 0, 0); break;
    case 2: leds[1] = CRGB(0, 255, 0); break;
    case 3: leds[1] = CRGB(0, 0, 255); break;
    case 4: leds[1] = CRGB(100, 0, 200); break;
    case 5: leds[1] = CRGB(35, 40, 210); break;
  }
  FastLED.show();
}
void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2811, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
  FastLED.clear();
  FastLED.show();
  pinMode(button, INPUT_PULLUP);
}
void loop() {
  static bool buttonOld = 1;
  bool state = digitalRead(button);
  if (buttonOld != state) {
    delay(200);
    buttonOld = state;
    if (!state) {
    makeStripe();
    Serial.println(mode);
    mode = mode + 1;
    mode = mode % 6;
  }
  }
}

Untested, but well done.

1 Like

Thank you so much for your help, the code works but I has a small problem, whenever I push the button down it changes the color of the light but it also changes when I release the button. I'm not sure how to fix this, could you help me? Thank you and have a nice day.

  if (buttonOld != state) {
    delay(200);
    buttonOld = state;
    if (state == LOW) {
      Serial.println(mode);
      makeStripe();
      mode = mode + 1;
      mode = mode % 6;
    }
  }

Hello
I did made the mods inside sketch above.

1 Like

Thank you both, both of the solutions work well. Have a great day!

1 Like

Hello
Many thanks for your reply.
Have a nice day, too. :vulcan_salute:

1 Like

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