#include <LiquidCrystal.h>
// Constants for LCD Pins
const int rs = 7; // Register Select pin
const int en = 8; // Enable pin
const int d4 = 12; // Data pin 4
const int d5 = 11; // Data pin 5
const int d6 = 10; // Data pin 6
const int d7 = 9; // Data pin 7
// Constants for Time
const long hour = 3600000; // 3600000 milliseconds in an hour
const long minute = 60000; // 60000 milliseconds in a minute
const long second = 1000; // 1000 milliseconds in a second
// Push buttons
int pbA = 6; // SWITCH BETWEEN CLOCK AND TEMPERATURE MODE
int pbB = 5; // Increments hours & switches between C and F
int pbC = 4; // Decrements hours
int pbD = 3; // Increments minutes
int pbE = 2; // Decrements minutes
// LCD setup
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Variables
int g = 1; // a variable that toggles between Celsius & Fahrenheit
int i = 0; // a variable to toggle between clock and temp
long addition = 0; // this will be used as an addition to add the time
// Enums for Mode and Unit
enum Mode { CLOCK, TEMPERATURE };
enum Unit { CELSIUS, FAHRENHEIT };
Mode currentMode = CLOCK;
Unit currentUnit = CELSIUS;
// Variable to indicate display mode (0 for clock, 1 for temperature)
int displayMode = 0;
// Constants for NTC Thermistor
const int thermistorPin = A0; // Analog pin connected to thermistor
const float seriesResistor = 10000.0; // Resistance of series resistor (10k ohm)
const float thermistorNominal = 10000.0; // Resistance at 25 degrees C
const float temperatureNominal = 25.0; // Nominal temperature
const int bValue = 3950; // Beta value for the thermistor
// Setting up push buttons as INPUT_PULLUP
void setup() {
pinMode(pbA, INPUT_PULLUP);
pinMode(pbB, INPUT_PULLUP);
pinMode(pbC, INPUT_PULLUP);
pinMode(pbD, INPUT_PULLUP);
pinMode(pbE, INPUT_PULLUP);
Serial.begin(57600);
lcd.begin(16, 2); // Set the lcd column and row
}
// Function to display fixed temperature value
void temp() {
if (digitalRead(pbB) == LOW) {
currentUnit = (currentUnit == CELSIUS) ? FAHRENHEIT : CELSIUS; // Toggle temperature unit
delay(100);
}
int adcValue = analogRead(thermistorPin);
float resistance = seriesResistor / ((1023.0 / adcValue) - 1);
float steinhart;
steinhart = resistance / thermistorNominal; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= bValue; // 1/B * ln(R/Ro)
steinhart += 1.0 / (temperatureNominal + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // Convert to Celsius
float tempC = steinhart;
float tempF = (tempC * 9.0) / 5.0 + 32.0;
printTemperature(tempC, tempF); // Display temperature on LCD
delay(500);
}
// Function to handle time
void time() {
long timeNow = millis() + hour; // Get current time in milliseconds and add the initial offset
Serial.print(addition); // Print the current time adjustment
Serial.println();
timeNow = timeNow + addition; // Adjust the current time with the accumulated addition
Serial.print(timeNow); // Print the adjusted time
Serial.println();
int hours = timeNow / hour; // Calculate the hours from the adjusted time
Serial.println(hours);
Serial.println(hours * hour);
if (hours >= 24) {
hours = hours % 24; // Ensure hours are within 24-hour format
} else if (hours < 0) {
addition = 3600000 * 24; // Reset the addition to keep time positive
Serial.print("hello");
}
if (digitalRead(pbB) == LOW) {
addition = addition + hour; // Increment hours if button B is pressed
} else if (digitalRead(pbC) == LOW) {
addition = addition - hour; // Decrement hours if button C is pressed
}
timeNow = timeNow + addition; // Adjust the time again based on button input
Serial.print(timeNow);
Serial.println();
if (digitalRead(pbD) == LOW) {
addition = addition + minute; // Increment minutes if button D is pressed
} else if (digitalRead(pbE) == LOW) {
addition = addition - minute; // Decrement minutes if button E is pressed
}
int minutes = (timeNow % hour) / minute; // Calculate the minutes from the adjusted time
int seconds = (timeNow % minute) / second; // Calculate the seconds from the adjusted time
printTime(hours, minutes, seconds); // Display time on LCD
delay(500);
}
// Function to print temperature on LCD
void printTemperature(float tempC, float tempF) {
lcd.clear(); // Clear the LCD screen
if (displayMode == 1) { // Check if the display mode is set to temperature
lcd.setCursor(0, 0);
if (currentUnit == CELSIUS) {
lcd.print("TempC ");
lcd.setCursor(6, 0);
lcd.print(tempC); // Print the temperature in Celsius
Serial.print("TempC ");
Serial.print(tempC);
} else {
lcd.print("TempF ");
lcd.setCursor(6, 0);
lcd.print(tempF); // Print the temperature in Fahrenheit
Serial.print("TempF ");
Serial.print(tempF);
}
}
}
// Function to print time on LCD
void printTime(int hours, int minutes, int seconds) {
lcd.clear(); // Clear the LCD screen
if (displayMode == 0) { // Check if the display mode is set to time
lcd.setCursor(0, 0);
lcd.print("HH:MM:SS ");
lcd.setCursor(0, 1);
if (hours < 10) {
lcd.print("0");
}
lcd.print(hours);
lcd.print(":");
if (minutes < 10) {
lcd.print("0");
}
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(seconds); // Print hours, minutes, and seconds on LCD
}
}
// Main loop
void loop() {
if (digitalRead(pbA) == LOW) {
if (i == 1) {
i = 0;
currentMode = CLOCK; // Toggle between clock and temperature modes
} else if (i == 0) {
i = 1;
currentMode = TEMPERATURE;
}
delay(100);
}
if (currentMode == CLOCK) {
time(); // Execute time-related functionality
displayMode = 0; // Set display mode to clock
} else if (currentMode == TEMPERATURE) {
temp(); // Execute temperature-related functionality
displayMode = 1; // Set display mode to temperature
}
delay(1000);
}