// https://forum.arduino.cc/t/starting-led-strips-from-middle-with-potentiometer/1269639
#include "FastLED.h"
#define POTENTIOMETER A0
#define NUM_LEDS 40
#define DATA_PIN 3
// define color names for easy use
#define RED CRGB::Red
#define GRN CRGB::Green
#define BLU CRGB::Blue
#define WHT CRGB::White
#define BLK CRGB::Black
CRGB pixel[NUM_LEDS]; //
void setup() {
Serial.begin(115200);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(pixel, NUM_LEDS);
FastLED.clear(); // all pixels off
FastLED.show();
pixel[0] = WHT;
pixel[NUM_LEDS / 2] = RED;
pixel[NUM_LEDS - 1] = BLU;
FastLED.show();
}
void loop() {
int last_pixel = map (analogRead(POTENTIOMETER), 0, 1023, 0, NUM_LEDS / 2);
// color the pixels from the middle of the LED strip to the mapped potentiometer value
for (int i = 0; i < last_pixel; i++) {
pixel[NUM_LEDS / 2 - i - 1] = RED; // pixels before the middle
pixel[NUM_LEDS / 2 + i] = GRN; // pixels after the middle
FastLED.show();
}
// clear the pixels from the mapped potentiometer value to 0 or NUM_LEDS
for (int i = last_pixel; i < NUM_LEDS / 2; i++) {
pixel[NUM_LEDS / 2 - i - 1] = BLK; // pixels before the middle
pixel[NUM_LEDS / 2 + i] = BLK; // pixels after the middle
FastLED.show();
}
}
/*
0---------H--------N
1111111111
0123456789.123456789
*/