Guest User

Scrollling Countdown

a guest
Dec 28th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. /*
  2. * TimeSerial.pde
  3. * example code illustrating Time library set through serial port messages.
  4. *
  5. * Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
  6. * you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013
  7. T1357041600
  8. *
  9. * A Processing example sketch to automatically send the messages is included in the download
  10. * On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone)
  11. *
  12. *LEDText code by Aaron Liddiment
  13. *
  14. */
  15.  
  16. #include <TimeLib.h>
  17. #include <FastLED.h>
  18.  
  19. #include <LEDMatrix.h>
  20. #include <LEDText.h>
  21. #include <FontMatrise.h>
  22.  
  23. #define LED_PIN 2
  24. #define COLOR_ORDER GRB
  25. #define CHIPSET WS2812B
  26.  
  27. #define MATRIX_WIDTH 92
  28. #define MATRIX_HEIGHT 8
  29. #define MATRIX_TYPE HORIZONTAL_ZIGZAG_MATRIX
  30.  
  31. cLEDMatrix<MATRIX_WIDTH, MATRIX_HEIGHT, MATRIX_TYPE> leds;
  32.  
  33. cLEDText ScrollingMsg;
  34.  
  35. const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
  36.  
  37.  
  38. const unsigned char TxtDemo1[] = { EFFECT_SCROLL_LEFT " Countdown to Houston SUPERBOWL LI "
  39. };
  40.  
  41. const unsigned char TxtDemo2[] = {hour()};
  42.  
  43.  
  44.  
  45. /////// test scroll
  46.  
  47. #define TIME_HEADER "T" // Header tag for serial time sync message
  48. #define TIME_REQUEST 7 // ASCII bell character requests a time sync message
  49.  
  50.  
  51.  
  52.  
  53.  
  54. void printDigits(int digits){
  55. // utility function for digital clock display: prints preceding colon and leading 0
  56. Serial.print(":");
  57. if(digits < 10)
  58. Serial.print('0');
  59. Serial.print(digits);
  60. }
  61.  
  62.  
  63. void processSyncMessage() {
  64. unsigned long pctime;
  65. // const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
  66.  
  67. if(Serial.find(TIME_HEADER)) {
  68. pctime = Serial.parseInt();
  69. if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
  70. setTime(pctime); // Sync Arduino clock to the time received on the serial port
  71. }
  72. }
  73. }
  74.  
  75. time_t requestSync()
  76. {
  77. Serial.write(TIME_REQUEST);
  78. return 0; // the time will be sent later in response to serial mesg
  79. }
  80.  
  81.  
  82.  
  83. void digitalClockDisplay(){
  84. // digital clock display of the time
  85. Serial.print(hour());
  86. printDigits(minute());
  87. printDigits(second());
  88. Serial.print(" ");
  89. Serial.print(day());
  90. Serial.print(" ");
  91. Serial.print(month());
  92. Serial.print(" ");
  93. Serial.print(year());
  94. Serial.println();
  95. }
  96.  
  97. void setup() {
  98.  
  99. Serial.begin(9600);
  100. while (!Serial) ; // Needed for Leonardo only
  101. pinMode(13, OUTPUT);
  102. setSyncProvider( requestSync); //set function to call when sync required
  103. Serial.println("Waiting for sync message");
  104.  
  105.  
  106. //////////////////////////////////
  107. FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds[0], leds.Size());
  108. FastLED.setBrightness(64);
  109. FastLED.clear(true);
  110. delay(500);
  111. FastLED.showColor(CRGB::Red);
  112. delay(1000);
  113. FastLED.showColor(CRGB::Lime);
  114. delay(1000);
  115. FastLED.showColor(CRGB::Blue);
  116. delay(1000);
  117. FastLED.showColor(CRGB::White);
  118. delay(1000);
  119. FastLED.show();
  120.  
  121. ScrollingMsg.SetFont(MatriseFontData);
  122. ScrollingMsg.Init(&leds, leds.Width(), ScrollingMsg.FontHeight() + 1, 0, 0);
  123. ScrollingMsg.SetText((unsigned char *)TxtDemo1, sizeof(TxtDemo1) - 1);
  124. ScrollingMsg.SetTextColrOptions(COLR_RGB | COLR_SINGLE, 0xff, 0xff, 0xff);
  125. ////////////////////////////////////
  126.  
  127.  
  128. }
  129.  
  130. void loop(){
  131. if (Serial.available()) {
  132. processSyncMessage();
  133. }
  134. if (timeStatus()!= timeNotSet) {
  135. digitalClockDisplay();
  136. }
  137. if (timeStatus() == timeSet) {
  138. digitalWrite(13, HIGH); // LED on if synced
  139. } else {
  140. digitalWrite(13, LOW); // LED off if needs refresh
  141. }
  142.  
  143. if (ScrollingMsg.UpdateText() == -1)
  144. ScrollingMsg.SetText((unsigned char *)TxtDemo1, sizeof(TxtDemo1) - 1);
  145. else
  146. FastLED.show();
  147. delay(10);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment