Button LED project

Hey, i am not very good at coding and trying to learn as much as i could. I am thinking about creating a homemade party light, in the past i have made different codes for different light effects and combined them in a loop making them play all over without eny sort of controlling the modes. Therefor i want to include a button to be able to switch between different modes on this light. Do anyone have any tips to make it so that each press of a button changes the mode from one effect to another? Heres a part of my existing code of effects:

#include <Adafruit_NeoPixel.h>

#define PIN1 7
#define N_LEDS1 24
#define NUM_LIT 50
#define SPEED 0.1
#define BRIGHTNESS 225
#include <Adafruit_NeoPixel.h>
#ifdef AVR
#include <avr/power.h>
#endif

Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(N_LEDS1, PIN1, NEO_RGB + NEO_KHZ800);

void setup() {
strip1.begin();
strip1.show();

}

void loop() {
Strobe(0xff, 0xff, 0xff, 10, 50, 1000);
chase(strip1.Color(BRIGHTNESS, BRIGHTNESS, 0)); //
// chase(strip1.Color(0, BRIGHTNESS, BRIGHTNESS)); //
// chase(strip1.Color(0, BRIGHTNESS, 0)); // Green
chase_back(strip1.Color(BRIGHTNESS, 0, 0)); // Red
// chase(strip1.Color(0, 0, BRIGHTNESS)); // Blue

Thanks for any help!:slight_smile:

Buttons...... They more than often tends to be difficult. There are tons of button question topics here. Do some search, "Arduino + button".

If you're going to be adding a pushbutton it's time to get acquainted with debouncing and state change detection: IDE > file/examples/digital.

How many effects are planned?

I have not quite desided yet but lets say 6 to say a number

I found a code online for changing color and brightness, will this one work if i change the parts where the code stats int and the color to the effect name and include the effect code later on in the code?

tandt1-btn.ino (1.98 KB)

Please post code here, using code tags. Most helpers don't like filling the computers with code like this.

This is the code

#include <FastLED.h> // FastLed library version 3.2.1 - Overview · FastLED/FastLED Wiki · GitHub or http://fastled.io/ with NEOPIXEL or WS2812B
#define LED_TYPE WS2812B // define the led type
#define NUM_STRIPS 1 // number of led strips
#define NUM_LEDS_PER_STRIP 50 // total number of leds per strip
CRGB leds[NUM_STRIPS][NUM_LEDS_PER_STRIP];

const int btn1 = 2;
const int btn2 = 3;

int btn1Val = 0;
int btn1State = 0;

int btn2Val = 0;

int colorState = 0;
int currentColor = 0;

int color1 = 0; //red
int color2 = 96; //green
int color3 = 160; //blue

int currentBrt = 100;

void setup() {
Serial.begin(115200);

pinMode(btn1, INPUT);
pinMode(btn2, INPUT);

FastLED.addLeds<NEOPIXEL, 12>(leds[0], NUM_LEDS_PER_STRIP); //define the pin output for led strip one

fill_solid(leds[0], NUM_LEDS_PER_STRIP, CRGB::Black); // some led strips are all on at power on, so let's power them off at boot
FastLED.show();
}

void loop() {
btn1Val = digitalRead(btn1);
btn2Val = digitalRead(btn2);

if(btn1Val == 1){
delay(50);
btn1State = 1;
}else if(btn1State == 1){
colorState++;
if(colorState > 2){colorState = 0;};
btn1State = 0;

Serial.print("colorState:");
Serial.println(colorState);
}

if(colorState == 0){
currentColor = color1;
}else if(colorState == 1){
currentColor = color2;
}else if(colorState == 2){
currentColor = color3;
}

if(btn2Val == 1){
delay(50);
currentBrt++;
//EVERY_N_MILLISECONDS( 30 ) { currentBrt++; }
if(currentBrt > 150){currentBrt = 0;};

Serial.print("currentBrt:");
Serial.println(currentBrt);
}

fadeToBlackBy( leds[0], NUM_LEDS_PER_STRIP, 5);
int pos = beat8(50);
pos = map(pos, 0, 255, 0, NUM_LEDS_PER_STRIP-1);
leds[0][pos] += CHSV( currentColor, 255, currentBrt);

FastLED.show();
}

Eehhh. I don't se the use of code tags, the symbol up to the left in this window... Using the Autoformat in the IDE makes the code even more readable.

Still a few important things are missing. What controller are You using?
Wiring diagram... Not any Fritzing!

All this is found in the first topic like "How to use this Forum".

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