I think I am setting boolean values correctly but...

Hi,

When I am running this code it works, up until I press the # key, variables GameA and GameB are Boolean, when the a key is pressed gameA should be set to true and gameB to false and vice versa when B is pressed. when I press a or b the screen changes to indicate that they have been pressed, but then when I press the #(to confirm and move on in this case) both gamea and gameb are still 0 therefore the if statement I want to run is not working.

I would appreciate if someone could have a look and help me out

many thanks

Dexter

//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

// boolean variable to determine if a game type hase been selected and which game type it was
boolean gameA = false;
boolean gameB = false;
// gametype string will be used throughout code, hence it is declared up here
String gametype = " ";


const byte rows = 4;
const byte cols = 4;


char keys[rows][cols] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[rows] = {9,8,7,6};
byte colPins[cols] = {5,4,3,2};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
  lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0,0); // set cursor to first character ont he first line
  lcd.clear(); // clear anything that may be on the lcd screen
  for(int i = 1; i <4; i++ ) { // creates blinking text - start of for loop complete the loop until i = 4, i increments each time the loop is run 
    lcd.print("Initializing!");
    delay(100); //delay for a time
    lcd.clear();
    delay(100);
  }
  lcd.print("Complete!");
  delay(1000);
  lcd.clear();
  lcd.print("Select Game");
  lcd.setCursor(0,1); //change cursor location to second line
  lcd.print("A=KOH B=Bomb");  
}
void loop()
{
 

  char key = keypad.getKey();
  int displaychosengame; // variable used to call the routine ChooseGame
  
   if (int(key) != 0) {
     gametype = " " ;
     if (key == 65) // if character pressed = A(ASCII 65) set the game type to A
     { 
       boolean gameA = true; // set the game type indicator to game a
       boolean gameB = false; // ensure game type indicator is not game b
       gametype = "King of the Hill"; //Set game type text to King of the hill
       displaychosengame = ChooseGame(); // call routine to display the game details chosen

     }
     else if (key == 66) // if character pressed = B(ASCII 66) set the game type to B
     {
       boolean gameB = true; // set the game type indicator to game b
       boolean gameA = false; // ensure game type indicator is not game a
       gametype = "Bomb"; //Set game type text to King of the hill
       displaychosengame = ChooseGame(); // call routine to display the game details chosen

     }
      if (key == 35) // if character pressed = #(ASCII 35)
       {
         //if (gameA != false && gameB != false) 
          //{   
           lcd.clear();
           lcd.setCursor(0,0);
           // lcd.print("Set Game Time");
           lcd.print(gameA); //display content of gameA for testing purposes
           lcd.print(gameB); // display content of gameb for testing purposes
          //}
       }
   }
  }

int ChooseGame() // this routine simplay displays on LCD 1 the selected game type
{
       lcd.clear();
       lcd.setCursor(0,0);
       lcd.print("Chosen Game");
       lcd.setCursor(0,1);
       lcd.print(gametype);
}

Fixed

if (key == 65) // if character pressed = A(ASCII 65) set the game type to A

If you write it if (key == 'A') you don't need the massive comment, and it is easier to change and understand.

You didn't say what the solution was, but I'm going to say "scope"

You didn't say what the solution was, but I'm going to say "scope"

I'm going with the global and local variables of the same name.

Just for reference you can shorten and speed up your if/then command.
if/then basically only verifies if the operands are true, since you are working with Boolean variables the state is already defined.

You have:

if (gameA != false && gameB != false) 
//  "!= false" adds about 2 clock cycles to the equation for each instance

can be shortened to:

if ( gameA && gameB ) {  // if both are true
// or
if ( !gameA && gameB ) {  //  A=false and B=true
// the " ! " is a "not" comparator and by appending it to the Boolean variable you are inverting the state making a false report as true
// or 
if( !gameA == gameB ) { // This does not care which one is true, only that they are opposite of each other

Likewise:

 if (int(key) != 0)

can be shortened to:

 if (int(key))

Given the code I see no reason it can't simply be -

if ( key )

lloyddean:
Given the code I see no reason it can't simply be -

if ( key )

He originally cast 'key' as a char. Would 'if(key)' work without recasting it?

Yes. Anything that doesn't evaluate to 0 (false), is non 0 (or not false) so considered true.

Thank you all for the reply's my code will be better after reading this for sure