#include <Servo.h>
#include <Adafruit_NeoPixel.h>
#define LED_PIN 6
#define NUM_LEDS 30
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
Servo switchServo;
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to off
switchServo.attach(9); // Attach servo signal wire to digital pin 9
switchServo.write(90); // Set the initial servo position
// Set the LED strip to initial color (red)
setColor(255, 0, 0);
}
void loop() {
int switchPosition = switchServo.read();
// You may need to adjust these values depending on your specific transmitter
int position1 = 65; // Switch position for the first color (red)
int position2 = 85; // Switch position for the second color (blue and green)
if (switchPosition < position1) {
// Set the color to red
setColor(255, 0, 0);
} else if (switchPosition < position2) {
// Set the color to blue and green
setColor(0, 0, 255);
delay(1000); // Delay for a smoother transition between colors
setColor(0, 255, 0);
} else {
// Set the color to off (black)
setColor(0, 0, 0);
}
}
void setColor(uint8_t red, uint8_t green, uint8_t blue) {
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, strip.Color(red, green, blue));
}
strip.show();
}
Note that this script assumes you have a common anode connection for your WS2812 LED strip (power is connected to VCC). If you have a common cathode strip, you'll need to adjust the color values accordingly.