#include <LiquidCrystal_I2C.h> // versie 18/12/2023
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
#define PIN_POT A2
#define PIN_BUT 7
#define BUZZER 8
// States
#define GAME_ON 0
#define CHANGE_BEKER 1
#define NEW_DRUPJE 2
#define CHECK_POINTS 3
// functions
void drawBeker();
void gaNaarNieuwePos ();
void checkInputPot();
void checkpoints();
void drawDruppel();
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
uint8_t char0[8] = {17, 17, 31, 4, 4, 4, 4, 31}; // bekertje
uint8_t char1[8][8] = {
{4, 0, 0, 0, 0, 0, 0, 0},
{0, 4, 0, 0, 0, 0, 0, 0},
{0, 0, 4, 0, 0, 0, 0, 0},
{0, 0, 0, 4, 0, 0, 0, 0},
{0, 0, 0, 0, 4, 0, 0, 0},
{0, 0, 0, 0, 0, 4, 0, 0},
{0, 0, 0, 0, 0, 0, 4, 0},
{0, 0, 0, 0, 0, 0, 0, 4}
}; // drupje
int speed = 250;
const int VERSNELLING = 20;
byte teller = 0;
int oldValue = 0;
byte statelcd = GAME_ON;
byte posDruppel;
byte posBeker;
int score = 0;
int highScore = 0;
const int DREMPEL = 3;
void setup() {
Serial.begin(9600);
pinMode(A2, INPUT);
// Init
randomSeed(analogRead(A2));
posDruppel = random(15);
lcd.init();
lcd.backlight();
lcd.createChar(0, char0);
lcd.setCursor(0, 1);
lcd.print(char(0));
}
void loop() {
switch (statelcd) {
case GAME_ON :
drawDruppel(); //0
checkInputPot();
break;
case CHANGE_BEKER: // 1
drawBeker();
break;
case NEW_DRUPJE: // 2
gaNaarNieuwePos ();
statelcd = GAME_ON;
break;
case CHECK_POINTS: //3
checkpoints();
statelcd = NEW_DRUPJE;
break;
default:
Serial.println("Error ");
}
}
// definitie functie :::
void drawDruppel() {
if ( millis() % speed
> speed
- 5 ) {
if (teller % 8 == 7) {
statelcd = CHECK_POINTS;
}
lcd.createChar(1, char1[ teller % 8 ]);
lcd.setCursor(posDruppel, 0);
lcd.print(char(1));
teller++;
}
}
void checkInputPot() {
if (analogRead(PIN_POT) != oldValue) {
oldValue = analogRead(PIN_POT);
statelcd = CHANGE_BEKER;
}
}
void drawBeker() {
posBeker = map(analogRead(PIN_POT), 0, 1023, 0, 15);
lcd.setCursor(0, 1);
for (int i = 0 ; i < posBeker; i++) {
lcd.print(" "); // blanco
}
lcd.print(char(0)); // bekertje
for (int i = posBeker + 1 ; i < 16; i++) {
lcd.print(char(254)); // blanco
}
statelcd = GAME_ON;
}
void checkpoints() {
if (posDruppel == posBeker) {
score++;
speed = speed - 10;
tone (BUZZER, 880, 200);
if (score > highScore) {
highScore = score;
}
if(score %DREMPEL > DREMPEL){
speed = speed - VERSNELLING;
}
Serial.print("aantal punten = ");
Serial.print(score);
Serial.print( " je persoonlijk record is = ");
Serial.println(highScore);
} else {
score = 0;
speed = 200;
Serial.println("druppel gesmost, start opnieuw");
tone (BUZZER, 220, 200);
}
}
void gaNaarNieuwePos () {
lcd.setCursor(posDruppel, 0);
lcd.print(" ");
posDruppel = random(16);
}