#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#define buttonY 3
#define buttonG 2
#define buttonB 1
#define buttonR 0
int arr [255];
int arrsize = 1;
int highScore = 0;
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
EEPROM.update(0,0);
pinMode(buttonY, INPUT_PULLUP);
pinMode(buttonG, INPUT_PULLUP);
pinMode(buttonB, INPUT_PULLUP);
pinMode(buttonR, INPUT_PULLUP);
//srand (time(NULL));
}
void generate(int DATA_MAX){
for (int i=0; i < DATA_MAX; i++){
int to_blink = random(60000) % 4;
arr[i] = to_blink;
}
}
void yellow(int x){
if(x==1){
lcd.setCursor(0,0);
lcd.print("Zluta");
}else{
lcd.setCursor(0,0);
lcd.print(" ");
}
}
void green(int x){
if(x==1){
lcd.setCursor(0,1);
lcd.print("Zelena");
}else{
lcd.setCursor(0,1);
lcd.print(" ");
}
}
void blue(int x){
if(x==1){
lcd.setCursor(0,2);
lcd.print("Modra");
}else{
lcd.setCursor(0,2);
lcd.print(" ");
}
}
void red(int x){
if(x==1){
lcd.setCursor(0,3);
lcd.print("Cervena");
}else{
lcd.setCursor(0,3);
lcd.print(" ");
}
}
enum {
YELLOW,
GREEN,
BLUE,
RED,
ALL
};
int STATE = YELLOW;
bool REDRAW_MENU = true;
void blinkColor(int color, int time) {
switch(color){
case YELLOW: yellow(1); delay(time); yellow(0); break;
case GREEN: green(1); delay(time); green(0); break;
case BLUE: blue(1); delay(time); blue(0); break;
case RED: red(1); delay(time); red(0); break;
}
}
bool isPressed(int button) {
return digitalRead(button) == LOW;
}
int whichButtonPressed() {
if (isPressed(buttonY)) {
return YELLOW;
}
if (isPressed(buttonR)) {
return RED;
}
if (isPressed(buttonG)) {
return GREEN;
}
if (isPressed(buttonB)) {
return BLUE;
}
return -1;
}
bool isAnyButtonPressed() {
return whichButtonPressed() != -1;
}
void flash(const char* txt, int time=500) {
lcd.setCursor(0,0);
lcd.print(txt);
delay(time);
lcd.setCursor(0,0);
lcd.print(" ");
}
void drawErrors(int err) {
lcd.setCursor(14,0);
if (err >= 0) {
lcd.print("err:");
lcd.print(err);
} else {
lcd.print(" ");
}
}
void scoreLoop() {
lcd.clear();
lcd.print("Your score: ");
lcd.print(highScore);
int highestScore = EEPROM.read(0);
if (highestScore < highScore){
EEPROM.update(0, highScore);
}
highestScore = EEPROM.read(0);
lcd.setCursor(0,2);
lcd.print("High score: ");
lcd.print(highestScore);
EEPROM.get(0, highScore);
Serial.println(highScore);
delay(100);
bool validSecret = true;
int seq[3] = {0,1,2};
for (auto b : seq) {
if (!validSecret) {
break;
}
while (!isAnyButtonPressed()) {}
if (whichButtonPressed() != b) {
validSecret = false;
}
Serial.println(".");
delay(300);
}
Serial.println("Rozhodne to funguje");
if (validSecret) {
EEPROM.update(0,0);
Serial.println("Secret");
}
while (whichButtonPressed() == -1) {
}
lcd.clear();
}
void loop() {
generate(arrsize);
for (int i=0; i < arrsize ;i++) {
blinkColor(arr[i], 1000);
delay(100);
}
int pos = 0;
int err = 0;
while (1) {
int button = whichButtonPressed();
if (button != -1) {
if (button == arr[pos]) {
flash("Yes!", 300);
pos++;
if (pos >= arrsize) {
arrsize++;
break;
}
} else {
err++;
if (err == 3) {
flash("You lose");
if (arrsize > highScore) {
highScore = arrsize;
}
arrsize = 1;
drawErrors(-1); // hide
scoreLoop();
break;
}
drawErrors(err);
}
delay(500);
}
}
delay(1000);
}
/* cicle(x, y , velikost){
Esplora.tone(frequency, duration)
}
*/