Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TimeLib.h>
- #include <TimeAlarms.h>
- #include <FastLED.h>
- // MapTwinkle
- // Designed to illuminate a 'map' of pixels, each of which randomly
- // sometimes twinkles brighter and then back down to it's base color again.
- //
- // Parameters include: background color, peak twinkle color, and speed
- // of brightening and dimming.
- //
- // Mark Kriegsman, August 2015
- #define NUM_GARAGE_LEDS 100
- #define NUM_HOUSE_LEDS 150
- #define NUM_LEDS (NUM_GARAGE_LEDS+NUM_HOUSE_LEDS)
- #define LED_TYPE WS2812
- #define COLOR_ORDER RGB
- // Pins
- #define GARAGE_PIN 2
- #define HOUSE_PIN 14
- #define PS_ON_PIN 23
- #define MASTER_BRIGHTNESS 255
- // Base background color
- #define BASE_COLOR CRGB(0,0,127)
- // Peak color to twinkle up to
- #define PEAK_COLOR CRGB(255,255,255)
- // Currently set to brighten up a bit faster than it dims down,
- // but this can be adjusted.
- // Amount to increment the color by each loop as it gets brighter:
- #define DELTA_COLOR_UP CRGB(4,4,2)
- // Amount to decrement the color by each loop as it gets dimmer:
- #define DELTA_COLOR_DOWN CRGB(4,4,2)
- // Chance of each pixel starting to brighten up.
- // 1 or 2 = a few brightening pixels at a time.
- // 10 = lots of pixels brightening at a time.
- #define CHANCE_OF_TWINKLE 1
- CRGB leds[NUM_LEDS];
- bool isRunning = false;
- void setup() {
- // set the Time library to use Teensy 3.0's RTC to keep time
- setSyncProvider(getTeensy3Time);
- // turn off the power supply
- pinMode(PS_ON_PIN, OUTPUT);
- digitalWrite(PS_ON_PIN, HIGH);
- //FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE, GARAGE_PIN, COLOR_ORDER>(leds, 0, NUM_GARAGE_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE, HOUSE_PIN, COLOR_ORDER>(leds, NUM_GARAGE_LEDS, NUM_HOUSE_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.setBrightness(MASTER_BRIGHTNESS);
- InitPixelStates();
- Alarm.alarmRepeat(17, 0, 0, startRunning);
- Alarm.alarmRepeat( 1, 0, 0, stopRunning);
- if (hour() == 0 || hour() >= 17) {
- digitalWrite(PS_ON_PIN, LOW);
- isRunning = true;
- }
- }
- void loop()
- {
- if (isRunning) {
- TwinkleMapPixels();
- FastLED.show();
- Alarm.delay(40);
- } else {
- Alarm.delay(1000);
- }
- }
- enum { SteadyDim, GettingBrighter, GettingDimmerAgain };
- uint8_t PixelState[NUM_LEDS];
- void InitPixelStates()
- {
- memset( PixelState, sizeof(PixelState), SteadyDim); // initialize all the pixels to SteadyDim.
- fill_solid( leds, NUM_LEDS, BASE_COLOR);
- }
- void TwinkleMapPixels()
- {
- for( uint16_t i = 0; i < NUM_LEDS; i++) {
- if( PixelState[i] == SteadyDim) {
- // this pixels is currently: SteadyDim
- // so we randomly consider making it start getting brighter
- if( random16(1024) < CHANCE_OF_TWINKLE) {
- PixelState[i] = GettingBrighter;
- }
- } else if( PixelState[i] == GettingBrighter ) {
- // this pixels is currently: GettingBrighter
- // so if it's at peak color, switch it to getting dimmer again
- if( leds[i] >= PEAK_COLOR ) {
- PixelState[i] = GettingDimmerAgain;
- } else {
- // otherwise, just keep brightening it:
- leds[i] += DELTA_COLOR_UP;
- }
- } else { // getting dimmer again
- // this pixels is currently: GettingDimmerAgain
- // so if it's back to base color, switch it to steady dim
- if( leds[i] <= BASE_COLOR ) {
- leds[i] = BASE_COLOR; // reset to exact base color, in case we overshot
- PixelState[i] = SteadyDim;
- } else {
- // otherwise, just keep dimming it down:
- leds[i] -= DELTA_COLOR_DOWN;
- }
- }
- }
- }
- time_t getTeensy3Time()
- {
- return Teensy3Clock.get();
- }
- void stopRunning() {
- isRunning = false;
- FastLED.showColor(CRGB::Black);
- digitalWrite(PS_ON_PIN, HIGH);
- }
- void startRunning() {
- InitPixelStates();
- digitalWrite(PS_ON_PIN, LOW);
- isRunning = true;
- }
Add Comment
Please, Sign In to add comment