Advertisement
ErnestoGrimes

Arduino clock RTC

May 25th, 2018
2,915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <FastLED.h>
  2. #include <DS1302.h>
  3.  
  4. // How many leds are in the strip?
  5. #define NUM_LEDS 12
  6.  
  7. // Data pin that led data will be written out over
  8. #define DATA_PIN 3
  9.  
  10. // Clock pin only needed for SPI based chipsets when not using hardware SPI
  11. //#define CLOCK_PIN 8
  12.  
  13. const int kCePin   = 4;  // Chip Enable
  14. const int kIoPin   = 5;  // Input/Output
  15. const int kSclkPin = 6;  // Serial Clock
  16.  
  17. CRGB leds[NUM_LEDS];
  18.  
  19. // Create a DS1302 object.    //object for vma301 RTC
  20. DS1302 rtc(kCePin, kIoPin, kSclkPin);
  21.  
  22. int seconds;
  23. int secondsColor=95;
  24. int minutes;
  25. int minutesColor=0;
  26. int hours;
  27. int hoursColor=160;
  28.  
  29.  
  30. unsigned long previousMillis;
  31. unsigned long updateMillis;
  32. unsigned long runs;
  33.  
  34. void setup() {
  35.     // sanity check delay - allows reprogramming if accidently blowing power w/leds
  36.     delay(1000);
  37.  
  38.       // Uncomment one of the following lines for your leds arrangement.
  39.       // FastLED.addLeds<TM1803, DATA_PIN, RGB>(leds, NUM_LEDS);
  40.       // FastLED.addLeds<TM1804, DATA_PIN, RGB>(leds, NUM_LEDS);
  41.       // FastLED.addLeds<TM1809, DATA_PIN, RGB>(leds, NUM_LEDS);
  42.       ///FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
  43.       //FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
  44.       FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
  45.       // FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  46.       // FastLED.addLeds<APA104, DATA_PIN>(leds, NUM_LEDS);
  47.       // FastLED.addLeds<WS2811_400, DATA_PIN, RGB>(leds, NUM_LEDS);
  48.       // FastLED.addLeds<GW6205, DATA_PIN, RGB>(leds, NUM_LEDS);
  49.       // FastLED.addLeds<GW6205_400, DATA_PIN, RGB>(leds, NUM_LEDS);
  50.       // FastLED.addLeds<UCS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
  51.       // FastLED.addLeds<UCS1903B, DATA_PIN, RGB>(leds, NUM_LEDS);
  52.  
  53.       // FastLED.addLeds<WS2801, RGB>(leds, NUM_LEDS);
  54.       // FastLED.addLeds<SM16716, RGB>(leds, NUM_LEDS);
  55.       // FastLED.addLeds<LPD8806, RGB>(leds, NUM_LEDS);
  56.       // FastLED.addLeds<P9813, RGB>(leds, NUM_LEDS);
  57.       // FastLED.addLeds<APA102, RGB>(leds, NUM_LEDS);
  58.       // FastLED.addLeds<DOTSTAR, RGB>(leds, NUM_LEDS);
  59.      
  60.       // FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  61.       // FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  62.       // FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  63.       // FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  64.       // FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  65.       // FastLED.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  66.  
  67.       FastLED.setBrightness(5);
  68.       pinMode(7, INPUT);
  69.       Serial.begin(19200);
  70.  
  71.       leds[0].setHSV(150,255,255);    //light the 12 oclick led for 2 sec
  72.       FastLED.show();
  73.       delay(2000);
  74.       FastLED.clear();
  75.      
  76.       //setfromRTC();
  77.      
  78. }
  79.  
  80. void loop() {
  81.   //incrementTime();
  82.   setfromRTC();  
  83.   showSeconds(seconds);
  84.   showMinutes(minutes);
  85.   showHours(hours);
  86.   //periodicUpdate(180);
  87.   //printTime();
  88.   //tick(1000);
  89.   FastLED.show();
  90.   FastLED.clear();
  91. //  runs++;
  92. //  Serial.println(millis()/runs);
  93. }
  94.  
  95. void showSeconds(int a){            // map secounds to led
  96.   int b = map(a,0,59,0,NUM_LEDS-1);
  97.   leds[b].setHSV(secondsColor,255,255);
  98. //  Serial.print(a);
  99. //  Serial.print(" ");
  100. //  Serial.println(b);
  101. }
  102.  
  103. void showMinutes(int a){            //map minutes to led
  104.   int b = map(a,0,59,0,NUM_LEDS-1);
  105.   leds[b].setHSV(minutesColor,255,255);
  106. //  Serial.print(a);
  107. //  Serial.print(" ");
  108. //  Serial.println(b);
  109. }
  110.  
  111. void showHours(int a){              //map hours to led
  112.   if (NUM_LEDS > 12) {
  113.     a = map(a,0,23,0,NUM_LEDS-1);
  114.     leds[a].setHSV(hoursColor,255,255);
  115.   }
  116.   else
  117.   {
  118.   a = a%12;
  119.   leds[a].setHSV(hoursColor,255,255);
  120. }
  121. }
  122.  
  123. void incrementTime(){               //roll seconds > minutes > hours
  124.   //seconds++;
  125.   if (seconds > 59){
  126.     seconds = 0;
  127.     minutes++;
  128.   }
  129.     if (minutes > 59){
  130.     minutes = 0;
  131.     hours++;
  132.   }
  133.     if (hours > 23){
  134.     hours = 0;
  135.   }
  136. }
  137.  
  138. void printTime(){                   //print current time to serial
  139.   Time t = rtc.time();
  140.   Serial.print(hours);
  141.   Serial.print(":");
  142.   Serial.print(minutes);
  143.   Serial.print(":");
  144.   Serial.print(seconds);
  145.   Serial.print("  ");
  146.   Serial.print(t.hr);
  147.   Serial.print(":");
  148.   Serial.print(t.min);
  149.   Serial.print(":");
  150.   Serial.println(t.sec);
  151. }
  152.  
  153. void tick(int a){                        // increment secounds based on input millis
  154.     if ( millis() - previousMillis >= a) {
  155.     seconds++;
  156.     previousMillis = millis();
  157.     //Serial.println(previousMillis);
  158.   }
  159. }
  160.  
  161. void setfromRTC(){                  // get hours minutes and seconds from RTC
  162.     Time t = rtc.time();
  163.     hours = t.hr;
  164.     minutes = t.min;
  165.     seconds = t.sec;
  166. }
  167.  
  168. void periodicUpdate(int a){       //update from RTC at interval in minutes
  169.   a = a * 60;
  170.   if ( millis() - updateMillis >= a) {
  171.     setfromRTC();
  172.     updateMillis = millis();
  173.   }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement