#include <LiquidCrystal_I2C.h>
const int IO_PINS[] = {12, 11, 10, 9, 8, 7};
const int BTN_CTRL_PINS[] = {5, 4};
const int BTN_IN_PIN = 6;
const int NUM_BTNS = 2;
int btnState[NUM_BTNS];
int oldBtnState[NUM_BTNS] = {1, 1};
LiquidCrystal_I2C lcd(0x27, 16, 2);
int checkButtons() {
int btnNumber = 0;
for (int i = 0; i < NUM_BTNS; i++) {
// check each button
btnState[i] = digitalRead(BTN_CTRL_PINS[i]);
if (btnState[i] != oldBtnState[i]) { // if it changed
oldBtnState[i] = btnState[i]; // remember state for next time
btnNumber = i + 1;
Serial.print("Button ");
Serial.print(btnNumber);
if (btnState[i] == LOW) { // was just pressed
Serial.println(" pressed.");
} else {
btnNumber = -1; // was just released
Serial.println(" released.");
}
delay(20); // debounce
}
}
return btnNumber;
}
void showSplash() {
lcd.setCursor(5, 0);
lcd.print("Hello");
lcd.setCursor(5, 1);
lcd.print("world!");
delay(2000);
lcd.clear();
}
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
for (int i = 0; i < 6; i++) {
pinMode(IO_PINS[i], OUTPUT);
}
pinMode(BTN_CTRL_PINS[0], INPUT_PULLUP);
pinMode(BTN_CTRL_PINS[1], INPUT_PULLUP);
pinMode(BTN_IN_PIN, INPUT);
showSplash();
}
void loop() {
bool flag = false;
int btnNumber = checkButtons();
for (int i = 0; i < 6; i++) {
digitalWrite(IO_PINS[i], HIGH);
if (digitalRead(BTN_IN_PIN) == HIGH) {
flag = true;
lcd.setCursor(5, 1);
lcd.print(i);
}
digitalWrite(IO_PINS[i], LOW);
delay(50);
}
if (!flag) lcd.clear();
}
Start
Reset