// LCD1602 and Pi Pico!
#include <LiquidCrystal.h>
#include <stdio.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
#define buttonPin 6
uint8_t full[8] = {
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
};
uint8_t empty[8] = {
0b11111,
0b10001,
0b10001,
0b10001,
0b10001,
0b10001,
0b10001,
0b11111,
};
void setup() {
Serial.begin(9600);
lcd.createChar(3, full);
lcd.createChar(2, empty);
lcd.begin(16, 2);
lcd.setCursor(12,0);
lcd.print("|");
pinMode(buttonPin, INPUT_PULLUP);
}
int x = 0;
int timer = 0;
int score = 0;
int tempScore = 0;
bool clicked = false;
bool hitRight = false;
bool stopped = false;
void loop() {
if (score < 10 and not stopped) {
timer ++;
delay(1); // Adding a delay() here speeds up the simulation
if (timer > 500) {
lcd.clear();
lcd.setCursor(x,0);
lcd.print("\x03 ");
x += 2;
timer = 0;
clicked = false;
}
if (x >= 16) {
x = 0;
if (not hitRight) { score --; }
hitRight = false;
}
lcd.setCursor(12,0);
lcd.print("|");
lcd.setCursor(8,1);
lcd.print(score);
if (digitalRead(buttonPin) == LOW) { // used to have a bug here | digitalRead(buttonPin == LOW)
if (x == 14 and not clicked) {
tempScore += 500-timer;
if (tempScore > 200) {
score ++;
hitRight = true;
}
else {
score --;
}
clicked = true;
}
else if (not clicked) {
score --;
clicked = true;
}
}
}
else if (score > 9) {
stopped = true;
lcd.clear();
lcd.setCursor(8,0);
lcd.print("You win!");
}
if (score < -10) {
stopped = true;
lcd.clear();
lcd.setCursor(8,0);
lcd.print("... why?");
}
}