Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. #include <TimeLib.h>
  2. #include <Adafruit_NeoPixel.h>
  3. #ifdef __AVR__
  4. #include <avr/power.h>
  5. #endif
  6.  
  7. #define TIME_HEADER 'T' // Header tag for serial time sync message
  8. #define TIME_REQUEST 7 // ASCII bell character requests a time sync message
  9.  
  10. // Which pin on the Arduino is connected to the NeoPixels?
  11. // On a Trinket or Gemma we suggest changing this to 1
  12. #define PIN 6
  13.  
  14. // How many NeoPixels are attached to the Arduino?
  15. #define NUMPIXELS 24
  16.  
  17. #define SEC_COL 20
  18. #define HOUR_COL 20
  19. #define MIN_COL 20
  20. #define STATIC_COL 10
  21.  
  22. // When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
  23. // Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
  24. // example for more information on possible values.
  25. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  26.  
  27. int delayval = 5; // delay for half a second
  28. //setting the colors
  29. char Colors[] = {'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'g', 'b', 'w', 'p', 'b', 'b', 'b', 'e', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w'};
  30.  
  31.  
  32. void setup() {
  33. Serial.begin(9600);
  34. setSyncProvider(requestSync); //set function to call when sync required
  35. Serial.println("Waiting for sync message");
  36.  
  37.  
  38. // put your setup code here, to run once:
  39.  
  40. // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  41. #if defined (__AVR_ATtiny85__)
  42. if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  43. #endif
  44. // End of trinket special code
  45.  
  46. pixels.begin(); // This initializes the NeoPixel library.
  47. }
  48.  
  49. void loop(){
  50. if (Serial.available()) {
  51. processSyncMessage();
  52. }
  53. /*
  54. if (timeStatus()!= timeNotSet) {
  55. digitalClockDisplay();
  56. }
  57. */
  58. int milis = ((millis()%1000)*24/1000);
  59. float m = (float) (minute()%60);
  60. float s = (float) (second()%60);
  61. float h = (float) (hour()%12);
  62.  
  63. float minutes = (m + (s/60))*24/60;
  64. float hours = h + (m/60);
  65. hours = hours/0.5;
  66.  
  67. int m2 = round(minutes)%24;
  68. int m1 = m2-1;
  69. if (m2 == 0){
  70. m1 = 23;
  71. }
  72.  
  73. int h2 = round(hours)%24;
  74. int h1 = h2-1;
  75. if (h2 == 0){
  76. h1 = 23;
  77. }
  78.  
  79. resetpixels();
  80. if (h2 != 23 && h1 != 23 && m1 != 23 && m2 != 23){
  81. pixels.setPixelColor(23, pixels.Color(STATIC_COL,STATIC_COL,STATIC_COL));
  82. }
  83. if (h2 != 0 && h1 != 0 && m1 != 0 && m2 != 0){
  84. pixels.setPixelColor(0, pixels.Color(STATIC_COL,STATIC_COL,STATIC_COL));
  85. }
  86. if (m2 == h2){
  87. pixels.setPixelColor(m1, pixels.Color(MIN_COL,0,MIN_COL));
  88. pixels.setPixelColor(m2, pixels.Color(MIN_COL,0,MIN_COL));
  89. } else {
  90. pixels.setPixelColor(h1, pixels.Color(HOUR_COL,0,0));
  91. pixels.setPixelColor(h2, pixels.Color(HOUR_COL,0,0));
  92. pixels.setPixelColor(m1, pixels.Color(0,0,MIN_COL));
  93. pixels.setPixelColor(m2, pixels.Color(0,0,MIN_COL));
  94. }
  95. pixels.setPixelColor(milis, pixels.Color(0,SEC_COL,0));
  96. pixels.show();
  97. delay(5);
  98. }
  99.  
  100. void resetpixels(){
  101. for(int i=0;i<NUMPIXELS;i++){
  102. pixels.setPixelColor(i, pixels.Color(0, 0, 0));
  103. pixels.show(); // This sends the updated pixel color to the hardware
  104. }
  105. pixels.show();
  106. }
  107.  
  108. void digitalClockDisplay(){
  109. // digital clock display of the time
  110. Serial.print(hour());
  111. printDigits(minute());
  112. printDigits(second());
  113. Serial.print(" ");
  114. Serial.print(day());
  115. Serial.print(" ");
  116. Serial.print(month());
  117. Serial.print(" ");
  118. Serial.print(year());
  119. Serial.println();
  120. }
  121.  
  122. void printDigits(int digits){
  123. // utility function for digital clock display: prints preceding colon and leading 0
  124. Serial.print(":");
  125. if(digits < 10)
  126. Serial.print('0');
  127. Serial.print(digits);
  128. }
  129.  
  130.  
  131. void processSyncMessage() {
  132. unsigned long pctime;
  133. const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
  134.  
  135. if(Serial.find(TIME_HEADER)) {
  136. pctime = Serial.parseInt();
  137. if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
  138. setTime(pctime); // Sync Arduino clock to the time received on the serial port
  139. }
  140. }
  141. }
  142.  
  143. time_t requestSync()
  144. {
  145. Serial.write(TIME_REQUEST);
  146. return 0; // the time will be sent later in response to serial mesg
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement