#include <LiquidCrystal.h>
// Define pins for buttons
const int startButtonPin = 2;
const int stopButtonPin = 3;
const int resetButtonPin = 4;
// Define pins for LCD
const int rs = 7;
const int en = 8;
const int d4 = 9;
const int d5 = 10;
const int d6 = 11;
const int d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Define buzzer pin
const int buzzerPin = 6;
// Define activities and their corresponding timers (less than 10 seconds)
const char* activities[] = {"Activity 1", "Activity 2", "Activity 3"};
const int activityTimers[] = {5, 7, 9}; // in seconds
// Variables
int currentActivityIndex = -1;
unsigned long countdownEndTime = 0;
bool countdownActive = false;
bool stopButtonPressed = false;
bool buzzerActive = false;
void setup() {
// Initialize LCD
lcd.begin(16, 2);
lcd.print("Press Start");
// Initialize buttons
pinMode(startButtonPin, INPUT_PULLUP);
pinMode(stopButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
// Initialize buzzer
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Check for button presses
if (digitalRead(startButtonPin) == LOW && !countdownActive) {
startActivity();
}
if (digitalRead(stopButtonPin) == LOW) {
if (countdownActive) {
stopButtonPressed = true;
stopActivity();
} else if (buzzerActive) {
resetActivity();
}
}
if (digitalRead(resetButtonPin) == LOW) {
if (countdownActive || buzzerActive) {
buzzerActive = false;
resetActivity();
}
}
// Check if the stop button was released after being pressed
if (digitalRead(stopButtonPin) == HIGH && stopButtonPressed) {
stopButtonPressed = false;
countdownActive = false;
}
// Check countdown and update display
if (countdownActive) {
unsigned long currentTime = millis();
if (currentTime >= countdownEndTime) {
// Countdown reached 0
countdownActive = false;
buzzerActive = true;
lcd.clear();
lcd.print("Press Reset/Stop");
activateBuzzer();
} else {
// Display remaining time
int remainingTime = (countdownEndTime - currentTime) / 1000; // Convert to seconds
updateLCD(remainingTime);
}
}
}
void startActivity() {
currentActivityIndex = random(0, sizeof(activities) / sizeof(activities[0]));
int activityTimer = activityTimers[currentActivityIndex];
countdownEndTime = millis() + (activityTimer * 1000);
countdownActive = true;
buzzerActive = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(activities[currentActivityIndex]);
updateLCD(activityTimer);
noTone(buzzerPin);
}
void stopActivity() {
countdownActive = false;
noTone(buzzerPin);
lcd.clear();
lcd.print("Press Start");
}
void resetActivity() {
startActivity();
}
void updateLCD(int remainingTime) {
lcd.setCursor(0, 1);
lcd.print("Time: ");
if (remainingTime < 10) {
lcd.print("0");
}
lcd.print(remainingTime);
lcd.print("s");
}
void activateBuzzer() {
while ((buzzerActive && digitalRead(stopButtonPin) == HIGH) || (buzzerActive && digitalRead(resetButtonPin) == HIGH) ) {
tone(buzzerPin, 1000); // Activate the buzzer at 1000 Hz
}
}
test