#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 DS1307;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int menuButtonPin = 2;
const int increaseButtonPin = 3;
const int decreaseButtonPin = 4;
enum MenuState { CLOCK, SET_HOUR, SET_MINUTE, SET_SECOND };
MenuState currentMenuState = CLOCK;
void setup() {
lcd.backlight();
lcd.begin(16, 2);
sensors.begin(); // Start up the library
Serial.begin(9600);
DateTime datumCas = DS1307.now();
if (!DS1307.begin()) {
lcd.println("Hodiny nejsou pripojeny !");
for (;;) ;
}
if (!DS1307.isrunning()) {
lcd.println("Hodiny nejsou spusteny! Spoustim nyni..");
DS1307.adjust(DateTime(__DATE__, __TIME__));
}
pinMode(menuButtonPin, INPUT_PULLUP);
pinMode(increaseButtonPin, INPUT_PULLUP);
pinMode(decreaseButtonPin, INPUT_PULLUP);
}
void adjustTime(DateTime &datumCas, int hours, int minutes, int seconds) {
DateTime newTime = datumCas + TimeSpan(0, hours, minutes, seconds);
DS1307.adjust(newTime);
}
void loop() {
DateTime datumCas = DS1307.now();
//print the temperature in Celsius
sensors.requestTemperatures();
Serial.print("Temperature: ");
Serial.print(sensors.getTempCByIndex(0));
int temp=sensors.getTempCByIndex(0);
Serial.print((char)176);//shows degrees character
Serial.print("C | ");
// Check button presses
if (digitalRead(menuButtonPin) == LOW) {
// Toggle the menu state
currentMenuState = static_cast<MenuState>((currentMenuState + 1) % 4);
delay(200); // Add a debounce delay
}
if (digitalRead(increaseButtonPin) == LOW) {
// Depending on the menu state, adjust the time
switch (currentMenuState) {
case SET_HOUR:
adjustTime(datumCas, 1, 0, 0); // Increase the hour by 1
break;
case SET_MINUTE:
adjustTime(datumCas, 0, 1, 0); // Increase the minute by 1
break;
case SET_SECOND:
adjustTime(datumCas, 0, 0, 1); // Increase the second by 1
break;
default:
break;
}
delay(200); // Add a debounce delay
}
if (digitalRead(decreaseButtonPin) == LOW) {
// Depending on the menu state, decrease the time
switch (currentMenuState) {
case SET_HOUR:
adjustTime(datumCas, -1, 0, 0); // Decrease the hour by 1
break;
case SET_MINUTE:
adjustTime(datumCas, 0, -1, 0); // Decrease the minute by 1
break;
case SET_SECOND:
adjustTime(datumCas, 0, 0, -1); // Decrease the second by 1
break;
default:
break;
}
delay(200); // Add a debounce delay
}
// Display the time
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(datumCas.hour());
lcd.print(':');
lcd.print(datumCas.minute());
lcd.print(':');
lcd.print(datumCas.second());
lcd.setCursor(13, 1);
lcd.print("T");
lcd.setCursor(14, 1);
lcd.println(temp);
// Display the menu state
lcd.setCursor(0, 1);
switch (currentMenuState) {
case CLOCK:
lcd.print("Clock Mode ");
break;
case SET_HOUR:
lcd.print("Set Hour ");
break;
case SET_MINUTE:
lcd.print("Set Minute ");
break;
case SET_SECOND:
lcd.print("Set Second ");
break;
}
delay(1000);
}