#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
#define VERT_PIN A0
#define HORZ_PIN A1
#define SEL_PIN 2
const int buttonUp = 6;
const int buttonDown = 7;
const int buttonLeft = 8;
const int buttonRight = 9;
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
byte arrowUp[] = {
B00100,
B01010,
B10101,
B00100,
B00100,
B00100,
B00100,
B00100
};
byte arrowDown[] = {
B00100,
B00100,
B00100,
B00100,
B00100,
B10101,
B01110,
B00100
};
byte arrowRight[] = {
B00000,
B00100,
B00010,
B11111,
B00010,
B00100,
B00000,
B00000
};
byte arrowLeft[] = {
B00000,
B00100,
B01000,
B11111,
B01000,
B00100,
B00000,
B00000
};
void setup() {
// Init
lcd.init();
lcd.createChar(0, arrowUp);
lcd.createChar(1, arrowDown);
lcd.createChar(2, arrowLeft);
lcd.createChar(3, arrowRight);
lcd.backlight();
lcd.setCursor(5, 2); //X,Y
lcd.write(0);
lcd.setCursor(15, 2);
lcd.write(1);
lcd.setCursor(9, 0);
lcd.print("||");
lcd.setCursor(9, 1);
lcd.print("||");
lcd.setCursor(9, 2);
lcd.print("||");
lcd.setCursor(9, 3);
lcd.print("||");
pinMode(buttonUp, INPUT);
pinMode(buttonDown, INPUT);
pinMode(buttonLeft, INPUT);
pinMode(buttonRight, INPUT);
Serial.begin(115200);
}
void loop() {
long next_arrow = random(4); //Generate random number
lcd.setCursor(5, 2); // Set cursor for anonymous arrow position
//next_arrow = 0;
// as we store custom object (arrow) in number, we can just write random number value to display
lcd.write(next_arrow);
delay(1000);
// The display is showing the up arrow:P
if (next_arrow == 0) {
// We have to read the value of buttonUp:)
int newValue = digitalRead(buttonUp);
Serial.println(newValue);
// if (newValue == HIGH) {
// lcd.setCursor(12, 2);
// lcd.write(next_arrow);
// } else {
// gameOver();
// }
} // The display is showing the Down arrow:)
else if (next_arrow == 1) {
// We have to read the value of buttonUp:)
int newValue = digitalRead(buttonDown);
Serial.println(newValue);
// if (newValue == HIGH) {
// lcd.setCursor(12, 2);
// lcd.write(next_arrow);
// } else {
// gameOver();
// }
} // The display is showing the Left arrow:)
else if (next_arrow == 2) {
// We have to read the value of buttonLeft:)
int newValue = digitalRead(buttonLeft);
Serial.println(newValue);
// if (newValue == HIGH) {
// lcd.setCursor(12, 2);
// lcd.write(next_arrow);
// } else {
// gameOver();
// }
} // The display is showing the Right arrow:)
else if (next_arrow == 3) {
// We have to read the value of buttonRight:)
int newValue = digitalRead(buttonRight);
Serial.println(newValue);
// if (newValue == HIGH) {
// lcd.setCursor(12, 2);
// lcd.write(next_arrow);
// } else {
// gameOver();
// }
}
}
void gameOver() {
lcd.setCursor(0, 12);
lcd.print("GAME OVER!");
}