Reputation: 11
I am trying to build the game for the kids, I have looked a few other ways to play the game but I am doing it differently.
I am using 3 buttons for each player for Rock, Paper and Scissor. Players would push a button at the same time and whoever wins will get his LED ON.
The issue is, any button that meets one of the if statements condition is pressed, the light will turn on even though it's an "AND" condition.
for now I only have one possibility for R1 && S2 buttons pressed which yield in player 1 wins (R > S), but when I pressed the buttons individually, the LED still turns on, it is not respecting the AND condition.
What am I doing wrong?
//
// Rock Paper Scissor Game
//
// Each player gets 3 buttons for R, P and S.
//
// If player 1 presses R and player 2 presses S, led on W1 will turn on indicating player 1 wins
//
int R1 = 3; //Rock player 1
int P1 = 4; //Paper player 1
int S1 = 5; //Scissor player 1
int R2 = 6;
int P2 = 7;
int S2 = 8;
int W1 = 9; //player 1 wins led
int W2 = 10; //player 2 wins led
void setup() {
//input buttons for player 1
pinMode(R1, INPUT_PULLUP);
pinMode(P1, INPUT_PULLUP);
pinMode(S1, INPUT_PULLUP);
//input buttons for player 2
pinMode(R2, INPUT_PULLUP);
pinMode(P2, INPUT_PULLUP);
pinMode(S2, INPUT_PULLUP);
pinMode(W1, OUTPUT);
pinMode(W2, OUTPUT);
pinMode(13, OUTPUT);
}
void loop() {
//Reading push buttons
int x1 = digitalRead(R1); //player 1 choses Rock
Serial.println(x1);
int x2 = digitalRead(P1);
Serial.println(x2);
int x3 = digitalRead(S1);
Serial.println(x3);
int y1 = digitalRead(R2);
Serial.println(y1);
int y2 = digitalRead(P2);
Serial.println(y2);
int y3 = digitalRead(S2); //player 2 choses Scissor
Serial.println(y3);
//first possibility if player 1 choses R and player 2 choses S.
if (x1 == HIGH && y3 == HIGH)
digitalWrite(9, LOW); //Player 1 LED on indicating winning
else //is this else necessary
digitalWrite(9, HIGH);
}
Upvotes: 1
Views: 378
Reputation: 1835
What if a player presses more than one button? What if a player quickly changes her choice?
In general, with 6 buttons, there are just 2^6 = 64 possibilities, most of them "invalid entry", 6 "not yet ready" (player 1 or 2 have not pressed any button yet), 3 tie, 3 wins for player1 and 3 wins for player2.
Set up a table with 64 entries and you're done. You can even expand that to show which player is trying to cheat or has not yet responded :)
Upvotes: 0
Reputation: 51
your code should result in the LED being on LOW per default, because the input pins are set to HIGH as pull-ups: https://www.arduino.cc/en/Tutorial/InputPullupSerial
if (x1 == HIGH && y3 == HIGH)
{
digitalWrite(9, LOW);
}
else
{
digitalWrite(9, HIGH);
}
This means that all your integer variables are fulfilling a (x_i==HIGH && y_j==HIGH) statement. You then proceed to press a button, which is resulting in the variable that is read from that button to be set on LOW.
With now one variable being HIGH and the one whose button you just pressed being LOW you end up in the else branch of your if statement. This is turning your LED on.
You could make your idea work like this (LOW is the signal that a button is pressed):
if(x1==LOW && y3==LOW)
{
digitalWrite(9,HIGH); //Rock wins over Scissor
delay(2000);
}
.... all the Rest of the Button combinations......
digitalWrite(9,LOW);
digitalWrite(10,LOW);
The loop is probably faster than the any player pressing buttons, so you should make sure a winning condition is never fulfilled if only one button is pressed at the same time.
Upvotes: 1
Reputation: 3066
A partial reply regarding how you handle your inputs:
As your inputs are pull-ups, when you press a button, it should make a connection from the pin to ground.
This means you must connect the normally open pins of your button between the pin and ground.
Now when you read a pin, it will read as 1
or HIGH
when the button is not pressed, as it is pulled up by default. And it will read as 0
or LOW
when the button is pressed, as there is now a path between the pin and ground.
There is an arduino tutorial specifically on this topic: https://www.arduino.cc/en/Tutorial/DigitalInputPullup
Upvotes: 1