Date and time is useful for many applications. Most visitors to my blog search for rtc stuff. The following sketch show a simple application using a LCD shield, a RTC Sensor shield and a LM35 temperature sensor to display date, time, weekday, and environmental temperature. To use the sketch, you need to download the Time library.
The sketch is a new version of the sketches post on Jun. 3, 2012 and May. 3, 2013 using Time.h library and showing weekday.
Output
Sketch
/*
* TimeRTC.pde
* example code illustrating Time library with Real Time Clock.
* the sketch works on Arduino IDE 1.05
* 1) modified by Befun Hung on Jul. 28, 2013
* changing the sequence to year, month, day, hour, minute, second
* adding the day of week
* almost same function as sketch on May 1, 2012
* 2) modified by Befun Hung on Jul. 29, 2013
* change display device to LCD Shield
* adding temperature readout from LM35
* the sketch does not contain delay() in the loop section
*/
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);
char *dayOfWeek[] = {"", "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
int potPin = 3; // change potPin value to 0, 1, 2 for A0, A1, A2 respectly
float temperature = 0;
int displayAtSecond;
void setup() {
lcd.begin(16,2);
lcd.print("*cheaphousetek*");
lcd.setCursor(0,1);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
lcd.print("Unable to sync");
else
lcd.print("Sync system time ");
displayAtSecond = second();
delay(2000);
lcd.clear();
}
void loop()
{
if (displayAtSecond != second()) {
digitalClockDisplay();
displayAtSecond = second();
}
}
void printTenths(long value) {
// prints a value of 123 as 12.3
lcd.setCursor(10,1);
lcd.print(value / 10);
lcd.setCursor(12,1);
lcd.print('.');
lcd.setCursor(13,1);
lcd.print(value % 10);
}
void digitalClockDisplay(){
// digital clock display of the time
int span = 10;
long aRead = 0;
unsigned long temp;
lcd.setCursor(0,0);
lcd.print(year());
lcd.print('-');
if (month() < 10) {
lcd.print('0');
}
lcd.print(month());
lcd.print('-');
if (day() < 10) {
lcd.print('0');
}
lcd.print(day());
lcd.print(' ');
lcd.setCursor(11,0);
lcd.print(dayOfWeek[weekday()]);
lcd.setCursor(0,1);
if (hour() < 10) {
lcd.print('0');
}
lcd.print(hour());
lcd.print(':');
if (minute() < 10) {
lcd.print('0');
}
lcd.print(minute());
lcd.print(':');
if (second() < 10) {
lcd.print('0');
}
lcd.print(second());
lcd.print(' ');
for (int i=0;i<span;i++) {
aRead = aRead + analogRead(potPin);
}
temperature = (aRead / span * 500.0 / 1024.0); // for other analog sensor change the constants
printTenths(long (temperature * 10));
lcd.setCursor(14,1);
lcd.print(char(223));
lcd.setCursor(15,1);
lcd.print('C');
}
Plz give DS1307RTC.h time.h library...
ReplyDeletehttps://playground.arduino.cc/Code/Time
ReplyDelete