Need Help on Basic Beginner Arduino Game

Hi guys, I’ve been messing around with Arduino and i want to create a simple game using digital pins. The objective is simple. I have 5 LEDs setup in series and a pushbutton. If the button is pressed while the middle LED is lit, you win. If you press the button if the others are lit, you lose. I think something is wrong with my code. Please guys, help me out. I’ve been stuck for days.

Game.ino (830 Bytes)

/* Gaming Project Must Utilize: -LED -Multiple Pins -Check Variable (If-Else or While) -Send data from Arduino to Serial Monitor -Push Buttons */ const int buttonPin=2;

int ledPin=5; int buttonState=0;

void setup () { pinMode (buttonPin, INPUT); Serial.begin(9600); }

void loop () { buttonState=digitalRead(buttonPin);

pinMode (ledPin, OUTPUT); digitalWrite (ledPin, HIGH); delay (1000); digitalWrite (ledPin, LOW); ledPin=ledPin++;

if (ledPin >10){ ledPin=5; } //the following lines of code is to establish scoring switch(ledPin){ case 'a': if(ledPin = 7){ digitalRead(buttonState);} if (buttonState==HIGH){ Serial.println("Winner");} case 'b': if(ledPin = 5,6,8,9){ digitalRead(buttonState);} if(buttonState==HIGH){ Serial.println("Loser");} } }

Case b is wrong in three ways. One, you need to use == when comparing things. Two, no commas just && or ||. Three you need to compare them individually or check to see if the ledpin is NOT 7 when pressed.

** case A is also wrong in one specific spot. Do you see your error?

But apart from that, where do the 'a' and 'b' come from in the first place, to be the cases that switch tests ledPin against?

@JimboZA Exactly. I named them 'a' and 'b' respectively for the functions specified.

@HazardsMind Oh ok that makes sense..so all I have to do is basically rewrite the "loser" case for all ledPin numbers?? So in that case there would be a total of 5 cases. I see my mistake in case 'a'. I have to use the equals sign twice(==) in the if statement. But going back to rewriting the case for all LEDs, is there a more efficient way to code this? Or is the switch statement the only way to achieve the goal that I am trying to achieve?

sahmed007: Exactly. I named them 'a' and 'b' respectively for the functions specified.

But that's not how SwitchCase works: the switching variable has to be able to take on the value of each case. How does ledPin ever become an 'a' or a 'b'?

Ohh...I was under the impression that you are supposed to put anything arbitrarily as a label for the case. I did not realize that values must be placed there. This is starting to clear up. So I have to utilize digitalRead(ledPin) instead of the 'a' and 'b' assigned?? And assign the case for each LED??

A digitalRead will give you a 1 or a 0 so you could use those I guess. I'd have been inclined to use an if-else, but a switch case on the value of the digitalRead as 0 or 1 should work.

Generally speaking you use IF or SWITCH/CASE (just a matter of preference). So with the code in Reply #1 you could probably just drop the switch/case stuff altogether.

If you want to use SWITCH/CASE then the correct way is as follows (for the situation where ledPin has the value 7. Note also the use of break; which is essential.

switch(ledPin) {
  case 7:
    // things to do
    break;
  case 8: 
 // etc etc

Also note that each CASE can only work with a single value

...R

Also note that each CASE can only work with a single value

Or a range of values, specified with ellipsis.

Robin2: Also note that each CASE can only work with a single value

Although you can cheat that by leaving out the break, as explained to me a long time back here.

It worked!!!!! I decided to stick to using the switch/case and put in the breaks which i had forgot. I apologize, I'm new to this stuff so I did not really know what to do. You guys cleared this stuff up. Thanks for the help guys!!!