This circuit is designed around an Arduino UNO microcontroller and includes an LCD Display (16x4 I2C) for output and a green button as an input device. The purpose of the circuit is to implement a count-up timer that starts counting when the green button is pressed and displays the elapsed time on the LCD display. The timer stops and resets when the button is pressed again. The LCD display communicates with the Arduino UNO via the I2C protocol, and the green button is connected to a digital input pin with a pull-down resistor to ensure a stable button state.
/*
* This Arduino sketch implements a count-up timer. The timer starts counting
* when the green button is pressed and displays the elapsed time on a 16x4
* I2C LCD display. The timer stops and resets when the button is pressed again.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// LCD display setup
LiquidCrystal_I2C lcd(0x27, 16, 4);
// Button pin
const int buttonPin = 2;
// Timer variables
unsigned long startTime = 0;
bool timerRunning = false;
void setup() {
// Initialize the LCD
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Count-Up Timer");
// Initialize the button pin
pinMode(buttonPin, INPUT);
}
void loop() {
// Read the button state
int buttonState = digitalRead(buttonPin);
// Check if the button is pressed
if (buttonState == HIGH) {
if (!timerRunning) {
// Start the timer
startTime = millis();
timerRunning = true;
} else {
// Stop and reset the timer
timerRunning = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Count-Up Timer");
}
// Debounce delay
delay(200);
}
// Update the timer display if running
if (timerRunning) {
unsigned long elapsedTime = millis() - startTime;
unsigned long seconds = (elapsedTime / 1000) % 60;
unsigned long minutes = (elapsedTime / 60000) % 60;
unsigned long hours = (elapsedTime / 3600000);
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(hours);
lcd.print(":");
lcd.print(minutes);
lcd.print(":");
lcd.print(seconds);
}
}
This code is designed to run on the Arduino UNO and controls the count-up timer functionality. It initializes the LCD display and the button pin, reads the button state, and updates the display with the elapsed time when the timer is running. The timer uses the millis()
function to keep track of the elapsed time and displays it in hours, minutes, and seconds. The code also includes a debounce delay to prevent false triggering of the button press.