In: Electrical Engineering
Arduino code , using DS3231 library to set time/ date by keypad and print that on lcd. i would like to make user to set time and date from the keypad .
//************libraries**************//
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
//************************************//
LiquidCrystal_I2C lcd(0x3F,16,2); // Display I2C 16 x 2
RTC_DS1307 RTC;
//************Keys*****************//
int P1=6; // Keys SET options'
int P2=7; // Keys +
int P3=8; // Keys -
//************Variables**************//
int hours;
int mins;
int years;
int months;
int days;
int options = 0;
void setup()
{
lcd.begin();
lcd.backlight();
lcd.clear();
pinMode(P1,INPUT);
pinMode(P2,INPUT);
pinMode(P3,INPUT);
Serial.begin(9600);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// Set the date and time at compile time
RTC.adjust(DateTime(__DATE__, __TIME__));
}
// RTC.adjust(DateTime(__DATE__, __TIME__)); //removing "//" to
adjust the time
// The default display shows the date and time
int options=0;
}
void loop()
{
// check if you press the SET Keys and increase the options
index
if(digitalRead(P1))
{
options=options+1;
}
// in which subroutine should we go?
if (options==0)
{
DisplayDateTime(); // void DisplayDateTime
}
if (options==1)
{
DisplaySetHour();
}
if (options==2)
{
DisplaySetMinute();
}
if (options==3)
{
DisplaySetYear();
}
if (options==4)
{
DisplaySetMonth();
}
if (options==5)
{
DisplaySetDay();
}
if (options==6)
{
StoreAgg();
delay(500);
options=0;
}
delay(100);
}
void DisplayDateTime ()
{
// We show the current date and time
DateTime now = RTC.now();
lcd.setCursor(0, 1);
lcd.print("Hour:");
if (now.hour()<=9)
{
lcd.print("0");
}
lcd.print(now.hour(), DEC);
hours=now.hour();
lcd.print(":");
if (now.minute()<=9)
{
lcd.print("0");
}
lcd.print(now.minute(), DEC);
mins=now.minute();
lcd.print(":");
if (now.second()<=9)
{
lcd.print("0");
}
lcd.print(now.second(), DEC);
lcd.setCursor(0, 0);
lcd.print("Date: ");
if (now.day()<=9)
{
lcd.print("0");
}
lcd.print(now.day(), DEC);
days=now.day();
lcd.print("/");
if (now.month()<=9)
{
lcd.print("0");
}
lcd.print(now.month(), DEC);
months=now.month();
lcd.print("/");
lcd.print(now.year(), DEC);
years=now.year();
}
void DisplaySetHour()
{
// time setting
lcd.clear();
DateTime now = RTC.now();
if(digitalRead(P2)==HIGH)
{
if(hours==23)
{
hours=0;
}
else
{
hours=hours+1;
}
}
if(digitalRead(P3)==HIGH)
{
if(hours==0)
{
hours=23;
}
else
{
hours=hours-1;
}
}
lcd.setCursor(0,0);
lcd.print("Set time:");
lcd.setCursor(0,1);
lcd.print(hours,DEC);
delay(200);
}
void DisplaySetMinute()
{
// Setting the minutes
lcd.clear();
if(digitalRead(P2)==HIGH)
{
if (mins==59)
{
mins=0;
}
else
{
mins=mins+1;
}
}
if(digitalRead(P3)==HIGH)
{
if (mins==0)
{
mins=59;
}
else
{
mins=mins-1;
}
}
lcd.setCursor(0,0);
lcd.print("Set Minutes:");
lcd.setCursor(0,1);
lcd.print(mins,DEC);
delay(200);
}
void DisplaySetYear()
{
// setting the year
lcd.clear();
if(digitalRead(P2)==HIGH)
{
years=years+1;
}
if(digitalRead(P3)==HIGH)
{
years=years-1;
}
lcd.setCursor(0,0);
lcd.print("Set Year:");
lcd.setCursor(0,1);
lcd.print(years,DEC);
delay(200);
}
void DisplaySetMonth()
{
// Setting the month
lcd.clear();
if(digitalRead(P2)==HIGH)
{
if (months==12)
{
months=1;
}
else
{
months=months+1;
}
}
if(digitalRead(P3)==HIGH)
{
if (months==1)
{
months=12;
}
else
{
months=months-1;
}
}
lcd.setCursor(0,0);
lcd.print("Set Month:");
lcd.setCursor(0,1);
lcd.print(months,DEC);
delay(200);
}
void DisplaySetDay()
{
// Setting the day
lcd.clear();
if(digitalRead(P2)==HIGH)
{
if (days==31)
{
days=1;
}
else
{
days=days+1;
}
}
if(digitalRead(P3)==HIGH)
{
if (days==1)
{
days=31;
}
else
{
days=days-1;
}
}
lcd.setCursor(0,0);
lcd.print("Set Day:");
lcd.setCursor(0,1);
lcd.print(days,DEC);
delay(200);
}
void StoreAgg()
{
// Variable saving
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SAVING IN");
lcd.setCursor(0,1);
lcd.print("PROGRESS");
RTC.adjust(DateTime(years,months,days,hours,mins,0));
delay(200);
}