Hey guys! How're you doing today?
I need help to set the clock in circuit, that is, using a couple pushbuttons to do it...
I already have the clock running.
I create the SetClock and SetDate functions but no matter what I can't figure out how to do it.
Can you help me out?
I also accept some coding tips

I used the SetClockButtonState variable (which can be 0, 1 or 2) to change from DisplayClock, SetClock, SetDate.
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include <LiquidCrystal.h>
#include "RTClib.h"
#define DS1307_I2C_ADDRESS 0x68 // each I2C object has a unique bus address, the DS1307 is 0x68
RTC_DS1307 RTC;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Create custom char
byte graus[8] = {
0b01110,
0b10001,
0b10001,
0b01110,
0b00000,
0b00000,
0b00000,
0b00000
};
unsigned char pin=0; // analog pin
int temp=0, Vin=0, j=0;
float minimum = (-424.0/6.25), samples[20];
const int SetClockButton = 9; // Pushbutton switch between: display clock/date/temp or set clock or set date
int SetClockButtonState = 0; // Current state of the button
const int ChangeClockButton = 8; // Pushbutton to choose what to change - second, minute, hour, day, month or year.
void setup ()
{
Serial.begin(57600);
Wire.begin();
RTC.begin();
lcd.begin(16, 2);
lcd.createChar(0, graus);
analogReference(INTERNAL);
// initialize the pushbutton pin as an input:
pinMode(SetClockButton, INPUT);
pinMode(ChangeClockButton, INPUT);
// following line sets the RTC to the date & time this sketch was compiled
// RTC.adjust(DateTime(__DATE__, __TIME__));
if (! RTC.isrunning())
{
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled only if clock is not running.
RTC.adjust(DateTime(__DATE__, __TIME__));
}
// Square-wave output
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x13); // sends 0x13 (hex) 00010011 (binary)
Wire.endTransmission();
}
void loop ()
{
while (SetClockButtonState == 0)
{
WriteClockOnLCD();
WriteDateOnLCD();
WriteWeekDayOnLCD();
WriteTemperatureOnLCD();
//PrintmiliVoltOnLCD();
if (digitalRead(SetClockButton) == 0)
{
delay(250);
if (digitalRead(SetClockButton) == 1)
{
lcd.clear();
SetClockButtonState=1;
}
}
}
while (SetClockButtonState == 1)
{
SetClock();
}
while (SetClockButtonState == 2)
{
SetDate();
}
}
// Print time on LCD
void WriteClockOnLCD ()
{
lcd.home();
DateTime now = RTC.now();
if (now.hour() < 10)
{
lcd.print('0');
lcd.print(now.hour(), DEC);
lcd.print(':');
}
else
{
lcd.print(now.hour(), DEC);
lcd.print(':');
}
if (now.minute() < 10)
{
lcd.print('0');
lcd.print(now.minute(), DEC);
lcd.print(':');
}
else
{
lcd.print(now.minute(), DEC);
lcd.print(':');
}
if (now.second() < 10)
{
lcd.print('0');
lcd.print(now.second(), DEC);
}
else
{
lcd.print(now.second(), DEC);
}
}
// Print date on LCD
void WriteDateOnLCD()
{
DateTime now = RTC.now();
lcd.setCursor(0,1);
if (now.day() < 10)
{
lcd.print('0');
lcd.print(now.day(), DEC);
lcd.print('/');
}
else
{
lcd.print(now.day(), DEC);
lcd.print('/');
}
switch(now.month())
{
case 1:
lcd.print("Jan/");
break;
case 2:
lcd.print("Feb/");
break;
case 3:
lcd.print("Mar/");
break;
case 4:
lcd.print("Apr/");
break;
case 5:
lcd.print("May/");
break;
case 6:
lcd.print("Jun/");
break;
case 7:
lcd.print("Jul/");
break;
case 8:
lcd.print("Aug/");
break;
case 9:
lcd.print("Sep/");
break;
case 10:
lcd.print("Oct/");
break;
case 11:
lcd.print("Nov/");
break;
case 12:
lcd.print("Dec/");
break;
default:
lcd.print("Err/");
}
lcd.print(now.year(), DEC);
}
// Print weekday on LCD
void WriteWeekDayOnLCD()
{
DateTime now = RTC.now();
lcd.setCursor(13, 1);
switch(now.dayOfWeek())
{
case 0:
lcd.print("Sun");
break;
case 1:
lcd.print("Mon");
break;
case 2:
lcd.print("Tue");
break;
case 3:
lcd.print("Wed");
break;
case 4:
lcd.print("Thu");
break;
case 5:
lcd.print("Fri");
break;
case 6:
lcd.print("Sat");
break;
default:
lcd.print("Err");
}
}
// Print temperature on LCD
void WriteTemperatureOnLCD()
{
Vin=0;
temp = 0;
j=0;
for(j=0 ; j<=19 ; j++)
{
samples[j] = (analogRead(pin)*(1100.0/1073.0));
Vin = Vin + samples[j];
}
temp = (Vin/20 /1024.0 * 1073.0 / 6.25) + minimum;
if (temp >=0 & temp <=9)
{
lcd.setCursor(11, 0);
lcd.print(" ");
lcd.print(temp, DEC);
}
if (temp >=10 & temp <=99)
{
lcd.setCursor(11, 0);
lcd.print(" ");
lcd.print(temp, DEC);
}
if (temp >=100 & temp <=150)
{
lcd.setCursor(11, 0);
lcd.print(temp, DEC);
}
if (temp >=-9 & temp <=-1)
{
lcd.setCursor(11, 0);
lcd.print(" ");
lcd.print(temp, DEC);
}
if (temp >=-99 & temp <=-10)
{
lcd.setCursor(11, 0);
lcd.print(temp, DEC);
}
lcd.setCursor(14, 0);
lcd.write((uint8_t)0);
lcd.print("C");
}
// Print temperature on LCD
void PrintmiliVoltOnLCD()
{
lcd.setCursor(8, 0);
lcd.print(Vin/20, DEC);
//lcd.print("mV");
}
void SetClock()
{
DateTime now = RTC.now();
//lcd.clear();
lcd.home();
lcd.print("SET CLOCK");
if (digitalRead(SetClockButton) == 0)
{
delay(250);
if (digitalRead(SetClockButton) == 1)
{
lcd.clear();
SetClockButtonState=2;
}
}
}
void SetDate()
{
DateTime now = RTC.now();
//lcd.clear();
lcd.home();
lcd.print("SET DATE");
if (digitalRead(SetClockButton) == 0)
{
delay(250);
if (digitalRead(SetClockButton) == 1)
{
lcd.clear();
SetClockButtonState=0;
}
}
}