Bop it game code

I am trying to make a "bop it" game using a arduino uno but my code? is acting up

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int bop = 10;
const int twist = A0;
const int pull = A1;
const int start = 9;
int startVal = 0;
bool lose = false;
int picker = 0;
int wait = 500;
int bopState = 0;
bool hit = true;
void setup()
{
  lcd.begin(16,2);
  pinMode(bop,INPUT);
  pinMode(twist,INPUT);
  pinMode(pull,INPUT);
  pinMode(start,INPUT);
  lcd.print("Bop It to start");
}

void loop()
{
  while(startVal != 1){
   startVal = digitalRead(start);
  }
  startVal = digitalRead(start);
  if (startVal == 1){
    lcd.print("3");
    delay(200);
    lcd.print("2");
    delay(200);
    lcd.print("1");
    delay(200);
    while(lose == false){
      picker = random(1,4);
      switch(picker){
        case 1:
        	bopIt();
        	break;
		case 2:
        	twistIt();
        	break;
		case 3:
        	pullIt();
        	break;
      }
    }
  }
}
int bopIt(){
    lcd.print("Bop It");
    bopState = digitalRead(bop);
    while(millis() < millis() + wait){
      if(bopState != digitalRead(bop)){
        lcd.print("*bop noise*");
        hit = true;
      }
    }
    if(hit == false){
      lose == true;
    }
  }
int twistIt(){
    lcd.print("Twist It");
    bopState = analogRead(twist);
    while(millis() < millis() + wait){
      if(bopState != analogRead(twist)){
        lcd.print("*twist noise*");
        hit = true;
      }
    }
    if(hit == false){
      lose == true;
    }
  }
int pullIt(){
    lcd.print("Pull It");
    bopState = analogRead(pull);
    while(millis() < millis() + wait){
      if(bopState != analogRead(pull)){
        lcd.print("*pull noise*");
        hit = true;
      }
    }
    if(hit == false){
      lose == true;
    }
  }


attached are my wiring and code for the project i am working on
however when i ran the code in a physical environment it does not work as intended
first, it displays twist noise across the entire screen even though it is no where near that position in the code
the first if statement is seemingly ignored (it prints 3,2,1) then it displays this character that is constantly flickering (the lcd itself is not flickering but there is a character the is constanly changing)

when i try to simulate it the lcd just lights up

additionally here is a video of me testing it

You have three functions with return types and no return statements.

In each decision to lose, you are not setting lose to true, instead you are comparing it to true and this is likely optimized away by the compiler.

Hit is always true.
You should start it as false, and at the beginning of the while, you will need to reset it to false.

Also, you are using digital read on analog pots, this won't work. Make a simple program with just one pot. Check out tutorials on it and make it print values to the display. You may find that you need a differential value, such as moving it at least 5.

The GND pin needs to be grounded.

These statements are throughout your code.

This statement will ALWAYS be true so it will never exit the loop.

Thank you so much for your feedback
attached is a video of what happens after i did the fixes you recomended (see attached)
please lmk if you can help

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int bop = 10;
const int twist = A0;
const int pull = A1;
const int start = 8;
int startVal = 0;
bool lose = false;
int picker = 0;
int wait = 500;
int bopState = 0;
bool hit = false;
void setup()
{
  lcd.begin(16,2);
  pinMode(bop,INPUT);
  pinMode(twist,INPUT);
  pinMode(pull,INPUT);
  pinMode(start,INPUT);
  lcd.print("Bop It to start");
}

void loop()
{
  while(startVal != 1){
   startVal = digitalRead(start);
  }
  if (startVal == 1){
    lcd.clear();
    lcd.print("3");
    delay(200);
    lcd.print("2");
    delay(200);
    lcd.print("1");
    delay(200);
    while(lose == false){
      picker = random(1,4);
      switch(picker){
        case 1:
        	bopIt();
        	break;
		case 2:
        	twistIt();
        	break;
		case 3:
        	pullIt();
        	break;
      }
    }
  }
}
int bopIt(){
    lcd.print("Bop It");
    bopState = digitalRead(bop);
    while(millis() < millis() + wait){
      if(bopState != digitalRead(bop)){
        lcd.print("*bop noise*");
        hit = true;
      }
    }
    if(hit == false){
      lose = true;
    }
    return;
  }
int twistIt(){
    lcd.print("Twist It");
    bopState = analogRead(twist);
    while(millis() < millis() + wait){
      if(bopState != analogRead(twist)){
        lcd.print("*twist noise*");
        hit = true;
      }
    }
    if(hit == false){
      lose = true;
    }
    return;
  }
int pullIt(){
    lcd.print("Pull It");
    bopState = analogRead(pull);
    while(millis() < millis() + wait){
      if(bopState != analogRead(pull)){
        lcd.print("*pull noise*");
        hit = true;
      }
    }
    if(hit == false){
      lose = true;
    }
    return;
  }

This project looks to have been made all at once, with little to no testing of individual pieces. The result is that it is very difficult to find all the things wrong.

You should make one thing at a time, write code for it and test it, then do so with each other thing. After all of that, start combining things one at a time. That way, each time you hit an error, it's very likely the last thing that you did or added that caused it.

Please follow all of my directions as well as those of the other users before asking for more help.

okay tysm

time = millis();
while(millis() < time + wait){

To avoid any problems with overflow, you should actually use...

while(millis() - time < wait)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.