How to change colors using WS2812b LED STRIP using a button

Hello, my project is a lightsaber, but I want to make my own code as I am excited to learn about programming. I have thought of multiple ways before coming to this page. My code is shown below. There are three buttons and two of them are connected and work, however the third 'colorchange' button is stumping me. I would like it so that everytime I hit that button it will change the default color of the strip and cycle through a set preset. I was looking at switch statements and if statements but nothing was working. Any help would be appreciated.

#include <FastLED.h>
#include <Button.h>


#define FRAMES_PER_SECOND 120
#define BRIGHTNESS 50
#define DATA_PIN 7
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 144
CRGB leds[NUM_LEDS];

#define blue (CRGB::Blue);
#define green (CRGB::Green);
#define red (CRGB::Red);
#define purple (CRGB::Purple);

Button bladeup(2);
Button bladedown(4);
Button colorchange(8);

void setup() {
  
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
  
  bladeup.begin();
  bladedown.begin();
  colorchange.begin();
}

void loop(){

  if(bladeup.pressed())
    for (int i = 0; i < NUM_LEDS; i++){
    leds[i] = blue;
    FastLED.show();
    }


  if(bladedown.pressed())
    for (int i = NUM_LEDS - 1; i >= 0; i--) {
    leds[i] = CRGB::Black;
    FastLED.show();  
    }

}

All the electrical components have been triple checked, a code where I can cycle through the four colors with that one button is what I hope to find.

One idea I had compiles, and I am not sure why it isn't working. This code ends up messing up the other buttons.

#include <FastLED.h>
#include <Button.h>
#include <Adafruit_NeoPixel.h>


#define FRAMES_PER_SECOND 120
#define BRIGHTNESS 2
#define DATA_PIN 7
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 144
CRGB leds[NUM_LEDS];

#define blue (CRGB::Blue);
#define green (CRGB::Green);
#define red (CRGB::Red);
#define purple (CRGB::Purple);

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, 7, NEO_GRB + NEO_KHZ800);

int j=0;

Button bladeup(2);
Button bladedown(4);
Button colorchange(8);

void setup() {
  
  
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);

  bladeup.begin();
  bladedown.begin();
  colorchange.begin();
}

void loop(){
 
if(colorchange.pressed())     
    for (int j = 0; j < COLORS; j+1){
  if(bladeup.pressed())
     for (int i = 0; i < NUM_LEDS; i++)
    {
            if(j = 0)
            {
              leds[i] = blue;
              FastLED.show();
            }
            if(j = 1)
            {
              leds[i] = green;
              FastLED.show();
            }
            if(j = 2)
            {
              leds[i] = red;
              FastLED.show();        
            }
            if(j = 3)
            {
              leds[i] = purple;
              FastLED.show();
            }

            
    }
    }

  if(bladedown.pressed())
    for (int i = NUM_LEDS - 1; i >= 0; i--) {
    leds[i] = CRGB::Black;
    FastLED.show();  
    }
}

I could be heading in the wrong direction, any help would be appreciated.

All the examples in the Buuton.h library I found call the listen method at the top of the loop() function.

This is typical. Many button libraries need to have regular (loop frequency) attention, usually with a method like update or, evidently, listen.

Make sure you are using the button library as the designed intended.

There may be other problems.

a7

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