Help with A ODD or EVEN Gambling Game

Hi, I am in need of some expertise in finishing a ODD or Even gambling game. My game features two 1602 and 4 sanwa style arcade buttons. So far I have accomplished getting the odd or even to print to the first screen using one buttons to roll the dice so to speak and another to reset and start again. After hitting the first button it displays odd or even on the first screen and reset button starts it over. However I am at a loss on how to use other two buttons to pick odd for one button or even for the other button and then have a tally of how many correct guesses or if the house is wining how many incorrect guesses the player is down and have that print to the second screen.

Here is a fritzing of my wiring.

and here is my code so far

#include <LiquidCrystal.h>
#include <JC_Button.h> 
const byte Pin_button = (8);
const byte Pin_button2 = (9);
const byte Pin_button3 = (6);
const byte Pin_button4 = (7);
Button BUTTON2 (Pin_button2);
Button BUTTON1 (Pin_button);
Button BUTTON3 (Pin_button3);
Button BUTTON4 (Pin_button4);
long roll;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
LiquidCrystal lcd2(A5, A4, A3, A2, A1, A0);

void setup() {
  BUTTON1.begin();
  BUTTON2.begin();
  BUTTON3.begin();
  BUTTON4.begin();
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(0,0);
  lcd.print("Welcome Gambler");
  lcd.setCursor(0,1);
  lcd.print("ODD or EVEN");
  lcd2.begin(16, 2);
  // Print a message to the LCD.
  lcd2.setCursor(0,0);
  lcd2.print("Choose Now");
  lcd2.setCursor(0,1);
  lcd2.print("ODD or EVEN");
}

void loop() {
 Checkbuttons();

}

void Checkbuttons(){
  BUTTON1.read();
  if (BUTTON1.wasReleased()) Roll();
  BUTTON2.read();
  if (BUTTON2.wasReleased()) reset();
  //BUTTON3.read();
  //if (BUTTON3.wasReleased()) oneOdd();
 // BUTTON4.read();
  //if (BUTTON4.wasReleased()) oneEven();
    
}

void Roll(){
  lcd.clear();
  roll = random(2);
  if (roll == 0){
    lcd.print("Even WINS");
  }else if (roll == 1){
    lcd.print("Odd WINS");
    }
}

void reset(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Play Again");
  lcd.setCursor(0,1);
  lcd.print("Place Your Bets");
}

void oneOdd(){
  
}

void oneEven();{
  
}

Any help or direction on this would be much appreciated.

const byte Pin_button = (8);
const byte Pin_button2 = (9);
const byte Pin_button3 = (6);
const byte Pin_button4 = (7);

Is that how you count? Uh-huh, two, three, four? That's not how I count.

(Why) (are) (the) (numbers) (in) (parentheses) (?)

long roll;

So you can roll a -4542626?

  //BUTTON3.read();
  //if (BUTTON3.wasReleased()) oneOdd();
 // BUTTON4.read();
  //if (BUTTON4.wasReleased()) oneEven();

Why is this stuff commented out?

  roll = random(2);

You need a long to store a value of either 0 or 1?

  }else if (roll == 1){

Why the useless second test? If the value was not 0, there isn't a chance in hell that it is not 1.

void oneEven();{

What;is;that;semicolon;doing;in;there;?

Thanks for the tips. I will clean up my code and post an update, but believe it or not that code runs the first screen and phase one of my game just fine. Most of the code comes from other projects I have done. My real issue is coming up with a way of using the 3rd and 4th button to pick between odd or even and then have the second screen keep track of the score.

Ok here is that code fixed up like you said.

#include <LiquidCrystal.h>
#include <JC_Button.h>
const byte Pin_button = 8;
const byte Pin_button2 = 9;
const byte Pin_button3 = 6;
const byte Pin_button4 = 7;
Button BUTTON2 (Pin_button2);
Button BUTTON1 (Pin_button);
Button BUTTON3 (Pin_button3);
Button BUTTON4 (Pin_button4);
int roll;
int score;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
LiquidCrystal lcd2(A5, A4, A3, A2, A1, A0);

void setup() {
  BUTTON1.begin();
  BUTTON2.begin();
  BUTTON3.begin();
  BUTTON4.begin();
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Welcome Gambler");
  lcd.setCursor(0, 1);
  lcd.print("ODD or EVEN");
  lcd2.begin(16, 2);
  lcd2.setCursor(0, 0);
  lcd2.print("Scoring Tally");
  lcd2.setCursor(0, 1);
  lcd2.print(score);
}

void loop() {
  Checkbuttons();
  int i = 0;
  

}

void Checkbuttons() {
  BUTTON1.read();
  if (BUTTON1.wasReleased()) Roll();
  BUTTON2.read();
  if (BUTTON2.wasReleased()) reset();
  BUTTON3.read();
  if (BUTTON3.wasReleased()) oneOdd();
  BUTTON4.read();
  if (BUTTON4.wasReleased()) oneEven();

}

void Roll() {
  lcd.clear();
  roll = random(2);
  if (roll == 0) {
    lcd.print("Even WINS");
  } else {
    lcd.print("Odd WINS");
  }
}

void reset() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Play Again");
  lcd.setCursor(0, 1);
  lcd.print("Place Your Bets");
}

void oneOdd() {
 
}

void oneEven() {
  
}

My real issue is coming up with a way of using the 3rd and 4th button to pick between odd or even

If you can use the 1st and 2nd for roll and reset, I see no reason why you can's use the 3rd and 4th for odd and even. You'll need to explain what is stopping you.

and then have the second screen keep track of the score.

That is not the responsibility of the LCD. Keeping track of the score is the Arduino's responsibility. Telling the LCD to display the score is the Arduino's responsibility. Actually displaying some text is the LCD's only responsibility.

PaulS:
If you can use the 1st and 2nd for roll and reset, I see no reason why you can's use the 3rd and 4th for odd and even. You'll need to explain what is stopping you.
That is not the responsibility of the LCD. Keeping track of the score is the Arduino's responsibility. Telling the LCD to display the score is the Arduino's responsibility. Actually displaying some text is the LCD's only responsibility.

Not real sure how to have the "Arduino keep track of the score" my programing skills are weak and I need some direction with how to code it. I know I want the arduino keep track of the score but I want that scored to be on the second screen, and for it to keep a running tally of the score. That way the game knows how many your up or how many your down. So the arduino would need to be able to keep a negative number or a positive number that changes with every round of the game.

So you boot up and it displays the intro text on screen 1 and on screen 2 it displays your score which would start at 0.

Then I want to have a selection on button 3 and 4 for choosing odd or even. The arduino would have to then compare that choice to the random result of the button 1 which "rolls the dice so to speak". Then Button 2 needs to reset with out altering the arduino's track of the score which is displayed on screen 2.

Not real sure how to have the "Arduino keep track of the score" my programing skills are weak and I need some direction with how to code it.

   int score = 42; // Or some other initial value

<snip>

if(somethingHappened())
   score += somePoints;
else if(youLose())
   score -= someOtherNumberOfPoints;

lcd2.print(score);

Can a "if" statement have more than one condition to check to see if true?

moJoeRedRog:
Can a "if" statement have more than one condition to check to see if true?

Yes, although compound if statements can be hard to debug. Nested if statements can usually accomplish the same thing, and be easier to debug.

No. But you can use boolean operators such as && (and) and || (or) to combine multiple expressions and give a single result.

Yes i have applied that to my code, Here is the updated code but it still has not accomplished my goal. I need a way to store the value of a odd button press or an even button press and then compare that to the roll result.

#include <LiquidCrystal.h>
#include <JC_Button.h>
const byte Pin_button = 8;
const byte Pin_button2 = 9;
const byte Pin_button3 = 6;
const byte Pin_button4 = 7;
Button BUTTON2 (Pin_button2);
Button BUTTON1 (Pin_button);
Button BUTTON3 (Pin_button3);
Button BUTTON4 (Pin_button4);
int roll;
int score = 0;
int odd;
int even;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
LiquidCrystal lcd2(A5, A4, A3, A2, A1, A0);

void setup() {
  BUTTON1.begin();
  BUTTON2.begin();
  BUTTON3.begin();
  BUTTON4.begin();
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Welcome Gambler");
  lcd.setCursor(0, 1);
  lcd.print("ODD or EVEN");
  lcd2.begin(16, 2);
  lcd2.setCursor(0, 0);
  lcd2.print("Scoring Tally");
  lcd2.setCursor(0, 1);
  lcd2.print(score);
}

void loop() {
  Checkbuttons();
  if (roll == 0 && odd == 0) {
    score ++;
  } else if (roll == 0 && even == 1) {
    score --;
  } else if (roll == 1 && odd == 0) {
    score --;
  } else if (roll == 1 && even == 1) {
    score ++;
  }
  lcd2.setCursor(0, 1);
  lcd2.print (score);

}

void Checkbuttons() {
  BUTTON1.read();
  if (BUTTON1.wasReleased()) Roll();
  BUTTON2.read();
  if (BUTTON2.wasReleased()) reset();
  BUTTON3.read();
  if (BUTTON3.wasReleased()) oneOdd();
  BUTTON4.read();
  if (BUTTON4.wasReleased()) oneEven();

}

void Roll() {
  lcd.clear();
  roll = random(2);
  if (roll == 0) {
    lcd.print("Even WINS");
  } else {
    lcd.print("Odd WINS");
  }
}

void reset() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Play Again");
  lcd.setCursor(0, 1);
  lcd.print("Place Your Bets");
}

void oneOdd() {
  int odd = 0;
}

void oneEven() {
  int even = 1;
}

I need a way to store the value of a odd button press or an even button press and then compare that to the roll result.

The value of a button press is HIGH or LOW, depending on how the switch is wired. WHICH button was pressed is a different story, but you know whether the odd switch was pressed, or not, and whether the even switch was pressed.

const byte Pin_button = 8;
const byte Pin_button2 = 9;
const byte Pin_button3 = 6;
const byte Pin_button4 = 7;

Why are these not all numbered, while

Button BUTTON2 (Pin_button2);
Button BUTTON1 (Pin_button);
Button BUTTON3 (Pin_button3);
Button BUTTON4 (Pin_button4);

these are?

Consistency is a good thing.

However, the numbering contributes nothing. BUTTON_ODD, BUTTON_RESET, BUTTON_ROLL, and BUTTON_EVEN would make a lot more sense.

  if (roll == 0 && odd == 0) {
    score ++;
  } else if (roll == 0 && even == 1) {
    score --;
  } else if (roll == 1 && odd == 0) {
    score --;
  } else if (roll == 1 && even == 1) {
    score ++;
  }

The global odd and even variables are never assigned new values.

void oneOdd() {
  int odd = 0;
}

void oneEven() {
  int even = 1;
}

These local variables immediately go out of scope, so assigning them values is useless.

The oneOdd() and oneEven() functions need to assign values to BOTH variables. Which indicates that you only need one variable. oneOdd() should set a boolean variable, matched, for instance, to true if roll is 1, and false if roll is 0. oneEven should do the same, but with opposite values.

I am updating my code, I have a question though would this program be all in the loop or would functions be useful.

If you have more than about 40 lines of code, functions are useful.

Ok I have almost got it to work or at least made some progress. I can use the odd or even buttons to get a result but its always 2, not sure if random() is working for me. Then my reset button starts it all over again. However my score variable does not change and is just displayed as 0. Here is the code.

#include <LiquidCrystal.h>
#include <JC_Button.h>
const byte Pin_button1 = 8;
const byte Pin_button2 = 9;
const byte Pin_button3 = 6;
const byte Pin_button4 = 7;
Button ROLLDICE (Pin_button1);
Button RESETGAME (Pin_button2);
Button EVEN (Pin_button3);
Button ODD (Pin_button4);
int roll;
int score;
int odd;
int even;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
LiquidCrystal lcd2(A5, A4, A3, A2, A1, A0);

void setup() {
  ROLLDICE.begin();
  RESETGAME.begin();
  EVEN.begin();
  ODD.begin();
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Welcome Gambler");
  lcd.setCursor(0, 1);
  lcd.print("TO ODD or EVEN");
  lcd2.begin(16, 2);
  lcd2.setCursor(0, 0);
  lcd2.print("Scoring Tally");
  lcd2.setCursor(0, 1);
  lcd2.print(score);
  delay(1000);
}

void loop() {
  checkbuttons();
}

void checkbuttons() {
  int score;
  ODD.read();
  if (ODD.wasReleased()) oddRoll();
  EVEN.read();
  if (EVEN.wasReleased()) evenRoll();
  RESETGAME.read();
  if (RESETGAME.wasReleased()) freshStart();

}

void oddRoll() {
  int j = 2;
  if  (random(j) == 0) {
    score + 1;
  } else {
    score - 1;
  } lcd2.setCursor(0, 1);
  lcd2.print(score);
  lcd.clear();
  lcd.print (j);
  delay (1500);
}

void evenRoll() {
  int i = 2;
  if (random(i) == 1) {
    score + 1;
  } else {
    score - 1;
  } lcd2.setCursor(0, 1);
  lcd2.print(score);
  lcd.clear();
  lcd.print(i);
  delay(1500);
}

void freshStart () {
  lcd.clear();
  lcd.setCursor (0, 0);
  lcd.print("BET AGAIN!");
  lcd.setCursor(0, 1);
  lcd.print("Odd or Even");

}

I have removed the ROLLDICE button and just went with picking the ODD or EVEN button rolling the dice so to speak.

oddRoll() can roll an even number. That is a weird choice for a function name.

PaulS:
oddRoll() can roll an even number. That is a weird choice for a function name.

It is named after the selection of ODD for the ROLL as in if you pick ODD instead of picking EVEN. and ya a roll can go either way hence the name Odd or Even. Also you score a point if you pick oddRoll and the number is odd. If you pick oddRoll and the number is even hey -1, so can you see the logic behind the name. It does not really even matter what the functions name is as long as it does what I want it to. Help in doing that would be more appreciated than insults about my variable and function names.

Just to clear up any confusion about what the goals of this small project are simple.

First It boots and displays a message on screen 1, Welcome Gambler, to Odd or Even.

Then it asks you to choose odd or even.

Then you press a button 1 is for odd and 2 is for even. Once you press 1 or 2 it chooses a random number from 1 to 2 and compares it to what you picked. odd or even.

Then on the second screen a tally is kept of how many you are up or down and a third button resets the game but not the score. I am open to completing this what ever way is easy. I have removed the press to roll button as it was making things complicated I know its still in my code but will be removed.

If my code is in the ball park and you can see my error please help. Even new direction in doing it in a different way I am open to because my way does not work.

I can't see what your code is actually doing, and you haven't done a very good job of describing what it is doing.

userSelectedOdd() or userSelectedEven() would make more sense. userSelected(byte num) would be even better. There is no reason to have two functions that do nearly identical things.

Even though the goal is a standalone unit, not connected to a PC, the PC and serial port should be used to debug the sketch.

What did the user select (ODD or EVEN)? What did the random number generator generate (odd or even)? What did the score do?

There are so many unanswered questions that it is difficult to know what to suggest.

As a student I am always learning. I have updated the code with Comments describing what I am attempting in the sketch. Really appreciate the instruction on this project. Here is my code.

#include <LiquidCrystal.h>
#include <JC_Button.h>
//const byte Pin_button1 = 8;
const byte Pin_button2 = 9;
const byte Pin_button3 = 6;
const byte Pin_button4 = 7;
//Button ROLLDICE (Pin_button1);
Button RESETGAME (Pin_button2);
Button EVEN (Pin_button3);
Button ODD (Pin_button4);
// Declare My Global Variables 
int roll;
int score;
int odd;
int even;
//initialize my two LCD 1602 screens
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
LiquidCrystal lcd2(A5, A4, A3, A2, A1, A0);

void setup() {
  Serial.begin(9600);
  //ROLLDICE.begin();
  RESETGAME.begin();
  EVEN.begin();
  ODD.begin();
  //Begin My screens with their opening Messages
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Welcome Gambler");
  lcd.setCursor(0, 1);
  lcd.print("TO ODD or EVEN");
  lcd2.begin(16, 2);
  lcd2.setCursor(0, 0);
  lcd2.print("Scoring Tally");
  lcd2.setCursor(0, 1);
  lcd2.print(score);
  delay(1000);
}

void loop() {
  
  checkbuttons();
}

void checkbuttons() {
  
  ODD.read(); //read the odd button
  if (ODD.wasReleased()) userSelectedOdd();
  EVEN.read(); // read the even button
  if (EVEN.wasReleased()) userSelectedEven();
  RESETGAME.read(); // read the reset to a fresh round button
  if (RESETGAME.wasReleased()) freshStart();

}
// function for an odd selection that also does the rolling
void userSelectedOdd() {
  int j = 2; // a local variable that will be used to radomize does 
  //not seem to be mutable I do want it mutable
  if  (random(j) == 1) {
    score + 1; // if the j after being randomized is = to 1 score goes up
  } else {
    score - 1;  //else the score goes down one.  score is a variable
    //that is needed to change
  } lcd2.setCursor(0, 1);
  lcd2.print(score); //print the score on my second screen
  // always is zero the variable is not changing.
  lcd.clear();
  lcd.print (j);  //print the result of the roll on 1st screen.
  delay (1500);
}

void userSelectedEven() {
  int i = 2; // a local variable to be radomized in the function
  if (random(i) == 2) {
    score + 1; // if the random result of randomizing i is even
    // score goes up else it goes down
  } else {
    score - 1;
  } lcd2.setCursor(0, 1);
  lcd2.print(score);
  lcd.clear();
  lcd.print(i);
  delay(1500);
}

void freshStart () {
  lcd.clear(); //simple enought function that clears screen one
  // and gives a fresh start.
  lcd.setCursor (0, 0);
  lcd.print("BET AGAIN!");
  lcd.setCursor(0, 1);
  lcd.print("Odd or Even");

}
  if  (random(j) == 1) {
    score + 1; // if the j after being randomized is = to 1 score goes up
  } else {
    score - 1;  //else the score goes down one.  score is a variable
    //that is needed to change

To increment score, you need either:

   score = score + 1;

or

   score++;

Similarly, to decrement score, you need either:

   score = score - 1;

or

   score--;

Your code is not currently changing the score.

  int i = 2; // a local variable to be radomized in the function
  if (random(i) == 2) {

The values returned by random(), when the only argument is 2 are 0 or 1, not 1 or 2.