Trouble Changing a Variable

Hi,
I have alot going on in this code from controlling a motor, a keypad, light curtain, and I am trying to add to write code to turn the light curtain on or off. I am trying to use a variable value to determine which code it picks to run. I have included the full code although right now I am only trying to get the Variable section to work right at the start of the loop. I wanted the variable to be at 0 initially, then while it is at 0 with the question up on the display, I would like to pick A or B which would write a 1 or 2 to the variable. I am having an odd issue. When I select B it will change the variable to 2 in the serial monitor, but when I select A it does not change it to 1. Any ideas why this would happen. Sorry about the mess of code. I know it is not clean. I am a hardware guy tinkering with software until things work. I had to cut off the light curtain section of code because it was too long. This may screw up how many brackets are at the end of the code. Thanks for the help.

#include <Keypad.h>
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>

Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

#define WHITE 0x7 //Set Up Color

 
//const byte ROWS = 4;  //4 Rows Across
//const byte COLS = 3; // 3 Columns up and down

//char hexaKeys[ROWS][COLS] = {  //Shows the pattern of the Keypad
//  {'1', '2', '3'},
  //{'4', '5', '6'},
  //{'7', '8', '9'},
// {'*', '0', '#'}
//};

//byte rowPins[ROWS] = {8, 7, 6, 5}; //Assigning Arduino Pins Used {8, 7, 6, 5}
//byte colPins[COLS] = {4, 3, 2}; // Assigning Arduino Pins Used

 
#include <Keypad.h>

const byte ROWS = 4; 
const byte COLS = 4; 
 
char hexaKeys[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}; 
 
 
char Alarm;
char keyArray[5] = {'0', '0', '0', '0'};  //
int arrayPos = 0;  //
int inputPWM = 0;  //
int inputVariable1 = 0;

//const int threshold = 1;

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 
 
int PWMoutput = 10;
//int Motorstop = 12; 

void setup(){
  Serial.begin(9600);  // baud rate
char customKey = customKeypad.getKey();  //
  lcd.begin(16, 2);  //Defines how many characters in each row and how many rows
  
  lcd.setCursor(0,0); //Setting Print to first character and first row
lcd.print ("Push A SAFE");
lcd.setCursor(0,1);
lcd.print ("Push B UNSAFE");
//  lcd.print ("Select RPM Speed");  //Printing This Text on Top Row
  //lcd.print(customKey);

  //lcd.setBacklight(WHITE);
  lcd.setCursor(0, 1); // Setting up cursor to print on first character of 2nd row
}
uint8_t i=0;  

void loop()
{
  char customKey = customKeypad.getKey();
  Serial.println(inputVariable1);
  Serial.println(customKey);
while (inputVariable1 == '0') 
  
  {if (customKey == 'A')
    inputVariable1 = 1;                                               //this is the part that I can't get to work
  if (customKey == 'B')
    inputVariable1 = 2;}
}

Start simple with a new sketch based on one of the keypad examples. Print to Serial Monitor the key that is pressed.

Do you get the expected results?

That will prove that the hardware side is OK or not.

Yes, in the serial monitor I am seeing that the correct key is pressed and read correctly, but the variable is not changing when the A is pressed only when the B is pressed.

You REALLY need to learn the proper location for curly-braces. Here is how the compiler looks at your code:

while (inputVariable1 == '0')
{
    if (customKey == 'A')
        inputVariable1 = 1;
}                                               //this is the part that I can't get to work

if (customKey == 'B')
    inputVariable1 = 2;

The first "if" is the ONLY one within the while loop.

The second "if" is OUTSIDE the while loop.

I'm guessing that is not what you want...

Regards,
Ray L.

You are correct. Still looking for a source on the curly-braces that I can clearly understand. I have read over a few, but they don't seem to go deep into it. I did edit the original code above to simplify and try your suggestion, but it didn't work. I can see the variable changing if I comment out the While line. When I press A it changes the variable to 1 and B changes the variable to 2. Any ideas why this does not work with the while statement? Thanks

Perhaps something to do with the fact that nowhere in your code do you ever set inputVariable1 = '0'?? You initialize it to 0, but not to '0'. 0 and '0' have two completely different numeric values, because one is a binary integer, and the other is an ASCII-encoded character.

Braces group code. An if/while/for/whatever will act on only a single line of code, if you don't use braces. If you want them to act on more than one line of code, you have to enclose all those lines within a matched pair of braces. Look at your original code. How could the compiler possibly know you wanted BOTH if's to be within the while? Now look at the code I posted. It's very clear - the while is IMMEDIATELY followed by a {, then the two is statements, then a }. It is explicitly clear the BOTH if statements are within the while loop.

Regards,
Ray L.

Good eye, thanks for the help. I also needed to put the Customkeypad inside the loop for it to work. I will try to be more careful as I slowly learn this language.

while (inputVariable1 == 0)
{char customKey = customKeypad.getKey();
if (customKey == 'A')
inputVariable1 = 1; //this is the part that I can't get to work
if (customKey == 'B')
inputVariable1 = 2;}

You need to do some simple debugging:

Serial.print("start: inputVariable1=");
Serial.print(inputVariable1);
while (inputVariable1 == 0)
{
    char customKey = customKeypad.getKey();
    Serial.print(" customKey=);
    Serial.println(customKey);
    
    if (customKey == 'A')
    {
        Serial.println("Setting inputVariable1=1);
        inputVariable1 = 1;
    }
    else if (customKey == 'B')
    {
        Serial.println("Setting inputVariable1=2);
        inputVariable1 = 2;
    }
    Serial.print("end: inputVariable1=");
    Serial.print(inputVariable1);

    delay(1000);  // Don't swamp Serial
}
Serial.println("exited while");

Regards,
Ray L.