#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Keypad setup
const byte ROWS = 4, COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad pad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Button setup
const int btnPin = A0;
unsigned long lastTime = 0;
int cnt = 0;
bool lcdOn = true;
String buf = "";
void setup() {
lcd.init();
lcd.backlight();
pinMode(btnPin, INPUT);
}
void loop() {
checkBtn();
checkPad();
}
void checkBtn() {
static bool last = LOW;
bool curr = digitalRead(btnPin);
if (curr && !last) {
unsigned long now = millis();
cnt = (now - lastTime < 3000) ? cnt + 1 : 1;
lastTime = now;
lcdOn = !lcdOn;
lcdOn ? lcd.backlight() : lcd.noBacklight();
if (cnt >= 3) {
buf = "";
lcd.clear();
cnt = 0;
}
}
last = curr;
}
void checkPad() {
char key = pad.getKey();
if (key && lcdOn) {
buf += key;
if (buf.length() > 20) buf = buf.substring(buf.length() - 20);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(buf);
}
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6