Created
May 30, 2019 20:13
-
-
Save b0tting/ee00a05223392e686be33fd4e0b3fb28 to your computer and use it in GitHub Desktop.
Fading lights that jump to blue when the sensor (LDR) triggers. Used to trigger other leds on a google home mini light.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <FastLED.h> | |
#define DATA_PIN 1 | |
// On the ATTINY85, pins have different numbers when doing an analog reading compared to digital IO | |
#define SENSOR_PIN_A 1 | |
#define SENSOR_PIN_D 2 | |
#define NUM_LEDS 2 | |
#define BRIGHTNESS 150 | |
// Lower is slower | |
#define FRAMES_PER_SECOND 10 | |
// Sensitivity of the light sensor | |
#define SENSOR_SENSITIVITY 700 | |
CRGB leds[NUM_LEDS]; | |
CRGBPalette16 gPal; | |
int heat[NUM_LEDS]; | |
int order[NUM_LEDS]; | |
int sensorVal = 0; | |
const TProgmemPalette16 WarmYellow_p PROGMEM = | |
{ | |
CRGB::Yellow, | |
CRGB::Red, | |
CRGB::Orange, | |
CRGB::Orange, | |
CRGB::Orange | |
}; | |
void setup() { | |
FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS); | |
FastLED.setBrightness( BRIGHTNESS ); | |
pinMode(SENSOR_PIN_D, INPUT); | |
gPal = WarmYellow_p; | |
for(int i = 0; i < NUM_LEDS; i++) { | |
heat[i] = random(60,253); | |
order[i] = random(0,2) == 1 ? -1 : 1; | |
} | |
/* Clear display before starting */ | |
FastLED.clear(); | |
FastLED.show(); | |
FastLED.delay(1500); // Sanity before start | |
} | |
void loop() { | |
for(int i = 0; i < NUM_LEDS; i++) { | |
heat[i] = heat[i] + order[i]; | |
if(heat[i] >= 254){ | |
order[i] = -1; | |
} else if (heat[i] <= 50) { | |
order[i] = 1; | |
} | |
} | |
sensorVal = analogRead(SENSOR_PIN_A); | |
for(int i = 0; i < NUM_LEDS; i++) { | |
if(sensorVal > SENSOR_SENSITIVITY) { | |
leds[i] = CRGB::Blue; | |
} else { | |
leds[i] = ColorFromPalette( gPal, heat[i]); | |
} | |
} | |
FastLED.show(); // display this frame | |
FastLED.delay(1000 / FRAMES_PER_SECOND); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment