Guest User

Untitled

a guest
Jan 18th, 2015
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.51 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. #include <FastLED.h>
  3. #include <Wire.h>
  4. #include <DS3232RTC.h>
  5. #include <Time.h>
  6. #include <Timezone.h>
  7. #include <Math.h>
  8.  
  9. // LCD setup
  10. LiquidCrystal lcd(10, 8, 9, 4, 5, 6, 7);
  11.  
  12. // FastLED setup
  13. #define NUM_LEDS 60
  14. #define DATA_PIN 3
  15. CRGB leds[NUM_LEDS];
  16. int LDRPin = A3; // Pin for LDR
  17.  
  18. // TimeZone setup
  19. TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120};     //Central European Summer Time
  20. TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60};       //Central European Standard Time
  21. Timezone CE(CEST, CET);
  22. TimeChangeRule *tcr;        //pointer to the time change rule, use to get the TZ abbrev
  23. time_t local;
  24. word mill = 0;
  25. unsigned long last_time = millis();
  26. uint8_t last_second;
  27.  
  28. const uint8_t map_pixel_time[80] = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  29.                                     30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  30.                                     40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  31.                                     50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  32.                                      0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
  33.                                     10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  34.                                     20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  35.                                     30, 31, 32, 33, 34, 35, 36, 37, 38, 39};
  36.  
  37.  
  38. void setup() {
  39.   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  40.  
  41.   setSyncProvider(RTC.get);   // the function to get the time from the RTC
  42.   setSyncInterval(1);
  43.   last_second = second(local);
  44.  
  45.   lcd.begin(16, 2);
  46. }
  47.  
  48. // Read temperature
  49. void read_temp() {
  50.   if ( second(local) == 0) {
  51.     Serial.print(RTC.temperature() / 4.);
  52.     Serial.write(176);
  53.     Serial.println("C");
  54.   }  
  55. }
  56.  
  57. void draw_second(int pos, uint8_t hue) {
  58.   leds[ map_pixel_time[pos - 2]] += CHSV( hue, 255, 255 - mill);
  59.   leds[ map_pixel_time[pos - 1]] += CHSV( hue, 255, 255); // mid
  60.   leds[ map_pixel_time[pos]] += CHSV( hue, 255, 255 ); // mid
  61.   leds[ map_pixel_time[pos + 1]] += CHSV( hue, 255, 255 ); // mid
  62.   leds[ map_pixel_time[pos + 2]] += CHSV( hue, 255, 255 ); // mid
  63.   leds[ map_pixel_time[pos + 3]] += CHSV( hue, 255, mill);
  64.  
  65. }
  66.  
  67. void draw_minute(int pos, uint8_t hue) {
  68.   leds[ map_pixel_time[pos - 1]] += CHSV( hue, 255, 255 - map(second(local), 0, 59, 0, 255) );
  69.   leds[ map_pixel_time[pos]] += CHSV( hue, 255, 255 );
  70.   leds[ map_pixel_time[pos + 1]] += CHSV( hue, 255, map(second(local), 0, 59, 0, 255) );
  71. }
  72.  
  73. void draw_hour(int pos, uint8_t hue) {
  74.   leds[ map_pixel_time[pos - 1]] += CHSV( hue, 255, 255 - map(minute(local), 0, 59, 0, 255) );
  75.   leds[ map_pixel_time[pos]] += CHSV( hue, 255, 255 );
  76.   leds[ map_pixel_time[pos+1]] += CHSV( hue, 255, 255 );
  77.   leds[ map_pixel_time[pos + 2]] += CHSV( hue, 255, map(minute(local), 0, 59, 0, 255) );
  78. }
  79.  
  80. void display_date_temp() {
  81.   lcd.setCursor(3, 0);
  82.   if ( day() < 10 ) { lcd.print("0"); }
  83.   lcd.print(day());
  84.   lcd.print(".");
  85.   if ( month() < 10 ) { lcd.print("0"); }
  86.   lcd.print(month());
  87.   lcd.print(".");
  88.   lcd.print(year());
  89.   lcd.setCursor(0, 1);
  90.   lcd.print(RTC.temperature() / 4.);
  91.   lcd.print("\337C ");
  92.  
  93.   if ( hour(local) < 10 ) { lcd.print("0"); }
  94.   lcd.print(hour(local));
  95.   lcd.print(":");
  96.   if ( minute(local) < 10 ) { lcd.print("0"); }
  97.   lcd.print(minute(local));
  98.   lcd.print(":");
  99.   if ( second(local) < 10 ) { lcd.print("0"); }
  100.   lcd.print(second(local));
  101.   lcd.display();
  102. }
  103.  
  104. void loop() {
  105.   float pos_min_for_hour = floor(minute(local)/12);
  106.   int pos_hour = (hour(local) % 12) * 5 + pos_min_for_hour;
  107.  
  108.   FastLED.clear();
  109.   local = CE.toLocal(now(), &tcr);
  110.  
  111.   float gradient = minute(local)*99/59 + (hour(local)%3)*100;
  112.   uint8_t second_hue = map( gradient, 0, 299, 0, 255);
  113.   uint8_t minute_hue = second_hue + 35;
  114.   uint8_t hour_hue = minute_hue + 35;
  115.  
  116.   if (second(local) != last_second) {
  117.     mill = 0;
  118.     last_time = millis();
  119.     last_second = second(local);
  120.   } else {
  121.     mill = map( (millis() - last_time) % 1000, 0, 1000, 0, 255);
  122.   }  
  123.  
  124.   draw_hour(pos_hour+10, hour_hue);
  125.   draw_minute(minute(local)+10, minute_hue);
  126.   draw_second(second(local)+10, second_hue);
  127.  
  128.   if ( (hour(local) >= 1) & (hour(local) <= 4) ) {
  129.     analogWrite(11, 0);
  130.     FastLED.setBrightness(0); // energy saver
  131.     FastLED.show();
  132.   } else {
  133.     analogWrite(11, 96);
  134.     int led_brightness = 255 * ( 1 - exp(- analogRead(LDRPin)/800. * 5.));
  135.     if (led_brightness < 20) { led_brightness = 20; }
  136.     FastLED.setBrightness(led_brightness);
  137.     FastLED.show();
  138.   }
  139.  
  140.   display_date_temp();
  141. }
Advertisement
Add Comment
Please, Sign In to add comment