#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
const int buttonPin = 8; // The pin where the button is connected
const int buttonPin2 = 9;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
unsigned long nextMillis = 0;
const long interval = 1000;
unsigned char sekunnit = 0;
unsigned char minuutit = 0;
unsigned char tunnit = 0;
bool settingTime = false; // Flag to indicate if we are in time setting mode
void setup() {
lcd.begin(16, 2);
lcd.print("Valtteri");
lcd.setCursor(0, 1);
lcd.print(" : :");
tulosta_luku_EtuNollalla(6, 1, sekunnit);
tulosta_luku_EtuNollalla(3, 1, minuutit);
tulosta_luku_EtuNollalla(0, 1, tunnit);
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with internal pull-up resistor
pinMode(buttonPin2, INPUT_PULLUP);
}
void loop() {
unsigned long currentMillis = millis();
// Check if it's time to update the clock
if (currentMillis >= nextMillis && !settingTime) {
nextMillis = currentMillis + interval;
sekunnit++;
if (sekunnit > 59) {
sekunnit = 0;
minuutit++;
if (minuutit > 59) {
minuutit = 0;
tunnit++;
if (tunnit > 23) {
tunnit = 0;
}
tulosta_luku_EtuNollalla(0, 1, tunnit);
}
tulosta_luku_EtuNollalla(3, 1, minuutit);
}
tulosta_luku_EtuNollalla(6, 1, sekunnit);
}
// Check if the button is pressed to enter time setting mode
if (digitalRead(buttonPin) == LOW) {
settingTime = true;
setTime();
}
else {
settingTime = false;
}
}
void tulosta_luku_EtuNollalla(unsigned char x, unsigned char y, unsigned char luku) {
lcd.setCursor(x, y);
if (luku < 10)
lcd.print('0');
lcd.print(luku);
}
void setTime() {
static unsigned long lastDebounceTime = 0; // Initialize debounce variables
static unsigned long debounceDelay = 50;
if (millis() - lastDebounceTime > debounceDelay) { // Check if it's time for debouncing
if (digitalRead(buttonPin2) == LOW) { // Check if the button is pressed
minuutit++; // Increment minutes
if (minuutit >= 60) { // Check if minutes reach or exceed 60
minuutit = 0;
tunnit++;
if (tunnit >= 24) { // Check if hours reach or exceed 24
tunnit = 0;
}
}
tulosta_luku_EtuNollalla(3, 1, minuutit); // Update the display with the new minutes
tulosta_luku_EtuNollalla(0, 1, tunnit); // Update the display with the new hours
lastDebounceTime = millis(); // Save the last debounce time
}
}
}