#include <LiquidCrystal.h> // Library for LCD
#include <SevSeg.h> // Library for 7-segment display
// Pin definitions
const int temperaturePin = A0; // Analog pin for temperature sensor
const int buttonPin = 2; // Digital pin for push button
const int ledPin = 13; // Digital pin for LED
const int shiftDataPin = 3; // Digital pin for shift register data
const int shiftClockPin = 4; // Digital pin for shift register clock
const int shiftLatchPin = 5; // Digital pin for shift register latch
// Variables
float temperature = 0.0; // Variable to store temperature
int mode = 0; // Variable to store current mode
unsigned long previousMillis = 0; // Variable to store previous millis for timing
const long interval = 1000; // Interval for temperature reading
// Initialize LCD and 7-segment display
LiquidCrystal lcd(8, 9, 10, 11, 12, 6, 7); // LCD pins
SevSeg sevSeg; // 7-segment display
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(shiftDataPin, OUTPUT); // Set shift register data pin as output
pinMode(shiftClockPin, OUTPUT); // Set shift register clock pin as output
pinMode(shiftLatchPin, OUTPUT); // Set shift register latch pin as output
lcd.begin(16, 2); // Initialize LCD: 16 columns, 2 rows
sevSeg.begin(HARDWARE, 1, 2, 3, 4, 5, 6, 7, 8); // Initialize 7-segment display
}
void loop() {
unsigned long currentMillis = millis(); // Get current time
// Read temperature every interval
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Update previous time
temperature = readTemperature(); // Read temperature
updateDisplay(); // Update display with temperature
}
// Check for button press
if (digitalRead(buttonPin) == LOW) {
mode = (mode + 1) % 3; // Cycle through modes
updateMode(); // Update mode
}
}
// Function to read temperature
float readTemperature() {
int rawValue = analogRead(temperaturePin); // Read analog value
float voltage = rawValue * (5.0 / 1023.0); // Convert to voltage
float tempC = (voltage - 0.5) * 100.0; // Convert to temperature in Celsius
return tempC;
}
// Function to update display
void updateDisplay() {
lcd.clear(); // Clear LCD
lcd.setCursor(0, 0); // Set cursor to first row
lcd.print("Temp: "); // Print temperature label
lcd.print(temperature); // Print temperature value
lcd.print(" C"); // Print units
sevSeg.setNumber((int)temperature); // Set 7-segment display value to integer part of temperature
sevSeg.refreshDisplay(); // Refresh 7-segment display
}
// Function to update mode
void updateMode() {
switch (mode) {
case 0:
digitalWrite(ledPin, LOW); // Turn off LED
break;
case 1:
digitalWrite(ledPin, HIGH); // Turn on LED
break;
case 2:
// Additional mode functionality
break;
}
}