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
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.
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
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.
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.
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
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
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