Hi, can anyone point me in the right direction.

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;

FastLED.show();
delay(30);
}
}

Hi, welcome to working with “smart” pixels.

the led is a “smart” led, and it expects to receive instructions from the FastLED library, not just an on or off (high or low).

when you press the button, your code does this:

digitalWrite(ledPin,HIGH);//turn on the led

You want your code to send a change colour instruction.

Check the blink demo included with the library.

Come back if you’re still stuck after that.

Thanks. I’ll work with this this week. I’ll let you know how i get on.

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.

@Ross_Malyon if you want, you can use my button code as well:

Here you can see how to use it:

I think it is really helpful, because if you hold down the pushbutton inside a loop - and you want to “toggle” it can flash on and off really fast.

@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 :slight_smile:

Great news! Dont forget to #showoffyourwork when its done.

I will, Don’t worry. Planning some beach parties and will be using various LED objects.