The sketch covers the function on the post on May 9, 2013 displaying date, time and temperature and adds displaying weekday.
Usage
1. Use <RIGHT> keypad to enter set mode.
2. Use <RIGHT> to navigate on parameter to be modified.
3. Use <UP> to set value of the aimed parameter.
4. Use <DOWN> to set value of the aimed parameter.
5. Use <SELECT> to save date and time to RTC.
6. Use <LEFT> to leave set mode.
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
* 3) modified by Befun Hung on Jul. 31, 2013
* divide digitalClockDisplay() into dateDisplay(), weekdayDisplay(), timeDisplay() and temperatureDisplay()
* use time_t t to store the value of now()
* 4) modified by Befun Hung on Aug. 01, 2013
* adding keypadSetDateTime()
*/
#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);
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
char *dayOfWeek[] = {"", "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
int lcdKey = 0;
int adcKeyIn = 0;
time_t t;
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()
{
t = now();
lcdKey = readLCDButton();
if (lcdKey == btnRIGHT) {
keypadSetDateTime();
}
if (displayAtSecond != second(t)) {
digitalClockDisplay();
displayAtSecond = second(t);
}
}
int readLCDButton() {
adcKeyIn = analogRead(0);
delay(200);
// read the value from the sensor
// my buttons when read are centered at these values: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adcKeyIn > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adcKeyIn < 73) return btnRIGHT;
if (adcKeyIn < 237) return btnUP;
if (adcKeyIn < 415) return btnDOWN;
if (adcKeyIn < 623) return btnLEFT;
if (adcKeyIn < 882) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void digitalClockDisplay(){
// digital clock display of the time
dateDisplay();
weekdayDisplay();
timeDisplay();
temperatureDisplay();
}
void dateDisplay() {
lcd.setCursor(0,0);
lcd.print(year(t));
lcd.print('-');
if (month(t) < 10) {
lcd.print('0');
}
lcd.print(month(t));
lcd.print('-');
if (day(t) < 10) {
lcd.print('0');
}
lcd.print(day(t));
// lcd.print(' ');
}
void weekdayDisplay() {
lcd.setCursor(11,0);
lcd.print(dayOfWeek[weekday()]);
}
void timeDisplay() {
lcd.setCursor(0,1);
if (hour(t) < 10) {
lcd.print('0');
}
lcd.print(hour(t));
lcd.print(':');
if (minute(t) < 10) {
lcd.print('0');
}
lcd.print(minute(t));
lcd.print(':');
if (second(t) < 10) {
lcd.print('0');
}
lcd.print(second(t));
// lcd.print(' ');
}
void temperatureDisplay() {
int span = 10;
long aRead = 0;
unsigned long temp;
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');
}
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 keypadSetDateTime() {
int setYear=year(t), setMonth=month(t), setDay=day(t), setHour=hour(t), setMinute=minute(t), setSecond=second(t);
int setVariable=0, checkStatus=0;
dateDisplay();
timeDisplay();
while(true) {
lcdKey = readLCDButton();
switch(lcdKey) {
case btnNONE:
{
lcd.blink();
if (setVariable == 0) lcd.setCursor(3,0);
if (setVariable == 1) lcd.setCursor(6,0);
if (setVariable == 2) lcd.setCursor(9,0);
if (setVariable == 3) lcd.setCursor(1,1);
if (setVariable == 4) lcd.setCursor(4,1);
if (setVariable == 5) lcd.setCursor(7,1);
break;
}
case btnRIGHT:
{
setVariable = (setVariable + 1) % 6;
break;
}
case btnLEFT:
{
lcd.noBlink();
lcd.clear();
return;
}
case btnUP:
{
if (setVariable == 0) {
setYear = ((setYear + 1) % 100) + 2000;
lcd.setCursor(0,0);
lcd.print(setYear);
}
if (setVariable == 1) {
setMonth = (setMonth % 12) + 1;
lcd.setCursor(5,0);
if (setMonth < 10) {
lcd.print('0');
lcd.print(setMonth);
}
else {
lcd.print(setMonth);
}
}
if (setVariable == 2) {
setDay = (setDay % 31) + 1;
lcd.setCursor(8,0);
if (setDay < 10) {
lcd.print('0');
lcd.print(setDay);
}
else {
lcd.print(setDay);
}
}
if (setVariable == 3) {
setHour = (setHour + 1) % 24;
lcd.setCursor(0,1);
if (setHour < 10) {
lcd.print('0');
lcd.print(setHour);
}
else {
lcd.print(setHour);
}
}
if (setVariable == 4) {
setMinute = (setMinute + 1) % 60;
lcd.setCursor(3,1);
if (setMinute < 10) {
lcd.print('0');
lcd.print(setMinute);
}
else {
lcd.print(setMinute);
}
}
if (setVariable == 5) {
setSecond = (setSecond + 1) % 60;
lcd.setCursor(6,1);
if (setSecond < 10) {
lcd.print('0');
lcd.print(setSecond);
}
else {
lcd.print(setSecond);
}
}
break;
}
case btnDOWN:
{
if (setVariable == 0) {
setYear = ((setYear - 1) % 100) + 2000;
lcd.setCursor(0,0);
lcd.print(setYear);
}
if (setVariable == 1) {
setMonth = ((setMonth - 1) % 12);
if (setMonth == 0) {
setMonth = setMonth + 12;
}
lcd.setCursor(5,0);
if (setMonth < 10) {
lcd.print('0');
lcd.print(setMonth);
}
else {
lcd.print(setMonth);
}
}
if (setVariable == 2) {
setDay = ((setDay - 1) % 31);
if (setDay == 0) {
setDay = setDay + 31;
}
lcd.setCursor(8,0);
if (setDay < 10) {
lcd.print('0');
lcd.print(setDay);
}
else {
lcd.print(setDay);
}
}
if (setVariable == 3) {
setHour = (setHour - 1 + 24) % 24;
lcd.setCursor(0,1);
if (setHour < 10) {
lcd.print('0');
lcd.print(setHour);
}
else {
lcd.print(setHour);
}
}
if (setVariable == 4) {
setMinute = (setMinute - 1 + 60) % 60;
lcd.setCursor(3,1);
if (setMinute < 10) {
lcd.print('0');
lcd.print(setMinute);
}
else {
lcd.print(setMinute);
}
}
if (setVariable == 5) {
setSecond = (setSecond - 1 + 60) % 60;
lcd.setCursor(6,1);
if (setSecond < 10) {
lcd.print('0');
lcd.print(setSecond);
}
else {
lcd.print(setSecond);
}
}
break;
}
case btnSELECT:
{
if ((setMonth == 1 || setMonth == 3 || setMonth == 5 || setMonth == 7 || setMonth == 8 || setMonth == 10 || setMonth == 12) && setDay <= 31) checkStatus = 1;
if ((setMonth == 4 || setMonth == 6 || setMonth == 9 || setMonth == 11) && setDay <=30) checkStatus = 1;
if ((setMonth == 2 && (setYear % 4) == 0) && setDay <= 29) checkStatus = 1;
if ((setMonth == 2 && (setYear % 4) != 0) && setDay <= 28) checkStatus = 1;
if (checkStatus) {
setTime(setHour, setMinute, setSecond, setDay, setMonth, setYear);
RTC.set(now());
}
break;
}
}
}
}
No comments:
Post a Comment