#include <LiquidCrystal.h>
#include <Keypad.h>
// Keypad
#define ROW_NUM 4 // four rows
#define COLUMN_NUM 4 // four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {13, 12, 14, 27}; // GIOP19, GIOP18, GIOP5, GIOP17 connect to the row pins
byte pin_column[COLUMN_NUM] = {26, 25, 33, 32}; // GIOP16, GIOP4, GIOP0, GIOP2 connect to the column pins
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
// LCD
LiquidCrystal lcd(19, 23, 18, 17, 16, 15);
// Variablen
int status = 1; // 1 = ready for game; 2 = ingame; 3 = detonatet; 4 = defused; 5 = Error
String code = "62851";
String inputcode = "";
// Thread
TaskHandle_t InGameDisplayHandler;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Thread
delay(500);
}
void loop() {
switch(status){
case 1:
lcd.begin(1, 2);
lcd.setCursor(0, 0);
lcd.print("Ready for Game");
lcd.setCursor(0, 1);
lcd.print("Press 'D'");
while(keypad.getKey() != 'D'){
}
status = 2;
clearDisplay();
break;
case 2:
xTaskCreatePinnedToCore(
InGameDisplay, /* Task function. */
"InGameDisplayHandler", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&InGameDisplayHandler, /* Task handle to keep track of created task */
0);
InGame();
break;
case 3:
lcd.setCursor(0, 0);
lcd.print("Bomb detonated");
lcd.setCursor(0, 1);
lcd.print("Press 'D'");
break;
case 4:
lcd.setCursor(0, 0);
lcd.print("Bomb defused");
lcd.setCursor(0, 1);
lcd.print("Press 'D'");
break;
case 5:
break;
default:
break;
}
delay(50);
}
void InGameDisplay( void * pvParameters){
while(status == 2){
lcd.setCursor(0, 0);
lcd.print("Code:"+code);
delay(50);
lcd.setCursor(0, 1);
lcd.print("---->"+inputcode);
delay(50);
}
}
void InGame(){
while(status == 2){
char key = keypad.getKey();
if(key){
switch(key){
case 'B':
inputcode = "";
lcd.setCursor(5, 1);
lcd.print(" ");
break;
case 'D':
if(inputcode == code){
status = 4;
vTaskDelete(InGameDisplayHandler);
}else{
inputcode = "";
lcd.setCursor(5, 1);
lcd.print(" ");
}
break;
default:
inputcode += key;
}
}
}
}
void clearDisplay(){
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
}