Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * TimeSerial.pde
- * example code illustrating Time library set through serial port messages.
- *
- * Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
- * you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013
- T1357041600
- *
- * A Processing example sketch to automatically send the messages is included in the download
- * On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone)
- *
- *LEDText code by Aaron Liddiment
- *
- */
- #include <TimeLib.h>
- #include <FastLED.h>
- #include <LEDMatrix.h>
- #include <LEDText.h>
- #include <FontMatrise.h>
- #define LED_PIN 2
- #define COLOR_ORDER GRB
- #define CHIPSET WS2812B
- #define MATRIX_WIDTH 92
- #define MATRIX_HEIGHT 8
- #define MATRIX_TYPE HORIZONTAL_ZIGZAG_MATRIX
- cLEDMatrix<MATRIX_WIDTH, MATRIX_HEIGHT, MATRIX_TYPE> leds;
- cLEDText ScrollingMsg;
- const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
- const unsigned char TxtDemo1[] = { EFFECT_SCROLL_LEFT " Countdown to Houston SUPERBOWL LI "
- };
- const unsigned char TxtDemo2[] = {hour()};
- /////// test scroll
- #define TIME_HEADER "T" // Header tag for serial time sync message
- #define TIME_REQUEST 7 // ASCII bell character requests a time sync message
- void printDigits(int digits){
- // utility function for digital clock display: prints preceding colon and leading 0
- Serial.print(":");
- if(digits < 10)
- Serial.print('0');
- Serial.print(digits);
- }
- void processSyncMessage() {
- unsigned long pctime;
- // const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
- if(Serial.find(TIME_HEADER)) {
- pctime = Serial.parseInt();
- if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
- setTime(pctime); // Sync Arduino clock to the time received on the serial port
- }
- }
- }
- time_t requestSync()
- {
- Serial.write(TIME_REQUEST);
- return 0; // the time will be sent later in response to serial mesg
- }
- void digitalClockDisplay(){
- // digital clock display of the time
- Serial.print(hour());
- printDigits(minute());
- printDigits(second());
- Serial.print(" ");
- Serial.print(day());
- Serial.print(" ");
- Serial.print(month());
- Serial.print(" ");
- Serial.print(year());
- Serial.println();
- }
- void setup() {
- Serial.begin(9600);
- while (!Serial) ; // Needed for Leonardo only
- pinMode(13, OUTPUT);
- setSyncProvider( requestSync); //set function to call when sync required
- Serial.println("Waiting for sync message");
- //////////////////////////////////
- FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds[0], leds.Size());
- FastLED.setBrightness(64);
- FastLED.clear(true);
- delay(500);
- FastLED.showColor(CRGB::Red);
- delay(1000);
- FastLED.showColor(CRGB::Lime);
- delay(1000);
- FastLED.showColor(CRGB::Blue);
- delay(1000);
- FastLED.showColor(CRGB::White);
- delay(1000);
- FastLED.show();
- ScrollingMsg.SetFont(MatriseFontData);
- ScrollingMsg.Init(&leds, leds.Width(), ScrollingMsg.FontHeight() + 1, 0, 0);
- ScrollingMsg.SetText((unsigned char *)TxtDemo1, sizeof(TxtDemo1) - 1);
- ScrollingMsg.SetTextColrOptions(COLR_RGB | COLR_SINGLE, 0xff, 0xff, 0xff);
- ////////////////////////////////////
- }
- void loop(){
- if (Serial.available()) {
- processSyncMessage();
- }
- if (timeStatus()!= timeNotSet) {
- digitalClockDisplay();
- }
- if (timeStatus() == timeSet) {
- digitalWrite(13, HIGH); // LED on if synced
- } else {
- digitalWrite(13, LOW); // LED off if needs refresh
- }
- if (ScrollingMsg.UpdateText() == -1)
- ScrollingMsg.SetText((unsigned char *)TxtDemo1, sizeof(TxtDemo1) - 1);
- else
- FastLED.show();
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment