Using a SPDT Switch to change Lighting modes

Hi,

i wanna switch between different Lighting modes of my LED-Strip. For testing i only use different colors. No matter what i change it lights only in Yellow.

This is the code i am using:

#include <FastLED_NeoPixel.h>

int Pos1 = 0;   // Switch Position 1
int Pos2 = 0;   // Switch Position 2
int Pos3 = 0;   // Switch Position 3

#define BRIGHTNESS 100
#define NUM_LEDS 43
#define DATA_PIN 6
FastLED_NeoPixel<NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800> strip;

void setup() {

strip.begin();
strip.setBrightness(BRIGHTNESS);
  
 pinMode(7, INPUT);
 pinMode(8, INPUT);

  Pos1 = 0;   
  Pos2 = 0;
  Pos3 = 0;

}



void loop() {
 delay(1000);
  if (digitalRead(7) == HIGH) {        // Switch Position 1
    Pos1 = 1;
    Pos2 = 0;
    Pos3 = 0;
  } else {
   
    if (digitalRead(8) == HIGH) {      // Switch Position 2
      Pos1 = 0;
      Pos2 = 1;
      Pos3 = 0;
    } else {                           // Switch Position 3
      Pos1 = 0;
      Pos2 = 0;
      Pos3 = 1;
    }
  }
  if (Pos1 = 1) {
    strip.Color(  0,      0,     255   );   // Blue
  }
  if (Pos2 = 1) {
    strip.Color(  255,    0,     0     );   // Red
  }
  if (Pos3= 1) {
     strip.Color(  0,     255,     0   );   // Yellow
  }
}

And this is the way i connected everything. Thanks in advance :slight_smile:

 if (Pos1 = 1)

This statement sets Pos1 to 1

I expect that you meant

 if (Pos1 == 1)

There may be other problems but I did not look any further

1 Like

I would connect the center (C) of the switch directly to ground and set the pinModes of the pins connected to NO and NC to INPUT_PULLUP. Then the closed switch will read LOW and the open side will read HIGH.

Why not just else if { ? See the else structure reference.

I don't see the advantage of the pos variables over just putting the actions in the if, else if, else structures.

Where is the FastLED.show(); function? Without that nothing will happen with the strip.

1 Like

If i put the actions directly into the if, else structures nothing ever lights up.

Connect the center (C) of the switch directly to ground without a resistor and changed the Code to:

#include <FastLED_NeoPixel.h>


#define BRIGHTNESS 100
#define NUM_LEDS 43
#define DATA_PIN 6
FastLED_NeoPixel<NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800> strip;

void setup() {

strip.begin();
strip.setBrightness(BRIGHTNESS);

  
 pinMode(7, INPUT_PULLUP);
 pinMode(8, INPUT_PULLUP);

}

void loop() {
 delay(1000);
 
  if (digitalRead(7) == HIGH) {        // Switch Position 1
    strip.Color(  0,      0,     255   );   // Blue
    FastLED.show();
    
  } else {
       if (digitalRead(8) == HIGH) {      // Switch Position 2
      strip.Color(  255,    0,     0     );   // Red
      FastLED.show();
      
    } else {                           // Switch Position 3
     strip.Color(  0,     255,     0   );   // Yellow
     FastLED.show();
    }
}
}

Using input pullup, your 3 switch positions will be 7 low, both high, or 8 low.
or, if you prefer, if 7 lo else if 8 lo else...
Code with that in mind.
Suggestion - use that info to create a 3-state variable (0, 1, 2), then feed that variable to a
switch statement for your 3 possible actions.
MUCH cleaner code.
C

Wire the switch common to GND.

A closed switch will be detected as a LOW.

image

Didn´t knew that a closed switch will be LOW with "INPUT_PULLUP" so I changed the HIGH to LOW in both if structures (I am pretty new to this).

Common is already attached to the Ground. Its always Blue now, cant change anything with the switch.

When a switch is closed, it conducts. With the center pin wired to ground, you've connected the input to ground. When the switch is open, thrown to center or the other pole, the input pullup pulls it high.

Show your new code, it's not the wiring's fault if you've wired what you're showing.

Thanks so far :+1:

#include <FastLED_NeoPixel.h>


#define BRIGHTNESS 100
#define NUM_LEDS 43
#define DATA_PIN 6
FastLED_NeoPixel<NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800> strip;

void setup() {

strip.begin();
strip.setBrightness(BRIGHTNESS);

  
 pinMode(7, INPUT_PULLUP);
 pinMode(8, INPUT_PULLUP);

}

void loop() {
 delay(1000);
 
  if (digitalRead(7) == LOW) {        // Switch Position 1
    strip.Color(  0,      0,     255   );   // Blue
    FastLED.show();
    
  } else {
       if (digitalRead(8) == LOW) {      // Switch Position 2
      strip.Color(  255,    0,     0     );   // Red
      FastLED.show();
      
    } else {                           // Switch Position 3
     strip.Color(  0,     255,     0   );   // Yellow
     FastLED.show();
    }
}
}

The Arduino 5v pin cannot safety power 43 pixels.

Is that a centre off switch ?

It was enough for all 43 LEDs when i had a other code running without the switch.

If the Switch is in the left Position the Center and the right Pin are connected.
If the Switch is in the right Position the Center and the left Pin are connected.
If the Switch is in the middle Position no Pins are connected.

Tested this with a multimeter.

Don't see a code problem, but,

  • absolutely, your Uno won't last powering 43 LEDs. Don't care what may have worked briefly, longer term this is Uno-death.
  • point of order, since all 3 possible cases do the FastLED.show() call, move it out of the if-then-else tree. Logically, same difference, but repetitive code is wasted code.
    C

Sounds correct.

Please tell us in words what is supposed to happen in the code.


43 LEDs full brightness one colour (say red) is:

43 * .02A = 860mA too much for the Arduino.

You could check the switch like so:
Edit: for a SPDT center off switch

if(digitalRead(7) == LOW && digitalRead(8) == HIGH)
{
    // set color to red
}
else if(digitalRead(7) == HIGH && digitalRead(8) == LOW)
{
    // set color to blue
}
else  // both LOW or both HIGH
{
    // set color to yellow
}
FastLED.show();  // per @camsysca

I never used the full Brightness, can also use only 5 LEDs to test the Code and later attach a external power Source.

I want to Switch Lighting Modes with the Switch, for example:

Position 1: One fixed Color
Position 2: Rainbow running along (Strandtest - ColorWipe)
Position 3: Rainbow (All Pixels change color at the same time)

Normal function of an SPDT slide switch. Checking other pole disconnected is, IMHO only, unwarranted.
Unless you've located an issue, @picea , I'd like to see an actual photo of your setup. Your results are making me think you've got a one-off type error in your wiring.
C

Good plan for now, until you locate a usable 5V power supply of suitable current ampacity.

That’s full brightness blue.

I can upload a Picture of the wiring, but it looks a bit chaotic, might need to clean it up a bit for you to see anything on a photo.

What i forgot to say, i got a Arduino Nano every, not an Uno, but as far a i know it does not matter.