/* LCD 2x16 example
22.10.2024. Djordje Herceg
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define KEY1 39
#define KEY2 36
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("LCD 2x16 example");
LCD.init();
LCD.backlight();
LCD.setCursor(1, 0);
LCD.print("This is a 2x16");
LCD.setCursor(0, 1);
LCD.print("line LCD display");
delay(1000);
LCD.clear();
LCD.setCursor(0, 1);
LCD.print("A: dec, B: inc");
pinMode(KEY1, INPUT_PULLUP);
pinMode(KEY2, INPUT_PULLUP);
}
int counter = 100;
bool pPressed=false;
bool qPressed=false;
char buffer[7];
void loop() {
// put your main code here, to run repeatedly:
static long mils=millis();
int p = digitalRead(KEY1);
int q = digitalRead(KEY2);
if (p == LOW) {
mils=millis();
if (pPressed==false){
counter--;
pPressed=true;
}
}
if (q == LOW) {
mils=millis();
if (qPressed==false){
counter++;
qPressed=true;
}
}
sprintf(buffer, "%6d", counter);
LCD.setCursor(0, 0);
LCD.print(buffer);
if ((millis()-mils>50) && (pPressed==true)){
pPressed=false;
}
if ((millis()-mils>50) && (qPressed==true)){
qPressed=false;
}
delay(10); // this speeds up the simulation
}