Hello! I am working on a simple game where you use buttons to control a character that looks like a man on an LCD screen. You try and move it to a ball, similar to Snake. I was making a difficulty selector at the beginning around " while(gameplay == false)" Im using a button to enter what difficulty you want. I set up the button just like I did with the other direction button that works, I cant tell if it works. It immediately jumps to the gameplay section. But the gameplay section only happens if the button is pressed. But I'm not pressing the button. Can anyone tell me why?
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
LiquidCrystal lcd1(7, 8, 9, 10, 11, 13);
int x = 0;
int y = 0;
int up = 3;
int right = 2;
int down = 5;
int left = 4;
short bx;
short by;
int ms = 0;
int wm = 0;
int tl = 0;
int score = 0;
int goal = 0;
int gameplay = false;
int difficulty = 0;
int select = 12;
byte Man[] = {
B00100,
B01010,
B00100,
B01110,
B10101,
B00100,
B01010,
B10001
};
byte ball[] = {
B00000,
B00000,
B00000,
B01110,
B01110,
B00000,
B00000,
B00000
};
void setup() {
Serial.begin(9600);
pinMode(up, INPUT_PULLUP);
pinMode(left, INPUT_PULLUP);
pinMode(down, INPUT_PULLUP);
pinMode(right, INPUT_PULLUP);
pinMode(select, INPUT_PULLUP);
lcd1.begin(16,2);
lcd.init();
lcd.backlight();
lcd.createChar(1, Man);
lcd.createChar(2, ball);
lcd.setCursor(x,y);
lcd.write(1);
randomSeed(analogRead(0));
bx = random(0, 20);
by = random(0, 4);
}
void loop() {
while (gameplay == false){
lcd.setCursor(0,0);
lcd.print("Choose Difficulty:");
lcd.setCursor(0,2);
lcd.print("< >");
if (difficulty == 0 or difficulty == 2){
lcd.setCursor(8,2);
if(difficulty == 0){
lcd.print("Easy");
}
if(difficulty == 2){
lcd.print("Hard");
}
}
if (difficulty == 1 or difficulty == 3){
lcd.setCursor(7,2);
if(difficulty == 1){
lcd.print("Medium");
}
if(difficulty == 3){
lcd.print("Expert");
}
}
if(digitalRead(left) == HIGH){
if(difficulty != 0){
difficulty--;
}
else{
difficulty = 3;
}
}
if(digitalRead(right) == HIGH){
if(difficulty != 3){
difficulty++;
}
else{
difficulty = 0;
}
}
if (digitalRead(select) == HIGH){
if(difficulty == 0){
tl = 29;
goal = 10;
}
if(difficulty == 1){
tl = 59;
goal = 30;
}
if(difficulty == 2){
tl = 89;
goal = 45;
}
if(difficulty == 3){
tl = 119;
goal = 75;
}
gameplay = true;
}
delay(100);
}
lcd.clear();
lcd.setCursor(x,y);
lcd.write(1);
lcd.setCursor( bx, by);
lcd.write(2);
if(digitalRead(down) == HIGH and y != 4){
y++;
}
if(digitalRead(up) == HIGH and y > 0){
y--;
}
if(digitalRead(left) == HIGH and x != -1){
x--;
}
if(digitalRead(right) == HIGH and x < 19){
x++;
}
if(x == bx and y == by){
tone(6, 1975.53, 100);}
delay(100);
if(x == bx and y == by){
tone(6, 2637.02, 100);
score++;
bx = random(0, 20);
by = random(0, 4);
}
ms++;
if(ms == 10){
ms = 0;
tl--;
}
Serial.println( x );
Serial.println( y );
lcd1.setCursor(0,0);
lcd1.print("Score:" );
lcd1.print(score);
lcd1.print("/");
lcd1.print(goal);
lcd1.setCursor(0,1);
lcd1.print("Time:");
lcd1.print(tl);
lcd1.print(".");
lcd1.print(ms);
while(goal == score or tl == 0 and ms == 0){
if(score == goal){
if(wm == 0){
wm++;
lcd.clear();
lcd1.clear();
}
lcd.setCursor(0,0);
lcd.print("you win");
lcd1.setCursor(0,0);
lcd1.print("you win");
}
else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("YOU LOSE ");
lcd1.setCursor(0,0);
lcd1.print("HAHA");
}
}
}
I think the problem is occurring around "if (digitalRead(select) == HIGH)", but I can't figure out why. Below is a tinkercad diagram (sorry if it sucks). The LCD screen that isn't connected is controlled by A4 and A5. Thank you!
