Components and supplies
Real Time Clock (RTC) DS3231
Jumper Wires
Electronic Wire (20AWG)
Arduino Mega 2560
(External) Junction Box
12mm Diffused Thin Digital RGB LED Pixels WS2801 (Strand of 50)
Half-size Breadboard
DC Power supply - 5V 10A
Christmas Tree
Female DC Power adapter - 2.1mm jack to screw terminal block
Insulating tape
Tools and machines
Soldering iron (generic)
Apps and platforms
Arduino IDE
Project description
Code
Christmas_tree
arduino
This is the MAIN CODE of your project. It is ready-to-use!
1/* ================= CHRISTMAS_TREE =================== 2 * Code for 150 WS2801 Digital RGB leds. 3 * Written by MARTINO GHISLENI. 4 * Based on the FASTLED library. 5 * September 2016 - September 2017 6 * 7 * NOTE: COLORS ARE SET VIA THE NAMED HTML WEB COLORS 8 * ==================================================== 9 */ 10 11#include <FastLED.h> 12#define NUM_LEDS 150 13#define DATA_PIN 2 // BLUE 14#define CLOCK_PIN 3 // GREEN 15CRGBArray<NUM_LEDS> leds; 16#include <Wire.h> 17#include <TimeLib.h> 18#include <DS1307RTC.h> 19 20 21void setup() { 22 FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS); 23} 24 25 26void loop() { 27 tmElements_t tm; 28 RTC.read(tm); 29 30 int timeSinceMidnight; 31 int Blocco17 = 1020; //17:00 -> 17 * 60 = 1020 minutes from midnight 32 int Blocco24 = 1439; //23:59 33 int Blocco00 = 0; //00:00 34 int Blocco08 = 480; //08:00 35 int Blocco16 = 1005; //16:45 36 37 timeSinceMidnight = (tm.Hour * 60) + tm.Minute; 38 39 //SWITCHING ON and from 17:00 to 24:00 40 if ( (timeSinceMidnight >= Blocco17) && (timeSinceMidnight <= Blocco24) ) { 41 Animazione1(); 42 Animazione2(); 43 Animazione1(); 44 Animazione3(); 45 } 46 47 //From MIDNIGHT to MORNING (00:00-08:00) 48 if ( (timeSinceMidnight >= Blocco00) && (timeSinceMidnight < Blocco08) ) { 49 Animazione1(); 50 Animazione2(); 51 Animazione1(); 52 Animazione3(); 53 } 54 55 //SWITCHING OFF and from 08:00 to 16:45 =check every 10'= 56 if ( (timeSinceMidnight >= Blocco08) && (timeSinceMidnight < Blocco16) ) { 57 Spegnimento(); 58 delay(600000); 59 } 60 61 //WAITING FOR 5PM from 16:45 to 17:00 =check every 1'= 62 if ( (timeSinceMidnight >= Blocco16) && (timeSinceMidnight < Blocco17) ) { 63 delay(60000); 64 } 65} 66 67 68/* =================== Animations =================== 69 * Rainbow (1); 70 * Red-Orange-Yellow-Green-Blue (2); 71 * Rainbow (1 again); 72 * Purple-Blue-LightBLue-Green-Yellow-Orange-Red (3). 73 * =================================================== 74 */ 75 76//==1== RAINBOW 77void Animazione1() { 78 static uint8_t hue=0; 79 for(int i = 0; i < NUM_LEDS; i++) { 80 leds(0,i).fill_rainbow(hue++); 81 FastLED.show(); 82 delay(300); //0,3 seconds 83 } 84 delay(10000); //10 seconds 85} 86 87//==2== LED RED-ORANGE-YELLOW-GREEN-BLUE 88void Animazione2() { 89 for(int q = 0; q < NUM_LEDS; q=q+5) { 90 leds[q] = CRGB::Red; 91 FastLED.show(); 92 delay(300); //0,3 seconds 93 } 94 for(int w = 1; w < NUM_LEDS; w=w+5) { 95 leds[w] = CRGB::OrangeRed; 96 FastLED.show(); 97 delay(300); 98 } 99 for(int e = 2; e < NUM_LEDS; e=e+5) { 100 leds[e] = CRGB::Yellow; 101 FastLED.show(); 102 delay(300); 103 } 104 for(int r = 3; r < NUM_LEDS; r=r+5) { 105 leds[r] = CRGB::Green; 106 FastLED.show(); 107 delay(300); 108 } 109 for(int t = 4; t < NUM_LEDS; t=t+5) { 110 leds[t] = CRGB::Blue; 111 FastLED.show(); 112 delay(300); 113 } 114 delay(10000); //10 seconds 115} 116 117//==3== LED PURPLE-BLUE-LIGHT BLUE-GREEN-YELLOW-ORANGE-RED 118void Animazione3() { 119 for(int q = 0; q < NUM_LEDS; q=q+7) { 120 leds[q] = CRGB::DarkMagenta; 121 FastLED.show(); 122 delay(300); 123 } 124 for(int w = 1; w < NUM_LEDS; w=w+7) { 125 leds[w] = CRGB::Blue; 126 FastLED.show(); 127 delay(300); 128 } 129 for(int e = 2; e < NUM_LEDS; e=e+7) { 130 leds[e] = CRGB::Cyan; 131 FastLED.show(); 132 delay(300); 133 } 134 for(int r = 3; r < NUM_LEDS; r=r+7) { 135 leds[r] = CRGB::Green; 136 FastLED.show(); 137 delay(300); 138 } 139 for(int t = 4; t < NUM_LEDS; t=t+7) { 140 leds[t] = CRGB::Yellow; 141 FastLED.show(); 142 delay(300); 143 } 144 for(int y = 5; y < NUM_LEDS; y=y+7) { 145 leds[y] = CRGB::OrangeRed; 146 FastLED.show(); 147 delay(300); 148 } 149 for(int u = 6; u < NUM_LEDS; u=u+7) { 150 leds[u] = CRGB::Red; 151 FastLED.show(); 152 delay(300); 153 } 154 delay(10000); //10 seconds 155} 156 157//SWITCHING OFF 158void Spegnimento() { 159 for(int i = 0; i < NUM_LEDS; i++) { 160 leds[i] = CRGB::Black; 161 FastLED.delay(300); 162 } 163} 164
Christmas_tree_UPDATED
arduino
Same animations and functions, but colors are MORE CALIBRATED because they are set with the HSV method. If you are not an expert user, learn more here: https://github.com/FastLED/FastLED/wiki/Pixel-reference#setting-hsv-colors-
1/* ========== CHRISTMAS_TREE UPDATED ============ 2 * Code for 150 WS2801 Digital RGB leds. 3 * Written by MARTINO GHISLENI. 4 * Based on the FASTLED library. 5 * September 2016 - 10 September 2017 6 * 7 * NOTE: COLORS ARE ENCODED WITH THE HSV METHOD. 8 * ============================================== 9 */ 10 11#include <FastLED.h> 12#define NUM_LEDS 150 13#define DATA_PIN 2 // BLUE 14#define CLOCK_PIN 3 // GREEN 15CRGB leds[NUM_LEDS]; 16#include <Wire.h> 17#include <TimeLib.h> 18#include <DS1307RTC.h> 19 20 21void setup() { 22 FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS); 23} 24 25 26void loop() { 27 tmElements_t tm; 28 RTC.read(tm); 29 30 int timeSinceMidnight; 31 int Blocco17 = 1020; //17:00 -> 17 * 60 = 1020 minutes from midnight 32 int Blocco24 = 1439; //23:59 33 int Blocco00 = 0; //00:00 34 int Blocco08 = 480; //08:00 35 int Blocco16 = 1005; //16:45 36 37 timeSinceMidnight = (tm.Hour * 60) + tm.Minute; 38 39 //SWITCHING ON and from 17:00 to 24:00 40 if ( (timeSinceMidnight >= Blocco17) && (timeSinceMidnight <= Blocco24) ) { 41 Animazione1(); 42 Animazione2(); 43 Animazione1(); 44 Animazione3(); 45 } 46 47 //From MIDNIGHT to MORNING (00:00-08:00) 48 if ( (timeSinceMidnight >= Blocco00) && (timeSinceMidnight < Blocco08) ) { 49 Animazione1(); 50 Animazione2(); 51 Animazione1(); 52 Animazione3(); 53 } 54 55 //SWITCHING OFF and from 08:00 to 16:45 =check every 10'= 56 if ( (timeSinceMidnight >= Blocco08) && (timeSinceMidnight < Blocco16) ) { 57 Spegnimento(); 58 delay(600000); 59 } 60 61 //WAITING FOR 5PM from 16:45 to 17:00 =check every 1'= 62 if ( (timeSinceMidnight >= Blocco16) && (timeSinceMidnight < Blocco17) ) { 63 delay(60000); 64 } 65} 66 67 68/* =================== Animations =================== 69 * FastLED Rainbow (1); 70 * Red-Orange-Yellow-Green-Blue (2); 71 * Rainbow (1 again); 72 * Purple-Blue-LightBLue-Green-Yellow-Orange-Red (3). 73 * =================================================== 74 */ 75 76//==1== FASTLED RAINBOW 77void Animazione1() { 78 static uint8_t hue=0; 79 for(int i = 0; i < NUM_LEDS; i++) { 80 fill_rainbow(leds, NUM_LEDS, hue++); 81 FastLED.show(); 82 delay(300); 83 } 84 delay(10000); //10 seconds 85} 86 87//==2== LED RED-ORANGE-YELLOW-GREEN-BLUE 88void Animazione2() { 89 for(int q = 0; q < NUM_LEDS; q=q+5) { 90 leds[q].setHue(0); //red 91 FastLED.show(); 92 delay(300); //0,3 seconds 93 } 94 for(int w = 1; w < NUM_LEDS; w=w+5) { 95 leds[w].setHue(10); //orange 96 FastLED.show(); 97 delay(300); 98 } 99 for(int e = 2; e < NUM_LEDS; e=e+5) { 100 leds[e].setHue(40); //yellow 101 FastLED.show(); 102 delay(300); 103 } 104 for(int r = 3; r < NUM_LEDS; r=r+5) { 105 leds[r].setHue(96); //green 106 FastLED.show(); 107 delay(300); 108 } 109 for(int t = 4; t < NUM_LEDS; t=t+5) { 110 leds[t].setHue(160); //blue 111 FastLED.show(); 112 delay(300); 113 } 114 delay(10000); //10 seconds 115} 116 117//==3== LED PINK-PURPLE-BLUE-GREEN-YELLOW-ORANGE-RED 118void Animazione3() { 119 for(int q = 0; q < NUM_LEDS; q=q+7) { 120 leds[q].setHue(235); //pink 121 FastLED.show(); 122 delay(300); 123 } 124 for(int w = 1; w < NUM_LEDS; w=w+7) { 125 leds[w].setHue(200); //purple 126 FastLED.show(); 127 delay(300); 128 } 129 for(int e = 2; e < NUM_LEDS; e=e+7) { 130 leds[e].setHue(160); //blue 131 FastLED.show(); 132 delay(300); 133 } 134 for(int r = 3; r < NUM_LEDS; r=r+7) { 135 leds[r].setHue(100); //green 136 FastLED.show(); 137 delay(300); 138 } 139 for(int t = 4; t < NUM_LEDS; t=t+7) { 140 leds[t].setHue(40); //yellow 141 FastLED.show(); 142 delay(300); 143 } 144 for(int y = 5; y < NUM_LEDS; y=y+7) { 145 leds[y].setHue(10); //orange 146 FastLED.show(); 147 delay(300); 148 } 149 for(int u = 6; u < NUM_LEDS; u=u+7) { 150 leds[u].setHue(0); //red 151 FastLED.show(); 152 delay(300); 153 } 154 delay(10000); //10 seconds 155} 156 157//SWITCHING OFF 158void Spegnimento() { 159 for(int i = 0; i < NUM_LEDS; i++) { 160 leds[i] = CRGB::Black; 161 FastLED.delay(300); 162 } 163} 164
RTC_set
arduino
This code copies the hour of your PC on the RTC connected to Arduino. Just upload this code on Arduino once, and the open the Serial Monitor to see the changes. Remeber to upload this BEFORE the main code, in order to make it run correctly!
1/* ==================== SET TIME on RTC ====================== 2 * Upload on Arduino and open the serial monitor. 3 * 4 * This file is taken from the Examples inluded in the Time 5 * library. 6 * Original code: https://github.com/PaulStoffregen/Time 7 * =========================================================== 8 */ 9 10 11#include <TimeLib.h> 12#include <Wire.h> 13#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t 14 15 16void setup() { 17 Serial.begin(9600); 18 while (!Serial) ; // Needed for Leonardo only 19 setSyncProvider(RTC.get); // the function to get the time from the RTC 20 if (timeStatus() != timeSet) 21 Serial.println("Unable to sync with the RTC"); 22 else 23 Serial.println("RTC has set the system time"); 24} 25 26void loop() 27{ 28 if (Serial.available()) { 29 time_t t = processSyncMessage(); 30 if (t != 0) { 31 RTC.set(t); // set the RTC and the system time to the received value 32 setTime(t); 33 } 34 } 35 digitalClockDisplay(); 36 delay(1000); 37} 38 39void digitalClockDisplay(){ 40 // digital clock display of the time 41 Serial.print(hour()); 42 printDigits(minute()); 43 printDigits(second()); 44 Serial.print(" "); 45 Serial.print(day()); 46 Serial.print(" "); 47 Serial.print(month()); 48 Serial.print(" "); 49 Serial.print(year()); 50 Serial.println(); 51} 52 53void printDigits(int digits){ 54 // utility function for digital clock display: prints preceding colon and leading 0 55 Serial.print(":"); 56 if(digits < 10) 57 Serial.print('0'); 58 Serial.print(digits); 59} 60 61/* code to process time sync messages from the serial port */ 62#define TIME_HEADER "T" // Header tag for serial time sync message 63 64unsigned long processSyncMessage() { 65 unsigned long pctime = 0L; 66 const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 67 68 if(Serial.find(TIME_HEADER)) { 69 pctime = Serial.parseInt(); 70 return pctime; 71 if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013) 72 pctime = 0L; // return 0 to indicate that the time is not valid 73 } 74 } 75 return pctime; 76} 77 78 79 80 81 82
RTC_read
arduino
Upload this code and open the Serial Monitor to check the time stored on the RTC connected to the Arduino.
1/* =================== READ TIME from RTC ==================== 2 * Upload on Arduino and open the serial monitor. 3 * 4 * This file is a customized version of the "ReadTest.ino", 5 * from the Examples inluded in the DS1307RTC library. 6 * Original code: https://github.com/PaulStoffregen/DS1307RTC 7 * =========================================================== 8 */ 9 10 11#include <Wire.h> 12#include <TimeLib.h> 13#include <DS1307RTC.h> 14 15void setup() { 16 Serial.begin(9600); 17 while (!Serial) ; // wait for serial 18 delay(200); 19 Serial.println("RTC DS3231 Read Time"); 20 Serial.println("--------------------"); 21} 22 23void loop() { 24 tmElements_t tm; 25 26 if (RTC.read(tm)) { 27 Serial.print("Time = "); 28 print2digits(tm.Hour); 29 Serial.write(':'); 30 print2digits(tm.Minute); 31 Serial.write(':'); 32 print2digits(tm.Second); 33 Serial.print(", Date = "); 34 Serial.print(tm.Day); 35 Serial.write('/'); 36 Serial.print(tm.Month); 37 Serial.write('/'); 38 Serial.print(tmYearToCalendar(tm.Year)); 39 Serial.println(); 40 } else { 41 if (RTC.chipPresent()) { 42 Serial.println("The DS3231 is stopped. Please run the SetTime"); 43 Serial.println("example to initialize the time and begin running."); 44 Serial.println(); 45 } else { 46 Serial.println("DS3231 read error! Please check the circuitry."); 47 Serial.println(); 48 } 49 delay(9000); 50 } 51 delay(1000); 52} 53 54void print2digits(int number) { 55 if (number >= 0 && number < 10) { 56 Serial.write('0'); 57 } 58 Serial.print(number); 59} 60
Christmas_tree_UPDATED
arduino
Same animations and functions, but colors are MORE CALIBRATED because they are set with the HSV method. If you are not an expert user, learn more here: https://github.com/FastLED/FastLED/wiki/Pixel-reference#setting-hsv-colors-
1/* ========== CHRISTMAS_TREE UPDATED ============ 2 * Code for 150 WS2801 Digital RGB leds. 3 * Written by MARTINO GHISLENI. 4 * Based on the FASTLED library. 5 * September 2016 - 10 September 2017 6 * 7 * NOTE: COLORS ARE ENCODED WITH THE HSV METHOD. 8 * ============================================== 9 */ 10 11#include <FastLED.h> 12#define NUM_LEDS 150 13#define DATA_PIN 2 // BLUE 14#define CLOCK_PIN 3 // GREEN 15CRGB leds[NUM_LEDS]; 16#include <Wire.h> 17#include <TimeLib.h> 18#include <DS1307RTC.h> 19 20 21void setup() { 22 FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS); 23} 24 25 26void loop() { 27 tmElements_t tm; 28 RTC.read(tm); 29 30 int timeSinceMidnight; 31 int Blocco17 = 1020; //17:00 -> 17 * 60 = 1020 minutes from midnight 32 int Blocco24 = 1439; //23:59 33 int Blocco00 = 0; //00:00 34 int Blocco08 = 480; //08:00 35 int Blocco16 = 1005; //16:45 36 37 timeSinceMidnight = (tm.Hour * 60) + tm.Minute; 38 39 //SWITCHING ON and from 17:00 to 24:00 40 if ( (timeSinceMidnight >= Blocco17) && (timeSinceMidnight <= Blocco24) ) { 41 Animazione1(); 42 Animazione2(); 43 Animazione1(); 44 Animazione3(); 45 } 46 47 //From MIDNIGHT to MORNING (00:00-08:00) 48 if ( (timeSinceMidnight >= Blocco00) && (timeSinceMidnight < Blocco08) ) { 49 Animazione1(); 50 Animazione2(); 51 Animazione1(); 52 Animazione3(); 53 } 54 55 //SWITCHING OFF and from 08:00 to 16:45 =check every 10'= 56 if ( (timeSinceMidnight >= Blocco08) && (timeSinceMidnight < Blocco16) ) { 57 Spegnimento(); 58 delay(600000); 59 } 60 61 //WAITING FOR 5PM from 16:45 to 17:00 =check every 1'= 62 if ( (timeSinceMidnight >= Blocco16) && (timeSinceMidnight < Blocco17) ) { 63 delay(60000); 64 } 65} 66 67 68/* =================== Animations =================== 69 * FastLED Rainbow (1); 70 * Red-Orange-Yellow-Green-Blue (2); 71 * Rainbow (1 again); 72 * Purple-Blue-LightBLue-Green-Yellow-Orange-Red (3). 73 * =================================================== 74 */ 75 76//==1== FASTLED RAINBOW 77void Animazione1() { 78 static uint8_t hue=0; 79 for(int i = 0; i < NUM_LEDS; i++) { 80 fill_rainbow(leds, NUM_LEDS, hue++); 81 FastLED.show(); 82 delay(300); 83 } 84 delay(10000); //10 seconds 85} 86 87//==2== LED RED-ORANGE-YELLOW-GREEN-BLUE 88void Animazione2() { 89 for(int q = 0; q < NUM_LEDS; q=q+5) { 90 leds[q].setHue(0); //red 91 FastLED.show(); 92 delay(300); //0,3 seconds 93 } 94 for(int w = 1; w < NUM_LEDS; w=w+5) { 95 leds[w].setHue(10); //orange 96 FastLED.show(); 97 delay(300); 98 } 99 for(int e = 2; e < NUM_LEDS; e=e+5) { 100 leds[e].setHue(40); //yellow 101 FastLED.show(); 102 delay(300); 103 } 104 for(int r = 3; r < NUM_LEDS; r=r+5) { 105 leds[r].setHue(96); //green 106 FastLED.show(); 107 delay(300); 108 } 109 for(int t = 4; t < NUM_LEDS; t=t+5) { 110 leds[t].setHue(160); //blue 111 FastLED.show(); 112 delay(300); 113 } 114 delay(10000); //10 seconds 115} 116 117//==3== LED PINK-PURPLE-BLUE-GREEN-YELLOW-ORANGE-RED 118void Animazione3() { 119 for(int q = 0; q < NUM_LEDS; q=q+7) { 120 leds[q].setHue(235); //pink 121 FastLED.show(); 122 delay(300); 123 } 124 for(int w = 1; w < NUM_LEDS; w=w+7) { 125 leds[w].setHue(200); //purple 126 FastLED.show(); 127 delay(300); 128 } 129 for(int e = 2; e < NUM_LEDS; e=e+7) { 130 leds[e].setHue(160); //blue 131 FastLED.show(); 132 delay(300); 133 } 134 for(int r = 3; r < NUM_LEDS; r=r+7) { 135 leds[r].setHue(100); //green 136 FastLED.show(); 137 delay(300); 138 } 139 for(int t = 4; t < NUM_LEDS; t=t+7) { 140 leds[t].setHue(40); //yellow 141 FastLED.show(); 142 delay(300); 143 } 144 for(int y = 5; y < NUM_LEDS; y=y+7) { 145 leds[y].setHue(10); //orange 146 FastLED.show(); 147 delay(300); 148 } 149 for(int u = 6; u < NUM_LEDS; u=u+7) { 150 leds[u].setHue(0); //red 151 FastLED.show(); 152 delay(300); 153 } 154 delay(10000); //10 seconds 155} 156 157//SWITCHING OFF 158void Spegnimento() { 159 for(int i = 0; i < NUM_LEDS; i++) { 160 leds[i] = CRGB::Black; 161 FastLED.delay(300); 162 } 163} 164
RTC_read
arduino
Upload this code and open the Serial Monitor to check the time stored on the RTC connected to the Arduino.
1/* =================== READ TIME from RTC ==================== 2 * Upload on Arduino and open the serial monitor. 3 * 4 * This file is a customized version of the "ReadTest.ino", 5 * from the Examples inluded in the DS1307RTC library. 6 * Original code: https://github.com/PaulStoffregen/DS1307RTC 7 * =========================================================== 8 */ 9 10 11#include <Wire.h> 12#include <TimeLib.h> 13#include <DS1307RTC.h> 14 15void setup() { 16 Serial.begin(9600); 17 while (!Serial) ; // wait for serial 18 delay(200); 19 Serial.println("RTC DS3231 Read Time"); 20 Serial.println("--------------------"); 21} 22 23void loop() { 24 tmElements_t tm; 25 26 if (RTC.read(tm)) { 27 Serial.print("Time = "); 28 print2digits(tm.Hour); 29 Serial.write(':'); 30 print2digits(tm.Minute); 31 Serial.write(':'); 32 print2digits(tm.Second); 33 Serial.print(", Date = "); 34 Serial.print(tm.Day); 35 Serial.write('/'); 36 Serial.print(tm.Month); 37 Serial.write('/'); 38 Serial.print(tmYearToCalendar(tm.Year)); 39 Serial.println(); 40 } else { 41 if (RTC.chipPresent()) { 42 Serial.println("The DS3231 is stopped. Please run the SetTime"); 43 Serial.println("example to initialize the time and begin running."); 44 Serial.println(); 45 } else { 46 Serial.println("DS3231 read error! Please check the circuitry."); 47 Serial.println(); 48 } 49 delay(9000); 50 } 51 delay(1000); 52} 53 54void print2digits(int number) { 55 if (number >= 0 && number < 10) { 56 Serial.write('0'); 57 } 58 Serial.print(number); 59} 60
RTC_set
arduino
This code copies the hour of your PC on the RTC connected to Arduino. Just upload this code on Arduino once, and the open the Serial Monitor to see the changes. Remeber to upload this BEFORE the main code, in order to make it run correctly!
1/* ==================== SET TIME on RTC ====================== 2 * 3 Upload on Arduino and open the serial monitor. 4 * 5 * This file is taken 6 from the Examples inluded in the Time 7 * library. 8 * Original code: https://github.com/PaulStoffregen/Time 9 10 * =========================================================== 11 */ 12 13 14#include 15 <TimeLib.h> 16#include <Wire.h> 17#include <DS1307RTC.h> // a basic DS1307 library 18 that returns time as a time_t 19 20 21void setup() { 22 Serial.begin(9600); 23 24 while (!Serial) ; // Needed for Leonardo only 25 setSyncProvider(RTC.get); // 26 the function to get the time from the RTC 27 if (timeStatus() != timeSet) 28 29 Serial.println("Unable to sync with the RTC"); 30 else 31 Serial.println("RTC 32 has set the system time"); 33} 34 35void loop() 36{ 37 if (Serial.available()) 38 { 39 time_t t = processSyncMessage(); 40 if (t != 0) { 41 RTC.set(t); 42 // set the RTC and the system time to the received value 43 setTime(t); 44 45 } 46 } 47 digitalClockDisplay(); 48 delay(1000); 49} 50 51void 52 digitalClockDisplay(){ 53 // digital clock display of the time 54 Serial.print(hour()); 55 56 printDigits(minute()); 57 printDigits(second()); 58 Serial.print(" "); 59 60 Serial.print(day()); 61 Serial.print(" "); 62 Serial.print(month()); 63 64 Serial.print(" "); 65 Serial.print(year()); 66 Serial.println(); 67} 68 69void 70 printDigits(int digits){ 71 // utility function for digital clock display: prints 72 preceding colon and leading 0 73 Serial.print(":"); 74 if(digits < 10) 75 76 Serial.print('0'); 77 Serial.print(digits); 78} 79 80/* code to process 81 time sync messages from the serial port */ 82#define TIME_HEADER "T" // 83 Header tag for serial time sync message 84 85unsigned long processSyncMessage() 86 { 87 unsigned long pctime = 0L; 88 const unsigned long DEFAULT_TIME = 1357041600; 89 // Jan 1 2013 90 91 if(Serial.find(TIME_HEADER)) { 92 pctime = Serial.parseInt(); 93 94 return pctime; 95 if( pctime < DEFAULT_TIME) { // check the value is a 96 valid time (greater than Jan 1 2013) 97 pctime = 0L; // return 0 to indicate 98 that the time is not valid 99 } 100 } 101 return pctime; 102} 103 104 105 106 107 108
Downloadable files
Christmas Tree Breadboard scheme
Christmas Tree Breadboard scheme
Christmas Tree Breadboard scheme
Christmas Tree Breadboard scheme
Comments
Only logged in users can leave comments
martinoghisleni
0 Followers
•0 Projects
Table of contents
Intro
12
0