Advertisement
EmptySet5150

Final - Binary Clock - 3 RGB Strips

Jul 28th, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // RGB LED Strip Setup
  2. #define NUM_LEDS_Sec 6 // Number of leds on DATA_PIN_Sec
  3. #define NUM_LEDS_Min 6 // Number of leds on DATA_PIN_Min
  4. #define NUM_LEDS_Hrs 5 // Number of leds on DATA_PIN_Hrs
  5. #define DATA_PIN_Sec 9 // The Data line for NUM_LEDS_Sec
  6. #define DATA_PIN_Min 8 // The Data line for NUM_LEDS_Min
  7. #define DATA_PIN_Hrs 7 // The Data line for NUM_LEDS_Hrs
  8.  
  9. // Setup For FastLED.h
  10. #include <FastLED.h> // FastLED.h libary for rgb strip leds
  11. CRGB SecLedStrip[NUM_LEDS_Sec];
  12. CRGB MinLedStrip[NUM_LEDS_Min];
  13. CRGB HrsLedStrip[NUM_LEDS_Hrs];
  14.  
  15. // Clock
  16. uint8_t hrs;  
  17. uint8_t mins;
  18. uint8_t secs;
  19.  
  20. // Initialize Buttons Used To Advance The Time
  21. int minButtonPin = 10;
  22. int hrsButtonPin = 11;
  23. int minButtonState = 0;
  24. int hrsButtonState = 0;
  25.  
  26. void setup()
  27. {
  28.   // Initialize LED Strips
  29.   FastLED.addLeds<NEOPIXEL, DATA_PIN_Sec>(SecLedStrip, NUM_LEDS_Sec);
  30.   FastLED.addLeds<NEOPIXEL, DATA_PIN_Min>(MinLedStrip, NUM_LEDS_Min);
  31.   FastLED.addLeds<NEOPIXEL, DATA_PIN_Hrs>(HrsLedStrip, NUM_LEDS_Hrs);
  32.  
  33.   // Set ButtonPins To Input And Pull Up
  34.   pinMode(minButtonPin, INPUT_PULLUP);
  35.   pinMode(hrsButtonPin, INPUT_PULLUP);
  36. }
  37.  
  38. void loop()
  39. {
  40.   static uint32_t tick_tm = millis();  // 1 Second Tick Mark
  41.   // Buttons To Advance The Time - Hours Or Minutes
  42.   hrsButtonState = digitalRead(hrsButtonPin);
  43.   minButtonState = digitalRead(minButtonPin);
  44.   if (minButtonState == LOW){
  45.     mins++;
  46.     delay(250);
  47.   }
  48.   if (hrsButtonState == LOW){
  49.     hrs++;
  50.     delay(250);
  51.   }
  52.   // One Second Tick
  53.   if (millis() - tick_tm > 1000UL){
  54.     tick_tm += 1000UL; // Advance by Exactly 1 Second For Precision Timekeeping
  55.     // Increment The Time
  56.     secs++;
  57.     if (secs > 59) { secs = 0; mins++; }
  58.     if (mins > 59) { mins = 0; hrs++; }
  59.     if (hrs  > 23)   hrs  = 0; // Simple Wrap Clock
  60.   }
  61.   // Constantly Update The Display
  62.   updateLedStrip( SecLedStrip, secs, NUM_LEDS_Sec, CRGB::Blue );
  63.   updateLedStrip( MinLedStrip, mins, NUM_LEDS_Min, CRGB::Green );
  64.   updateLedStrip( HrsLedStrip, hrs,  NUM_LEDS_Hrs, CRGB::Red );
  65.   FastLED.show();
  66. }
  67.  
  68.   // Update One LED Strip
  69. void updateLedStrip( struct CRGB *led_strip, uint8_t val, uint8_t num_leds, CRGB::HTMLColorCode color )
  70. {
  71.   for (uint8_t i=0; i < num_leds; i++)
  72.     // One Bit Per LED In The Strip
  73.     led_strip[i] = (val & (1 << i)) ? color : CRGB::Black;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement