Go Down

Topic: Help with Game (Read 2488 times) previous topic - next topic

marshen

#15
Jan 30, 2011, 02:53 am Last Edit: Jan 30, 2011, 03:10 am by marshen Reason: 1
ok so when i pressed the white button i got

White button state: 1

from my Serial Monitor

CrossRoads

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?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

marshen

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



Code: [Select]
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);

    }
  }
}

WizenedEE


marshen


It's else if()


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

marshen

ok so it compiles now...

Still doesn't work it the lights only change with the white button it should be 1 button to one 1 LED

Code: [Select]
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);
    delay(1000);
   }
 }
 else if (ColorNumber == 1){
   //Play Red
   digitalWrite(11, HIGH);
   buttonStateRed = digitalRead(ButtonRed);
   if (buttonStateRed == HIGH){
     digitalWrite (LEDRed, LOW);
delay(1000);
   }
 }
 else
   digitalWrite(12, HIGH);
   buttonStateBlue = digitalRead(ButtonBlue);
   if (buttonStateBlue == HIGH){
     digitalWrite (LEDBlue, LOW);
delay(1000);
   }
 }

PaulS

So, Game() gets called. It generates a random number between 0 and 2. Say it generates a 1.

When colorNumber is 1, it turns pin 11 on and checks to see if the switch whose number so stored in ButtonRed is being pressed. If it is, it turns the pin whose number is stored in LEDRed LOW, waits one second, and returns.

If the switch whose number so stored in ButtonRed is NOT being pressed, the function returns immediately.

So, what is this game called?

Two additional questions. Why are pins referred to be name in some places (LEDRed, ButtonRed) and number (11) in other places?

Is Game() really doing what you want?

Actually, I had three additional questions. If Game() is not doing what you want, what is it you want it to be doing?

CrossRoads

After the random color light is turned on, do you want to give the players a chance to press the button that goes with it?
As it is now, the white button is pressed to start the game, and with no debouncing it will surely be read still being high when the game is entered.

You have no comments, so we can only guess at the intent of the game.
Here is my guess, hopefully this will give you some ideas:

You press the white button to start the game - you intended to debounce the switch, then have some random interval, after which one of the 3 LEDs would go on. The player would then get some interval of time in which to press the switch, after which the LED would do something to indicate a press made in time, otherwise do something different to indicate the press was not made in time, after whihc the game would automatically continue with another random delay followed by the next light turning on, etc.

Instead of the if() else() else(), you could set up void loop() like this, with some other thoughts on how the game might be used.
You'll have to flesh it out some, but I think the concept is shown clearly enough:

setup()
{
white button is pressed to start the game
}

loop()
{
delay (250+ random(3) * 500);// delay a random time from 250mS to 1750 ms, also serves to debounce the initial button
ColorRandom(3); // select random # 1,2,3 (??) - will 0 come up here also?  adjust as needed
switch(ColorRandom){  // pseudocode written for this to be 1,2,3 only
case 1: //
turn on white LED
start_time = millis();  // unsigned long data type  - capture the time NOW
end_time = start_time;  // unsigned long data type - no time elapsed to start
// now give the player 1000mS to press the button
while ( (end_time - start_time <1000)  || WhiteButtonState == 1)  // mini-loop waiting for 1000mS OR a button press
{
WhiteButtonState = digitalRead(WhiteButton)  // 
    if (WhiteButtonState == 1) { // button was pressed, this will end the While mini-loop
    turn off white LED
    }
    else
     { // button was not pressed yet
      WhiteButtonState = digitalRead(WhiteButton)  // read the button again
     end_time = millis()  // reset the end time so we can see if 1000mS went by
     }       
}  // back to while test for end of time or a button press

// now time has elapsed, or button was pressed
if (WhiteButtonState == 0)
{
// player missed, flash all 3 LEDs On/Off in obnoxious manner
}
else // WhiteButtonState was =to 1
{
// player made it, flash all 3 LEDs in a pleasing sequence
}
break;

case 2:
// repeat code for next button
break;

case 3:
// repeat code for next button
break;

} // end of switch
} // end void loop, on to next random delay & switch selection
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

marshen


So, Game() gets called. It generates a random number between 0 and 2. Say it generates a 1.

When colorNumber is 1, it turns pin 11 on and checks to see if the switch whose number so stored in ButtonRed is being pressed. If it is, it turns the pin whose number is stored in LEDRed LOW, waits one second, and returns.

If the switch whose number so stored in ButtonRed is NOT being pressed, the function returns immediately.

What i want it to do is end the game in a game over()
Quote
So, what is this game called?

Its called the button game for lack of a better name...
Quote
Two additional questions. Why are pins referred to be name in some places (LEDRed, ButtonRed) and number (11) in other places?

Im I have no idea thats just the way I programed it.
Quote
Is Game() really doing what you want?

No.
Quote
If Game() is not doing what you want, what is it you want it to be doing?

ColorNumber: 1
White button state: 1   // this is to start the game.
ColorNumber: 1
White button state: 1//as you can see none of the the other buttons do anything
ColorNumber: 2
White button state: 1
ColorNumber: 2
White button state: 1// also i just want one value to one button push but i seem to get multiple Color Numbers when i press the white button
ColorNumber: 1
White button state: 1
ColorNumber: 2
White button state: 1
ColorNumber: 0
White button state: 1
ColorNumber: 2


the aim of the game is to have an LED light up and then the button that is for example blue is pressed and the blue button is pressed it then restarts Game() starts over adding one to the score and decreasing the time you have to push the button

I have yet to write the red part but thats is the over all goal.


Go Up