Hello,
I am new to Arduino and I am trying to use a push button to control the brightness of the led strip. I am using FastLed and I have tried two different codes to try and do this but ones I press the button the strip will go from 255 to 0 and then back again and will continue to do so with out me pressing the button.
Here is the code that I am using:
#include "FastLED.h"
#define DATA_PIN 3
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 60
int BRIGHTNESS = 255;
#define button 9 // where the button is connected to
int buttonState = LOW;
int lastButtonState = LOW;
boolean lastButton = LOW;
boolean currentButton = LOW;
CRGB leds[NUM_LEDS];
void setup() {
delay(3000); // 3 second delay for recovery
pinMode(DATA_PIN,OUTPUT);
pinMode(button,INPUT);
// tell FastLED about the LED strip configuration
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
}
boolean debounce (boolean last)
{
boolean current = digitalRead(button);
if(last != current)
{
delay(50);
current = digitalRead(button);
}
return current;
}
void loop()
{
buttonState = digitalRead(button);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
delay(20);
buttonState = digitalRead(button);
if (buttonState == LOW)
{
BRIGHTNESS = BRIGHTNESS - 51;
}
if(BRIGHTNESS < 0)
{
BRIGHTNESS = 255;
}
FastLED.setBrightness(BRIGHTNESS);
lastButtonState = buttonState;
}
//try to use booleans here
// currentButton = debounce(lastButton);
// if(currentButton == HIGH && lastButton == LOW)
// {
// BRIGHTNESS = BRIGHTNESS - 51;
// FastLED.setBrightness(BRIGHTNESS);
// }
// lastButton = currentButton;
//
// if(BRIGHTNESS < 0)
// {
// BRIGHTNESS = 255;
// }
}