Using switch statement to toggle bool variable using matrix keypad input

This is my first time posting in the Arduino forum. I am building a simple controller with an 20x4 lcd display and 4x4 matrix keypad input. I declared a global variable called bool eject that is initially set to false. when the “C” key is pressed it displays two options. One is to press the “#” key to toggle the value to true, the other is to press the “" key which sets the value to false and both inputs return the display to the main screen.
I attempted to do so by calling a function with a switch statement. In the case"#": the variable is set to true, and case"
”: the variable is set/remains false.

When I run the code the display is correct and an input of either “#” or “*” returns the display to the main screen, but the eject value has not changed.

Update: I have posted the entire program.

I am relatively new to Arduino programming so there are many things that I do not yet know or understand. Any guidance is greatly appreciated.

#include <Keypad.h>
#include <Wire.h>
#include <PCF8574.h>
#include <LiquidCrystal_I2C.h>

#define lcdi2caddress 0x27

PCF8574 pcf_01(0x22);
PCF8574 pcf_02(0x21);

bool eject = false;
bool runmode = false;

long total = 25;
volatile long done = 0;
long current1 = total;
long current2 = done;
const byte interrupt = 2;

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]={4,5,6,7};
byte colPins[cols]={8,9,10,11};

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

LiquidCrystal_I2C lcd(lcdi2caddress,20,4);

void setup()
{
pcf_02.begin();
pcf_01.begin();
pcf_01.write8(LOW);

lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight

pinMode(interrupt, INPUT);
attachInterrupt(1, count, FALLING);

lcd.setCursor(0,0);
delay(1000);
lcd.print("***** ");
lcd.setCursor(0,1);
lcd.print("
");
lcd.setCursor(0,2);
lcd.print("
");
lcd.setCursor(0,3);
lcd.print("
**************");
delay(1000);
lcd.setCursor(7,0);
lcd.print(" “);
delay(750);
lcd.setCursor(5,1);
lcd.print(” “);
delay(750);
lcd.setCursor(4,2);
lcd.print(” ");
delay(3000);
lcd.clear();
lcd.setCursor(2,0);
lcd.print(“Counter”);
lcd.setCursor(0,2);
lcd.print(“TOTAL:”);
lcd.setCursor(0,3);
lcd.print(“DONE:”);
lcd.setCursor(7,2);
lcd.print(total);
lcd.setCursor(7,3);
lcd.print(done);
}

void loop()
{

if(current2 != done) //Updates the current2 volatile long integer used for the counter ISR
{
mainScreen();
current2 = done;
}

if(current1 != total) //Updates the current1 volatile long integer used for the counter ISR
{
mainScreen();
current1 = total;
}

if(done < 0) //Prevents the long integer done from holding a value less than 0
{
done = 0;
}

if(total < 1) //Prevents the total stack size from holding a value less than 1
{
total = 1;
}

if(done >= total) //Resets done items to 0
{
done = 0;
}
char key = keypad.getKey();

if(key == ‘A’)
{
runmode = false;

lcd.clear();
lcd.setCursor(2,0);
lcd.print(“Enter Total”);
lcd.setCursor(2,1);
lcd.print(“Then Press #”);
lcd.setCursor(2,2);
lcd.print(“Press * To Clear”);
total = GetNumber();
mainScreen();
}
if(key == ‘B’)
{
runmode = false;

lcd.clear();
lcd.setCursor(2,0);
lcd.print(“Enter Parts Done”);
lcd.setCursor(2,1);
lcd.print(“Then Press #”);
lcd.setCursor(2,2);
lcd.print(“Press * To Clear”);
done = GetNumber();
mainScreen();
}
if(key == ‘C’)
{
runmode = false;
lcd.clear();
lcd.setCursor(2,0);
lcd.print(“Ejection Sequence”);
lcd.setCursor(2,1);
lcd.print("# To Eject");
lcd.setCursor(2,3);
lcd.print("* To Cancel");

eject = manualeject();

}
if(key == ‘D’)
{
runmode = !runmode;
mainScreen();

}
if(key == ‘2’)
{
done++;
mainScreen();
}
if(key == ‘8’)
{
done–;
mainScreen();
}
if(key == ‘4’)
{
total–;
mainScreen();
}
if(key == ‘6’)
{
total++;
mainScreen();
}

}

void mainScreen()
{
lcd.clear();
lcd.setCursor(2,0);
lcd.print(“Stacker/Counter”);
lcd.setCursor(0,2);
lcd.print(“TOTAL:”);
lcd.setCursor(0,3);
lcd.print(“DONE:”);
lcd.setCursor(7,2);
lcd.print(total);
lcd.setCursor(7,3);
lcd.print(done);

if(eject == true)
{
lcd.setCursor(15,2);
lcd.print(“eject”);
}
else
{
lcd.setCursor(15,2);
lcd.print(" “);
}
if(runmode == true)
{
lcd.setCursor(15,3);
lcd.print(“Auto”);
}
else
{
lcd.setCursor(15,3);
lcd.print(” ");
}
}

long GetNumber()
{
lcd.setCursor(15,3);
long num = 0;
char key = keypad.getKey();
while(key != ‘#’)
{
switch (key)
{
case NO_KEY:
break;

case ‘0’: case ‘1’: case ‘2’: case ‘3’: case ‘4’:
case ‘5’: case ‘6’: case ‘7’: case ‘8’: case ‘9’:
lcd.print(key);
num = num * 10 + (key - ‘0’);
break;

case ‘*’:
num = 0;
lcd.setCursor(15,3);
lcd.print(" ");
lcd.setCursor(15,3);
break;

break;
}

key = keypad.getKey();
}

return num;
}

void count()
{
static unsigned long last_millis = 0;
unsigned long m = millis();

if (m - last_millis < 100)
{

}

else
{
done++;
}

last_millis = m;
}

bool manualeject()
{
bool ejectOK = false;

char key = keypad.getKey();

while(key != ‘#’ && key != ‘*’)
{
switch(key)
{
case NO_KEY:
break;

case’1’: case’2’: case’3’: case’A’:
case’4’: case’5’: case’6’: case’B’:
case’7’: case’8’: case’9’: case’C’:
case’0’: case’D’:
break;

case’#’:
ejectOK = true;
break;

case’*’:
eject = false;
break;

}
key = keypad.getKey();
}
mainScreen();
return ejectOK;
}

Which bool eject? The one from global scope defined before the loop function? Or the one at local scope in the manualeject function?

Having two variables with the same name in the same scope is a pretty silly thing to do and is a recipe for failure.

How do you know it isn't being set? You never print it or do anything based on it so how do you know what value it has?

Please take a minute to read the "How to use this forum - please read" post at the top of any board. In the programming and electronics game it really is crucial that you be the type of person who reads the instructions first.

Wait, that doesn't even compile. You're using key in loop but you don't have any variable by that name in loop.

What are you up to here? That isn't the real code you're asking about is it? Why are you sending us on a wild goose chase after some code that isn't even your real code?

If you want to try to strip something down to make it smaller, at least make sure it will compile and run and exhibit the problem.

Ok. Sorry about that. Here is the whole program. I am not using the serial monitor, but have constructed the controller and use the LCD display to test the program functions.

#include <Keypad.h>
#include <Wire.h>
#include <PCF8574.h>
#include <LiquidCrystal_I2C.h>

#define lcdi2caddress 0x27

PCF8574 pcf_01(0x22);
PCF8574 pcf_02(0x21);

bool eject = false;
bool runmode = false;

long total = 25;
volatile long done = 0;
long current1 = total;
long current2 = done;
const byte interrupt = 2;

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]={4,5,6,7};
byte colPins[cols]={8,9,10,11};

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

LiquidCrystal_I2C lcd(lcdi2caddress,20,4);

void setup()
{
pcf_02.begin();
pcf_01.begin();
pcf_01.write8(LOW);

lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight

pinMode(interrupt, INPUT);
attachInterrupt(1, count, FALLING);

lcd.setCursor(0,0);
delay(1000);
lcd.print("***** ");
lcd.setCursor(0,1);
lcd.print("
");
lcd.setCursor(0,2);
lcd.print("
");
lcd.setCursor(0,3);
lcd.print("
**************");
delay(1000);
lcd.setCursor(7,0);
lcd.print(" “);
delay(750);
lcd.setCursor(5,1);
lcd.print(” “);
delay(750);
lcd.setCursor(4,2);
lcd.print(” ");
delay(3000);
lcd.clear();
lcd.setCursor(2,0);
lcd.print(“Counter”);
lcd.setCursor(0,2);
lcd.print(“TOTAL:”);
lcd.setCursor(0,3);
lcd.print(“DONE:”);
lcd.setCursor(7,2);
lcd.print(total);
lcd.setCursor(7,3);
lcd.print(done);
}

void loop()
{

if(current2 != done) //Updates the current2 volatile long integer used for the counter ISR
{
mainScreen();
current2 = done;
}

if(current1 != total) //Updates the current1 volatile long integer used for the counter ISR
{
mainScreen();
current1 = total;
}

if(done < 0) //Prevents the long integer done from holding a value less than 0
{
done = 0;
}

if(total < 1) //Prevents the total stack size from holding a value less than 1
{
total = 1;
}

if(done >= total) //Resets done items to 0
{
done = 0;
}
char key = keypad.getKey();

if(key == ‘A’)
{
runmode = false;

lcd.clear();
lcd.setCursor(2,0);
lcd.print(“Enter Total”);
lcd.setCursor(2,1);
lcd.print(“Then Press #”);
lcd.setCursor(2,2);
lcd.print(“Press * To Clear”);
total = GetNumber();
mainScreen();
}
if(key == ‘B’)
{
runmode = false;

lcd.clear();
lcd.setCursor(2,0);
lcd.print(“Enter Parts Done”);
lcd.setCursor(2,1);
lcd.print(“Then Press #”);
lcd.setCursor(2,2);
lcd.print(“Press * To Clear”);
done = GetNumber();
mainScreen();
}
if(key == ‘C’)
{
runmode = false;
lcd.clear();
lcd.setCursor(2,0);
lcd.print(“Ejection Sequence”);
lcd.setCursor(2,1);
lcd.print("# To Eject");
lcd.setCursor(2,3);
lcd.print("* To Cancel");

eject = manualeject();

}
if(key == ‘D’)
{
runmode = !runmode;
mainScreen();

}
if(key == ‘2’)
{
done++;
mainScreen();
}
if(key == ‘8’)
{
done–;
mainScreen();
}
if(key == ‘4’)
{
total–;
mainScreen();
}
if(key == ‘6’)
{
total++;
mainScreen();
}

}

void mainScreen()
{
lcd.clear();
lcd.setCursor(2,0);
lcd.print(“Stacker/Counter”);
lcd.setCursor(0,2);
lcd.print(“TOTAL:”);
lcd.setCursor(0,3);
lcd.print(“DONE:”);
lcd.setCursor(7,2);
lcd.print(total);
lcd.setCursor(7,3);
lcd.print(done);

if(eject == true)
{
lcd.setCursor(15,2);
lcd.print(“eject”);
}
else
{
lcd.setCursor(15,2);
lcd.print(" “);
}
if(runmode == true)
{
lcd.setCursor(15,3);
lcd.print(“Auto”);
}
else
{
lcd.setCursor(15,3);
lcd.print(” ");
}
}

long GetNumber()
{
lcd.setCursor(15,3);
long num = 0;
char key = keypad.getKey();
while(key != ‘#’)
{
switch (key)
{
case NO_KEY:
break;

case ‘0’: case ‘1’: case ‘2’: case ‘3’: case ‘4’:
case ‘5’: case ‘6’: case ‘7’: case ‘8’: case ‘9’:
lcd.print(key);
num = num * 10 + (key - ‘0’);
break;

case ‘*’:
num = 0;
lcd.setCursor(15,3);
lcd.print(" ");
lcd.setCursor(15,3);
break;

break;
}

key = keypad.getKey();
}

return num;
}

void count()
{
static unsigned long last_millis = 0;
unsigned long m = millis();

if (m - last_millis < 100)
{

}

else
{
done++;
}

last_millis = m;
}

bool manualeject()
{
bool ejectOK = false;

char key = keypad.getKey();

while(key != ‘#’ && key != ‘*’)
{
switch(key)
{
case NO_KEY:
break;

case’1’: case’2’: case’3’: case’A’:
case’4’: case’5’: case’6’: case’B’:
case’7’: case’8’: case’9’: case’C’:
case’0’: case’D’:
break;

case’#’:
ejectOK = true;
break;

case’*’:
eject = false;
break;

}
key = keypad.getKey();
}
mainScreen();
return ejectOK;
}