#include <LiquidCrystal_PCF8574.h>
#include <Wire.h>
#include <time.h>
int userArrowPos = 0;
int targetArrowPos;
unsigned long startTime;
//initialize the liquid crystal library
//the first parameter is the address
LiquidCrystal_PCF8574 lcd(0x27);
int analogInPin = A0;
int sensorValue = 0;
byte upArrow[8] = {
0b00100,
0b01110,
0b10101,
0b00100,
0b00100,
0b00100,
0b00100,
0b00000
};
byte downArrow[8] = {
0b00100,
0b00100,
0b00100,
0b00100,
0b10101,
0b01110,
0b00100,
0b00000
};
bool runComplete = false;
void setup() {
//initialize lcd screen
lcd.begin(16, 2);
// turn on the backlight
lcd.setBacklight(255);
lcd.createChar(0, upArrow);
lcd.createChar(1, downArrow);
pinMode(2, INPUT_PULLUP);
pinMode(3, OUTPUT);
randomSeed(analogRead(0));
targetArrowPos = random(0, 16);
drawDownArrow();
startTime = millis();
lcd.setCursor(userArrowPos, 1);
lcd.write(byte(0));
}
void drawDownArrow() {
lcd.setCursor(targetArrowPos, 0);
lcd.write(byte(1));
}
void drawUpArrow() {
lcd.setCursor(userArrowPos, 1);
lcd.write(byte(0));
}
void loop() {
// put your main code here, to run repeatedly:
sensorValue = analogRead(analogInPin);
int mappedVal = map(sensorValue, 0, 1023, 0, 15);
if(userArrowPos != mappedVal) {
userArrowPos = mappedVal;
lcd.clear();
drawDownArrow();
drawUpArrow();
}
if(digitalRead(2) == LOW) {
if(userArrowPos != targetArrowPos) {
tone(3, 3000);
delay(2000);
noTone(3);
} else {
lcd.clear();
unsigned long finishTime = millis() - startTime;
lcd.print(finishTime);
delay(2000);
lcd.clear();
targetArrowPos = random(0, 16);
drawDownArrow();
drawUpArrow();
startTime = millis();
}
}
}