#include <FastLED.h> // https://github.com/FastLED/FastLED
#define LDR A0 // analog output
#define LED_NUM 10 // number of LEDs or WS2812
#define BAR_PIN 3 // LEDs starting pin
#define PIX_PIN 2 // WS2812 pin
#define MAXBRIGHT 255
CRGB pixleds[LED_NUM]; // create FastLED object
void setup() {
FastLED.addLeds<WS2812B, PIX_PIN, GRB>(pixleds, LED_NUM); // initialize FastLED object
FastLED.setBrightness(MAXBRIGHT);
FastLED.clear();
FastLED.show();
for (int i = 0; i < LED_NUM; i++) { // initialize the LED pins
pinMode(BAR_PIN + i, OUTPUT);
}
}
void loop() {
float ldr = analogRead(A0); // read the LDR
int lux = map (ldr, 1015, 8, 1, 10); // map LDR to 10 LEDs (1015 and 8 are simulation MAX and MIN)
for (int i = 0; i < lux; i++) { // counting zero to LDR map
pixleds[i] = CRGB(lux * 25.5, 0, 255 - (lux * 25.5)); // color WS2812 from BLU to RED
FastLED.show();
digitalWrite(BAR_PIN + i, HIGH); // light LEDs
}
for (int i = lux; i < LED_NUM; i++) { // counting from LDR map to MAX/10
pixleds[i] = CRGB::Black; // turn WS2812 off
FastLED.show();
digitalWrite(BAR_PIN + i, LOW); // turn LEDs off
}
}
Click LDR
to adjust.