Guest User

Untitled

a guest
Dec 12th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #include <TimeLib.h>
  2. #include <TimeAlarms.h>
  3. #include <FastLED.h>
  4.  
  5. // MapTwinkle
  6. // Designed to illuminate a 'map' of pixels, each of which randomly
  7. // sometimes twinkles brighter and then back down to it's base color again.
  8. //
  9. // Parameters include: background color, peak twinkle color, and speed
  10. // of brightening and dimming.
  11. //
  12. // Mark Kriegsman, August 2015
  13.  
  14. #define NUM_GARAGE_LEDS 100
  15. #define NUM_HOUSE_LEDS 150
  16. #define NUM_LEDS (NUM_GARAGE_LEDS+NUM_HOUSE_LEDS)
  17. #define LED_TYPE WS2812
  18. #define COLOR_ORDER RGB
  19.  
  20. // Pins
  21. #define GARAGE_PIN 2
  22. #define HOUSE_PIN 14
  23. #define PS_ON_PIN 23
  24.  
  25. #define MASTER_BRIGHTNESS 255
  26.  
  27. // Base background color
  28. #define BASE_COLOR CRGB(0,0,127)
  29.  
  30. // Peak color to twinkle up to
  31. #define PEAK_COLOR CRGB(255,255,255)
  32.  
  33. // Currently set to brighten up a bit faster than it dims down,
  34. // but this can be adjusted.
  35.  
  36. // Amount to increment the color by each loop as it gets brighter:
  37. #define DELTA_COLOR_UP CRGB(4,4,2)
  38.  
  39. // Amount to decrement the color by each loop as it gets dimmer:
  40. #define DELTA_COLOR_DOWN CRGB(4,4,2)
  41.  
  42. // Chance of each pixel starting to brighten up.
  43. // 1 or 2 = a few brightening pixels at a time.
  44. // 10 = lots of pixels brightening at a time.
  45. #define CHANCE_OF_TWINKLE 1
  46.  
  47. CRGB leds[NUM_LEDS];
  48. bool isRunning = false;
  49.  
  50. void setup() {
  51. // set the Time library to use Teensy 3.0's RTC to keep time
  52. setSyncProvider(getTeensy3Time);
  53.  
  54. // turn off the power supply
  55. pinMode(PS_ON_PIN, OUTPUT);
  56. digitalWrite(PS_ON_PIN, HIGH);
  57.  
  58. //FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  59. FastLED.addLeds<LED_TYPE, GARAGE_PIN, COLOR_ORDER>(leds, 0, NUM_GARAGE_LEDS).setCorrection(TypicalLEDStrip);
  60. FastLED.addLeds<LED_TYPE, HOUSE_PIN, COLOR_ORDER>(leds, NUM_GARAGE_LEDS, NUM_HOUSE_LEDS).setCorrection(TypicalLEDStrip);
  61. FastLED.setBrightness(MASTER_BRIGHTNESS);
  62.  
  63. InitPixelStates();
  64.  
  65. Alarm.alarmRepeat(17, 0, 0, startRunning);
  66. Alarm.alarmRepeat( 1, 0, 0, stopRunning);
  67.  
  68. if (hour() == 0 || hour() >= 17) {
  69. digitalWrite(PS_ON_PIN, LOW);
  70. isRunning = true;
  71. }
  72. }
  73.  
  74. void loop()
  75. {
  76. if (isRunning) {
  77. TwinkleMapPixels();
  78. FastLED.show();
  79. Alarm.delay(40);
  80. } else {
  81. Alarm.delay(1000);
  82. }
  83. }
  84.  
  85. enum { SteadyDim, GettingBrighter, GettingDimmerAgain };
  86. uint8_t PixelState[NUM_LEDS];
  87.  
  88. void InitPixelStates()
  89. {
  90. memset( PixelState, sizeof(PixelState), SteadyDim); // initialize all the pixels to SteadyDim.
  91. fill_solid( leds, NUM_LEDS, BASE_COLOR);
  92. }
  93.  
  94. void TwinkleMapPixels()
  95. {
  96. for( uint16_t i = 0; i < NUM_LEDS; i++) {
  97. if( PixelState[i] == SteadyDim) {
  98. // this pixels is currently: SteadyDim
  99. // so we randomly consider making it start getting brighter
  100. if( random16(1024) < CHANCE_OF_TWINKLE) {
  101. PixelState[i] = GettingBrighter;
  102. }
  103.  
  104. } else if( PixelState[i] == GettingBrighter ) {
  105. // this pixels is currently: GettingBrighter
  106. // so if it's at peak color, switch it to getting dimmer again
  107. if( leds[i] >= PEAK_COLOR ) {
  108. PixelState[i] = GettingDimmerAgain;
  109. } else {
  110. // otherwise, just keep brightening it:
  111. leds[i] += DELTA_COLOR_UP;
  112. }
  113.  
  114. } else { // getting dimmer again
  115. // this pixels is currently: GettingDimmerAgain
  116. // so if it's back to base color, switch it to steady dim
  117. if( leds[i] <= BASE_COLOR ) {
  118. leds[i] = BASE_COLOR; // reset to exact base color, in case we overshot
  119. PixelState[i] = SteadyDim;
  120. } else {
  121. // otherwise, just keep dimming it down:
  122. leds[i] -= DELTA_COLOR_DOWN;
  123. }
  124. }
  125. }
  126. }
  127.  
  128.  
  129. time_t getTeensy3Time()
  130. {
  131. return Teensy3Clock.get();
  132. }
  133.  
  134. void stopRunning() {
  135. isRunning = false;
  136. FastLED.showColor(CRGB::Black);
  137. digitalWrite(PS_ON_PIN, HIGH);
  138. }
  139.  
  140. void startRunning() {
  141. InitPixelStates();
  142. digitalWrite(PS_ON_PIN, LOW);
  143. isRunning = true;
  144. }
Add Comment
Please, Sign In to add comment