#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLLUMNS 20
#define LCD_ROWS 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLLUMNS, LCD_ROWS);
#define LEFT_BTN A1
#define RIGHT_BTN A0
const int WAIT_TIME = 200; //200 milliseconds
int POSITION = 0;
int PREVIOUS_LEFT_BTN = HIGH;
int PREVIOUS_RIGHT_BTN = HIGH;
unsigned long timeSinceLeftPress = 0;
unsigned long timeSinceRightPress = 0;
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(5, 0);
lcd.print("TUG OF WAR");
delay(1000);
lcd.init();
Serial.begin(115200);
pinMode(LEFT_BTN, INPUT_PULLUP);
pinMode(RIGHT_BTN, INPUT_PULLUP);
}
void loop() {
unsigned long nowTime = millis();
int LEFT_BTN_STATE = digitalRead(LEFT_BTN);
int RIGHT_BTN_STATE = digitalRead(RIGHT_BTN);
if (LEFT_BTN_STATE == LOW && PREVIOUS_LEFT_BTN == HIGH && (nowTime - timeSinceLeftPress) > WAIT_TIME && POSITION > -9)
{
timeSinceLeftPress = nowTime; //Track current time
POSITION = POSITION - 1;
}
if (RIGHT_BTN_STATE == LOW && PREVIOUS_RIGHT_BTN == HIGH && (nowTime - timeSinceLeftPress) > WAIT_TIME && POSITION < 9)
{
timeSinceRightPress = nowTime; //Track current time
POSITION = POSITION + 1;
}
lcd.init();
lcd.setCursor(9 + POSITION, 1);
lcd.print("|");
lcd.setCursor(9 + POSITION, 2);
lcd.print("|");
int PREVIOUS_LEFT_BTN = LEFT_BTN_STATE;
int PREVIOUS_RIGHT_BTN = RIGHT_BTN_STATE;
}