// Forum: https://forum.arduino.cc/t/pong-paddle-on-lcd-is-updating-too-late/1109845
// Author: niko65
// Not a working project, no buttons are read during a delay.
// Now working due to changes by
// 2023-04-02
// Thanks to Koepel for the Wokwi start sketch!
// ec2021
#include <LiquidCrystal.h>
int buttonState = 0;
int buttonState2 = 0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte hero2[8] = {
B00000,
B00000,
B01110,
B11111,
B11111,
B11111,
B01110,
B00000
};
byte hero4[8] = {
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000
};
byte hero5[8] = {
B00001,
B00001,
B00001,
B00001,
B00001,
B00001,
B00001,
B00001
};
const int LeftMargin = 1;
const int RightMargin = 15;
const int LeftPin = 6;
const int RightPin = 8;
const int LeftPad = 9;
const int RightPad = 10;
const int WinLevel = 10;
boolean paddle1Up = false;
boolean paddle2Up = false;
int countLeft = 0;
int countRight = 0;
void setup() {
pinMode(LeftPin, INPUT);
pinMode(RightPin, INPUT);
lcd.begin(16, 2);
lcd.createChar(7, hero2);
lcd.createChar(9, hero4);
lcd.createChar(10, hero5);
Start();
}
void loop() {
Handle(LeftPin, LeftMargin, paddle1Up, LeftPad);
Handle(RightPin,RightMargin, paddle2Up, RightPad);
MovePong();
CheckData();
}
void MovePong(){
static int j = LeftMargin;
static int Step = 1;
static unsigned long lastMove = 0;
if (millis()-lastMove > 300){
lastMove = millis();
Remove(j - Step);
Set(j);
if ((j == LeftMargin && paddle1Up) ||
(j == RightMargin && paddle2Up)) {Step = -Step;}
j = j + Step;
if (j > RightMargin) {
Remove(RightMargin);
j = LeftMargin;
countRight++;
PrintCounter(false);
}
if (j < LeftMargin) {
Remove(LeftMargin);
j = RightMargin;
countLeft++;
PrintCounter(false);
}
}
}
// Pin 6 für Left byte(9) für paddle
// Pin 8 für Right byte(10) für paddle
void Handle(int Pin,int Margin, boolean &padPos, byte paddle){
boolean buttonState = digitalRead(Pin);
if (buttonState == HIGH) {
lcd.setCursor(Margin, 0);
lcd.print(" ");
lcd.setCursor(Margin, 1);
lcd.write(paddle);
padPos = false;
} else {
lcd.setCursor(Margin, 1);
lcd.print(" ");
lcd.setCursor(Margin, 0);
lcd.write(paddle);
padPos = true;
}
}
void Remove(int at){
lcd.setCursor(at, 0);
lcd.print(" ");
}
void Set(int at){
lcd.setCursor(at, 0);
lcd.write(byte(7));
}
void Start(){
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(" PONG ");
delay(2000);
lcd.clear();
PrintCounter(true);
}
void PrintCounter(boolean Reset){
static int oldLeft = -1;
static int oldRight = -1;
if (Reset) {
oldLeft = -1;
oldRight = -1;
countLeft = 0;
countRight = 0;
}
if (oldLeft != countLeft){
oldLeft = countLeft;
int lm = LeftMargin+1;
lcd.setCursor(lm, 1);
lcd.print("00");
if (countLeft<10) lm++;
lcd.setCursor(lm, 1);
lcd.print(countLeft);
}
if (oldRight != countRight){
oldRight = countRight;
int rm = RightMargin-2;
lcd.setCursor(rm, 1);
lcd.print("00");
if (countRight < 10) rm++;
lcd.setCursor(rm, 1);
lcd.print(countRight);
}
}
void CheckData(){
if (countLeft >= WinLevel) {
Lost(" Left has lost ");
return;
};
if (countRight >= WinLevel) {
Lost(" Right has lost ");
}
}
void Lost(String Loser){
lcd.setCursor(0,0);
lcd.print(Loser);
delay(2000);
Start();
}