/*
Wokwi | general
Fatin — 12/19/24 at 10:50 PM
ok so this is actually a circuit where i would
like to control my led to specific time (on/off),
but the coding itself is wrong. so i don't really
know what to do. help please
*/
#include <LiquidCrystal_I2C.h>
const int NUM_BTNS = 4;
const int BTN_PINS[] = {8, 9, 10, 11};
const int BUZZ_PIN = 12;
const int RELAY_PIN = 13;
int btnState[NUM_BTNS];
int oldBtnState[NUM_BTNS];
unsigned long delayTime = 1000;
unsigned long prevTime = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
// function returns which button was pressed, or 0 if none
int checkButtons() {
int btnPressed = 0;
for (int i = 0; i < NUM_BTNS; i++) {
// check each button
btnState[i] = digitalRead(BTN_PINS[i]);
if (btnState[i] != oldBtnState[i]) { // if it changed
oldBtnState[i] = btnState[i]; // remember state for next time
if (btnState[i] == LOW) { // was just pressed
btnPressed = i + 1;
Serial.print("Button ");
Serial.print(btnPressed);
Serial.println(" pressed");
//digitalWrite(LED_PINS[i], HIGH);
} else { // was just released
//digitalWrite(LED_PINS[i], LOW);
}
delay(20); // debounce
}
}
return btnPressed;
}
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
for (int i = 0; i < 4; i++) {
pinMode(BTN_PINS[i], INPUT_PULLUP);
}
pinMode(BUZZ_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
lcd.print("Ready");
delay(1000);
lcd.clear();
}
void loop() {
int btnNumber = checkButtons();
if (btnNumber) {
lcd.clear();
lcd.print(btnNumber);
}
/*
digitalWrite(RELAY_PIN, HIGH);
tone(BUZZ_PIN, 440, 100);
delay(1000);
digitalWrite(RELAY_PIN, LOW);
delay(1000);
*/
}