Hi, can anyone point me in the right direction. I am trying to turn on and off a WS2812b LED from a button.
My code compiles OK, but when i upload it the LED is red and when i push the button it goes green.
I would like the LED to be off and when i push the button it goes Red and stays red when i release the button.
Here is the code
#include <FastLED.h> #define NUM_LEDS 1 #define DATA_PIN 7
const int keyPin = 12; //the number of the key pin
const int ledPin = 7;//the number of the led pin
CRGB leds[NUM_LEDS];
void setup()
{
pinMode(keyPin,INPUT);//initialize the key pin as input
pinMode(ledPin,OUTPUT);//initialize the led pin as output
delay(2000);
// FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
}
void loop()
{
if(digitalRead(keyPin) ==HIGH )
{
digitalWrite(ledPin,HIGH);//turn on the led
}
else
{
digitalWrite(ledPin,LOW);//turn off the led
}
//fill_solid(leds, NUM_LEDS, CRGB::Red);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Red;
Ross, you might need to store the on/off state also. Have a look at this example to combine with the example Stuart mentioned.
[Edit: corrected link] http://arduino.cc/en/Tutorial/ButtonStateChange
The button might work fine, or you might find that the it works erratically/inconsistently, in which case you might need to also look into debouncing.
@Jurgen_Skrotzky Thanks, but i really struggling to get my head around your button code. I wish they had some workshops here to help me develop a deeper understanding of code.
@Stuart_Taylor
Nearly there! Using Matthias Hertel’s One Button Master. I am going to try his two button master beacuse i want to try and call say eight different ‘cues’. Thanks