]> git.piffa.net Git - sketchbook_andrea_bak/blob - advanced_projects/LCD_clock/display_time_1/display_time_1.ino
Initial Commit
[sketchbook_andrea_bak] / advanced_projects / LCD_clock / display_time_1 / display_time_1.ino
1 // Chapter 7: Arduino Alarm Clock 
2 // An alarm clock that uses the Adafruit Industries DS1307 RTC Breakout board 
3 // and a 16 x 2 Parallel LCD Display 
4 #include <Wire.h> // I2C Wire Library for communicating with the DS1307 RTC 
5 #include "RTClib.h" // Date and time functions for the DS1307 RTC connected 
6 #include <LiquidCrystal.h> // Display functions for the LCD Display 
7 RTC_DS1307 rtc; // Create a realtime clock called rtc 
8 LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Create an LCD called lcd 
9
10 DateTime now; // Time object
11
12 void setup () { 
13   Wire.begin(); // Enables the communication for the LCD 
14   rtc.begin(); // Enables the RTC 
15   lcd.begin(16, 2); // Enables the LCD 
16
17   lcd.print(" It's Alive!"); // Print a message, centered, to the LCD to confirm it's working 
18   delay(500); // Wait a moment so we can read it 
19   lcd.clear(); // Clear the LCD 
20
21 void loop(){ 
22   now = rtc.now(); // Get the current time 
23   // Refresh the display 
24   updateDisplay();
25
26
27 void updateDisplay(){ 
28   int h = now.hour(); // Get the hours right now and store them in an integer called  h 
29     int m = now.minute(); // Get the minutes right now and store them in an integer  called m 
30     int s = now.second(); // Get the seconds right now and store them in an integer  called s 
31     lcd.setCursor(0, 0); // Set the cursor at the column zero, upper row... 
32   lcd.print(" The time is: "); // ...with spaces to clear characters from setting  alarm. 
33     lcd.setCursor(4, 1); // Move the cursor to column four, lower row 
34   if (h<10){ // Add a zero, if necessary, as above 
35     lcd.print(0); 
36   } 
37   lcd.print(h); // Display the current hour 
38   lcd.setCursor(6, 1); // Move to the next column 
39   lcd.print(":"); // And print the colon 
40   lcd.setCursor(7, 1); // Move to the next column 
41   if (m<10){ // Add a zero, if necessary, as above 
42     lcd.print(0); 
43   } 
44   lcd.print(m); // Display the current minute 
45   lcd.setCursor(9, 1); // Move to the next column 
46   lcd.print(":"); // And print the colon 
47   lcd.setCursor(10, 1); // Move to the next column 
48   if (s<10){ // Add a zero, if necessary, as above 
49     lcd.print(0); 
50   } 
51   lcd.print(s); // Display the current second 
52 }
53