The Random Number Generator is working but I'm having trouble to change the modes, even if im already pressing the timerButton nothing happens. I believe this is more to the code because I already check the two buttons and it's fine. What should i change?
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address and dimensions (16x2)
const int rngButtonPin = 4; // RNG button pin
const int timerButtonPin = 2; // Timer button pin
const int buzzerPin = 8; // Buzzer pin
int rngButtonState = 5;
int lastRngButtonState = 3;
int timerButtonState = 0;
int lastTimerButtonState = 0;
unsigned long lastRngDebounceTime = 0;
unsigned long lastTimerDebounceTime = 0;
unsigned long debounceDelay = 50;
unsigned long startTime = 0;
void setup() {
lcd.begin(16, 2); // Set up the LCD's number of columns and rows
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
pinMode(rngButtonPin, INPUT);
pinMode(timerButtonPin, INPUT);
pinMode(buzzerPin, OUTPUT);
randomSeed(analogRead(0)); // Seed the random number generator
}
void loop() {
int randomNumber;
int rngReading = digitalRead(rngButtonPin);
int timerReading = digitalRead(timerButtonPin);
// RNG Button functionality
if (rngReading != lastRngButtonState) {
lastRngDebounceTime = millis();
}
if ((millis() - lastRngDebounceTime) > debounceDelay) {
if (rngReading != rngButtonState) {
rngButtonState = rngReading;
if (rngButtonState == HIGH) {
randomNumber = random(1, 12); // Generate a random number between 1 and 100
lcd.clear(); // Clear the LCD display
lcd.setCursor(0, 0); // Set cursor to the first line, first position
lcd.print("Random Number: ");
lcd.setCursor(0, 1); // Set cursor to the second line, first position
lcd.print(randomNumber); // Display the generated random number
}
}
}
lastRngButtonState = rngReading;
// Timer Button functionality
if (timerReading != lastTimerButtonState) {
lastTimerDebounceTime = millis();
}
if ((millis() - lastTimerDebounceTime) > debounceDelay) {
if (timerReading != timerButtonState) {
timerButtonState = timerReading;
if (timerButtonState == HIGH) {
startTime = millis(); // Record the start time for the timer
}
}
}
lastTimerButtonState = timerReading;
// Check if 30 seconds have passed since the timer button was pressed
if (startTime != 0 && millis() - startTime >= 30000) {
digitalWrite(buzzerPin, HIGH); // Activate the buzzer
delay(1000); // Buzzer on for 1 second
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
startTime = 0; // Reset the timer
}
}
Hello, it has two modes, the Random Number Generator and The Countdown Timer, when pressing the rngbutton it generates any random number between 1-12. I expected when pressing the timerbutton it shows the Countdown timer but nothing happens. The wirings seems to be fine because i tried switching pin 2 and pin 4 in the code.
The safest approach is to wire the button from the input pin to ground, and use pinMode(pin, INPUT_PULLUP); to keep the input HIGH when the button is not pressed.
Hello! I've figured it out. By analyzing my code, I didn't put an LCD Print code for the Countdown Timer`
here's the updated code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address and dimensions (16x2)
const int rngButtonPin = 4; // RNG button pin
const int timerButtonPin = 2; // Timer button pin
const int buzzerPin = 8; // Buzzer pin
int rngButtonState = 5;
int lastRngButtonState = 3;
int timerButtonState = 0;
int lastTimerButtonState = 0;
unsigned long lastRngDebounceTime = 0;
unsigned long lastTimerDebounceTime = 0;
unsigned long debounceDelay = 50;
unsigned long startTime = 0;
unsigned long countdownDuration = 30000; // Countdown duration in milliseconds
bool buzzerActivated = false;
void setup() {
lcd.begin(16, 2); // Set up the LCD's number of columns and rows
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
pinMode(rngButtonPin, INPUT);
pinMode(timerButtonPin, INPUT);
pinMode(buzzerPin, OUTPUT);
randomSeed(analogRead(0)); // Seed the random number generator
digitalWrite(buzzerPin, LOW); // Ensure buzzer starts off
}
void loop() {
int randomNumber;
int rngReading = digitalRead(rngButtonPin);
int timerReading = digitalRead(timerButtonPin);
digitalWrite(buzzerPin, LOW);
// RNG Button functionality
if (rngReading != lastRngButtonState) {
lastRngDebounceTime = millis();
}
if ((millis() - lastRngDebounceTime) > debounceDelay) {
if (rngReading != rngButtonState) {
rngButtonState = rngReading;
if (rngButtonState == HIGH) {
randomNumber = random(1, 12); // Generate a random number between 1 and 100
lcd.clear(); // Clear the LCD display
lcd.setCursor(0, 0); // Set cursor to the first line, first position
lcd.print("Random Number: ");
lcd.setCursor(0, 1); // Set cursor to the second line, first position
lcd.print(randomNumber); // Display the generated random number
}
}
}
lastRngButtonState = rngReading;
// Timer Button functionality
if (timerReading != lastTimerButtonState) {
lastTimerDebounceTime = millis();
}
if ((millis() - lastTimerDebounceTime) > debounceDelay) {
if (timerReading != timerButtonState) {
timerButtonState = timerReading;
if (timerButtonState == HIGH) {
startTime = millis(); // Record the start time for the timer
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Timer Countdown: ");
}
}
}
lastTimerButtonState = timerReading;
if (startTime != 0) {
unsigned long elapsedTime = millis() - startTime;
unsigned long remainingTime = countdownDuration - elapsedTime;
if (remainingTime >= 0) {
lcd.setCursor(0, 1);
lcd.print((remainingTime / 1000) % 60); // Display seconds
lcd.print("s ");
}
// Check if countdown is complete
if (elapsedTime >= countdownDuration) {
digitalWrite(buzzerPin, HIGH); // Activate the buzzer
delay(1000); // Buzzer on for 1 second
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
startTime = 0; // Reset the timer
lcd.clear(); // Clear the display after countdown completes
}
}
}
I have another problem though, The buzzer wont shut off at the start of the device, it supposed to only turn on when the countdown timer reached. what should i do?