Help with Game

Hey all,
I'm working on a Game and i cant seem to get it to work.
So a few problems i have are:
1)it wont advance in to Game()
2)Game() I don't know how to loop it note it might work but idk because i cant try it.
3)I have an idea of how to make the game over sequence but again don't know if it will work.
4)Any helpful tips on how to program this more effectively would be nice... it just seems super messy.

int ButtonWhite = 2;
int ButtonRed = 3;
int ButtonBlue = 4;
const int LEDBlue = 12;
const int LEDRed = 11;
const int LEDWhite = 10;
int buttonState = 0;
int DelayTime = 3000;
unsigned long changetime;
int ColorNumber;
int buttonStateWhite = 0;
int buttonStateBlue = 0;
int buttonStateRed = 0;

void setup()
{
  pinMode(ButtonBlue, INPUT);
  pinMode(ButtonRed, INPUT);
  pinMode(ButtonWhite, INPUT);
  pinMode(LEDBlue, OUTPUT);
  pinMode(LEDRed, OUTPUT);
  pinMode(LEDWhite, OUTPUT);
}

void loop()
{
  delay(200);
  digitalWrite(10, HIGH);
  delay(200);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
  delay(200);
  digitalWrite(10, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(11, HIGH);
  //Press Green Button to start!
  buttonStateWhite = digitalRead(ButtonWhite);
  // check if the pushbutton is pressed.

  if (buttonStateWhite == HIGH) {  // if it is, the buttonState is HIGH:   
    Game(); 
  }
}

void Game ()
{ 

  ColorNumber = random(2);

  if (ColorNumber = 0){ 
    //Play White
    digitalWrite(10, HIGH);
    buttonStateWhite = digitalRead(ButtonWhite);
    if (buttonState == HIGH){
      Game();
    }
  }
  if (ColorNumber = 1){ 
    //Play Red
    digitalWrite(11, HIGH);
    buttonStateRed = digitalRead(ButtonRed);
    if (buttonState == HIGH){
      Game();
    }
  }
  if (ColorNumber = 2){ 
    //Play Blue
    digitalWrite(12, HIGH);
    buttonStateBlue = digitalRead(ButtonBlue);
    if (buttonState == HIGH){
      Game();
    }
  }
}

There are no digitalWrite statements that enable the internal pull-up resistors. So, do you have external pull-down resistors wired with the switch?

If you are not getting into the Game function, it is because the switch is not being recognized as pressed, which indicates a hardware problem. What kind if switch is it? Are you pressing the switch wired to pin 2?

Thanks for your quick reply
yes I have external pull-down resistors wired with the switch
im using tactile switch.
yes im pressing the switch wired to pin 2

and i tested my hardware with this code and it works 100%

const int ledPin = 10;
int button = 2;
int buttonState = 0;
void setup() {
       pinMode(ledPin, OUTPUT);
       pinMode(button,INPUT);
}

void loop () {
   
buttonState = digitalRead(button);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
      sos(); 
    }
}
  void sos(){
 for (int x=0; x<3; x++) {
   digitalWrite(ledPin, HIGH);
   delay (150);
   digitalWrite(ledPin,LOW);
   delay (100);
 }
 
 delay(100);
 
  for (int x=0; x<3; x++) {
   digitalWrite(ledPin, HIGH);
   delay (400);
   digitalWrite(ledPin,LOW);
   delay (100);
  }
  
  delay(100);
  
  for (int x=0; x<3; x++) {
   digitalWrite(ledPin, HIGH);
   delay (150);
   digitalWrite(ledPin,LOW);
   delay (100);
  }
  delay(500);  

   
}

In your new code, you read a switch
buttonstateCOLOR = digital(COLOR Switch); // or similar

then you make a decision on something else:
if (buttonstate == something){ // vs buttonstateCOLOR

and buttonstate = 0 all the time as it was originally defined.

EDIT: Ignore this, I think I have this reply mixed up with another similar post.
EDIT #2 Nevermind, this is correct. I was confused with the 2nd code listing.

It looks like internal pullups are enabled - they were just written High by pin # vs be LED color name.
All the digitalWrite (pin#, HIGH) for 10, 11, 12 could be moved to void setup (), I don't think you're getting anything out of writging them over and over in void loop() as you not redefining the pins as outputs.

not a problem do you have any ideas as to why it wont work?

Okay, so you have pullDown resistors, pressing the switch brings the input up to 5V?

You've got 600mS of delay at the top of void loop(), unless you are pressing & holding the button, seems like you have a high chance of missing the press during the brief time when you are not in the delays.

But even if you are, then I go back to my earlier statement: within void game, you read buttonstateColor, then act on buttonstate which is set to 0.
If you put a serialprint in void game, I bet you will find that you are getting in there.

so i have made the changes in the void setup and that works good :smiley:
but still i have had no luck with entering the Game()

int ButtonWhite = 2;
int ButtonRed = 3;
int ButtonBlue = 4;
const int LEDBlue = 12;
const int LEDRed = 11;
const int LEDWhite = 10;
int buttonState = 0;
int DelayTime = 3000;
unsigned long changetime;
int ColorNumber;
int buttonStateWhite = 0;
int buttonStateBlue = 0;
int buttonStateRed = 0;

void setup()
{
  pinMode(ButtonBlue, INPUT);
  pinMode(ButtonRed, INPUT);
  pinMode(ButtonWhite, INPUT);
  pinMode(LEDBlue, OUTPUT);
  pinMode(LEDRed, OUTPUT);
  pinMode(LEDWhite, OUTPUT);
  delay(200);
  digitalWrite(10, HIGH);
  delay(200);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
  delay(200);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(12, HIGH);
}


void loop()
{
 
  //Press Green Button to start!
  buttonStateWhite = digitalRead(ButtonWhite);
  // check if the pushbutton is pressed.

  if (buttonStateWhite == HIGH) {  // if it is, the buttonState is HIGH:   
    Game(); 
  }
}

void Game ()
{ 

  ColorNumber = random(2);

  if (ColorNumber = 0){ 
    //Play White
    digitalWrite(10, HIGH);
    buttonStateWhite = digitalRead(ButtonWhite);
    if (buttonState == HIGH){
      Game();
    }
  }
  if (ColorNumber = 1){ 
    //Play Red
    digitalWrite(11, HIGH);
    buttonStateRed = digitalRead(ButtonRed);
    if (buttonState == HIGH){
      Game();
    }
  }
  if (ColorNumber = 2){ 
    //Play Blue
    digitalWrite(12, HIGH);
    buttonStateBlue = digitalRead(ButtonBlue);
    if (buttonState == HIGH){
      Game();
    }
  }
}

Maybe it is, but you just can't tell because of the way you test:

buttonStateWhite = digitalRead(ButtonWhite);
if (buttonState == HIGH){ <<< these need to be buttonStateWhite (or red or blue or green, whatever)

CrossRoads:
Maybe it is, but you just can't tell because of the way you test:

buttonStateWhite = digitalRead(ButtonWhite);
if (buttonState == HIGH){ <<< these need to be buttonStateWhite (or red or blue or green, whatever)

ok so i changed it but still cant cant get into the Game()
all that happens is that the three LED light up.

So, add Serial.begin(9600); to setup(), and some Serial.print() statements to loop(). Figure out whether Game() gets called but does nothing, or doesn't get called at all.

In setup(), there is no reason to set pin 10 HIGH three times.

PaulS:
In setup(), there is no reason to set pin 10 HIGH three times.

thats just for a cool start up look.

edit nvm you were right im still quite new at programing.

thats just for a cool start up look.

If you have a light switch in your bedroom, and you turn it on three times, without ever turning it off, is that a cooler look that just turning it on once?

Hahahaha yes i think that it only needs to be turn on once and like i said im new to this programing stuff this is about my 10th project.

im not quite sure on how to put in Serial.print()

Serial.print("White button state: ");
Serial.println(buttonStateWhite);

ok so when i pressed the white button i got

White button state: 1

from my Serial Monitor

Ok, now do the same after Colornumber = random(2) and see what value you are getting.

At some point, you need to add
digitalWrite (LEDRed ,LOW);
digitalWrite (LEDWhite ,LOW);
digitalWrite (LEDBlue,LOW);

so that when you do a digitalWrite (10, HIGH); you will actually see something change.

I would suggest putting these in void setup ()
digitalWrite (LEDRed , HIGH); // assuming you are writing High to turn them on.
digitalWrite (LEDWhite ,HIGH);
digitalWrite (LEDBlue,HIGH);
followed by a little delay, and then

digitalWrite (LEDRed ,LOW);
digitalWrite (LEDWhite ,LOW);
digitalWrite (LEDBlue,LOW);

and then you're ready for your 1st button press to start the game.

Also, you need == in the if () statements:
if (ColorNumber == 0){

this is a C code gotcha, it still gets me somewhat regularly also.
== is the comparison,
otherwise you are just assigning ColorNumber the value 0.

Are you intending for a player to see the light go on from the random #, then press the switch & turn the LED off?
If so, you need to write the LED low there.

If not, when are they supposed to go off again?

Once you're in game(), why do you call out game() again?

Its much much better
So almost there but my if else statement wont compile

Button_Game_V2.cpp: In function 'void Game()':
Button_Game_V2:67: error: expected (' before 'else' Button_Game_V2:87: error: expected }' at end of input

void Game ()
{ 

  ColorNumber = random(3);
  Serial.print("ColorNumber: ");
  Serial.println(ColorNumber);
  if (ColorNumber == 0){ 
    //Play White
    digitalWrite(10, HIGH);
    buttonStateWhite = digitalRead(ButtonWhite);
    if (buttonStateWhite == HIGH){
      digitalWrite (LEDWhite , LOW);

    }
  }
  if else (ColorNumber == 1){ 
    //Play Red
    digitalWrite(11, HIGH);
    buttonStateRed = digitalRead(ButtonRed);
    if (buttonStateRed == HIGH){
      digitalWrite (LEDRed, LOW);

    }
  }
  else (ColorNumber == 2){ 
    //Play Blue
    digitalWrite(12, HIGH);
    buttonStateBlue = digitalRead(ButtonBlue);
    if (buttonStateBlue == HIGH){
      digitalWrite (LEDBlue, LOW);

    }
  }
}

It's else if()

WizenedEE:
It's else if()

Ahhhhh another stupid mistake.... Thanks for the help