I'm making a desk lamp out of 2x WS2811 strips (attached to the triple ports at top right of board below). The brightness of the strips is controlled by a potentiometer, and the colour by a switch. Each press of the switch cycles through red, blue and white:
Here's the code I've come up with:
#include <FastLED.h>
#define strip_pin 7
#define NUM_LEDS 15
CRGB leds[NUM_LEDS];
int distance;
int brightness;
int brightnesspin = 2;
int pin_switch = 3;
// variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState1 = LOW;
boolean newSwitchState2 = LOW;
boolean newSwitchState3 = LOW;
byte state = 0;
void setup()
{
Serial.begin(9600);
FastLED.addLeds<WS2811, strip_pin, BRG>(leds, NUM_LEDS);
}
void loop()
{
delay(500);
// Colour control
//##############################################
newSwitchState1 = digitalRead(pin_switch);
delay(1);
newSwitchState2 = digitalRead(pin_switch);
delay(1);
newSwitchState3 = digitalRead(pin_switch);
// if all 3 values are the same we can continue
if ( (newSwitchState1==newSwitchState2) && (newSwitchState1==newSwitchState3) )
{
if ( newSwitchState1 != oldSwitchState )
{
// has the button switch been closed?
if ( newSwitchState1 == HIGH )
{
// increase the value of state
state++;
if (state > 3) { state = 0; }
for (int i = 0; i < NUM_LEDS; i++)
{
if (state==1) { leds[i].red = 100;Serial.println("Red"); }
if (state==2) { leds[i].blue = 100;Serial.println("Blue"); }
if (state==3) { leds[i].red = 100; leds[i].blue = 100; leds[i].green = 100;Serial.println("White");}
}
FastLED.show();
}
oldSwitchState = newSwitchState1;
}
// Brightness control
//##############################################
brightness = analogRead(brightnesspin); brightness = map(brightness, 0, 1024, 0, 255);
for (int i = 0; i<NUM_LEDS; i++)
{
FastLED.setBrightness(brightness);
}
FastLED.show();
Serial.print("Brightness: ");
Serial.println(brightness);
}
}
The brightness control works fine, and the switch cycles through the colours once, but the strip stays white once it's there, even though the serial monitor shows the switch changing state. How can I get the strip to cycle back round to red?