0

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:

enter image description here

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?

2

The problem lies in your for cycle.

  • If (state == 1) you are switching the red colour ON.
  • If (state == 2) you are switching the blue colour is ON. So at this moment, you have red AND blue colour on, not only blue. I assume the output colour is purple-ish.
  • If (state == 3), you activate red (which is already ON), blue (which is also already ON) and also green (which changes state from OFF to ON) so you get white colour. At this moment you went through all three possible conditions.
  • Then you go back to (state == 1). All colours are already activated, so from this moment you get only white...

Solution:

for (int i = 0; i < NUM_LEDS; i++) {
  if (state == 1) {
    leds[i].red = 100;
    leds[i].blue = 0;
    leds[i].green = 0;
    Serial.println("Red");
  }
  if (state == 2) {
    leds[i].red = 0;
    leds[i].blue = 100;
    leds[i].green = 0;
    Serial.println("Blue");
  }
  if (state == 3) {
    leds[i].red = 100;
    leds[i].blue = 100;
    leds[i].green = 100;
    Serial.println("White");
  }
}
2

In your lines:

                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");}

The LEDs are set on, but are never turned off.

Sequence is:

1: Red leds turn on; strip appears red.

2: Blue leds turn on (the leds will probably actually look purple rather than blue here as the red leds are still on)

3: Green leds turn on (red and blue leds are already on); strip appears white

back to start:

1,2,3: nothing changes as all leds are already on, strip remains white.

I'd suggest adding:

leds[i].green = 0; leds[i].blue = 0;

to the state == 1 condition and

leds[i].green = 0; leds[i].red = 0

to the state == 2 condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.