I have a 2560 controller with a membrane switch (keypad) on it. When I press buttons A, B or C I want it to change the mode of the "traffic light system". My issue is unless I hold it down, the read value goes back to zero and my mode of operation won't continue to execute. So how I can get the program to read the button, and then store that value unless a different button is pressed?
I was using serial print to see if I was saving the value and it doesn't! Any help would be appreciated.
//traffic light PIN assignments
int RedEastWest = 23;
int YellowEastWest = 24;
int GreenEastWest = 25;
int RedturnEW = 26;
int YellowturnEW = 27;
int GreenturnEW = 28;
int RedNorthSouth = 29;
int YellowNorthSouth = 30;
int GreenNorthSouth = 31;
int RedturnNS = 32;
int YellowturnNS = 33;
int GreenturnNS = 34;
// photocell PIN assignments
int ldrPin = 0; // A0 Pin connected to LDR
// pedestrian button assignments
int Buzzer = 12;
int Crossbtn = 22;
// button pad PIN assignments
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {43, 42, 41, 40}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {39, 38, 37, 36}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// LCD output PIN assignments
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
// Setup steps for each component as listed
void setup()
{
// define modes for each component
// Traffic lights
pinMode(RedEastWest, OUTPUT);
pinMode(YellowEastWest, OUTPUT);
pinMode(GreenEastWest, OUTPUT);
pinMode(RedNorthSouth, OUTPUT);
pinMode(YellowNorthSouth, OUTPUT);
pinMode(GreenNorthSouth, OUTPUT);
pinMode(RedturnEW, OUTPUT);
pinMode(YellowturnEW, OUTPUT);
pinMode(GreenturnEW, OUTPUT);
pinMode(RedturnNS, OUTPUT);
pinMode(YellowturnNS, OUTPUT);
pinMode(GreenturnNS, OUTPUT);
// photocell no setup step
// Pedestrian button
pinMode(Crossbtn, INPUT);
pinMode(Buzzer,OUTPUT);//initialize the buzzer pin as an output
// button pad has no setup step
Numberpad();
// LCD setup
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Traffic Control");
delay(2000);
// scroll 13 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(150);
}
Serial.begin(9600);
}
void loop()
{
// Check for keypad entry to select mode
char choice = Numberpad();
Serial.println(choice);
if (choice == 'A') {
ModeStreet();}
else if (choice == 'B') {
ModeAvenue();}
else if (choice == 'C') {
ModeNight(); }
}
char Numberpad(){
char lastKey;
char customKey = customKeypad.getKey();
if (customKey == 'A'){
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print(" Street Mode");
lastKey = customKey;
}
else if (customKey == 'B'){
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print(" Avenue Mode");
lastKey = customKey;
}
else if (customKey == 'C'){
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print(" Night Mode");
lastKey = customKey;
}
else if (customKey == 0) {
lastKey = lastKey;
}
return lastKey;
}
void ModeStreet()
{
Numberpad();
digitalWrite(RedEastWest, HIGH);
delay(2000);
/* Code for the alarm/buzzer for Pedestrian crosswalk
unsigned char i;
while(1)
{
//output an frequency
for(i=0;i<10;i++)
{
digitalWrite(Buzzer,HIGH);
delay(1000);//wait for 1ms
digitalWrite(Buzzer,LOW);
delay(1000);//wait for 1ms
Serial.print(i);
}
//output another frequency
for(i=0;i<5;i++)
{
digitalWrite(Buzzer,HIGH);
delay(400);//wait for 2ms
digitalWrite(Buzzer,LOW);
delay(1000);//wait for 2ms
Serial.print(i );
} */
}
void ModeAvenue()
{
Numberpad();
digitalWrite(RedEastWest, HIGH);
delay(2000);
}
void ModeNight()
{
Numberpad();
digitalWrite(RedEastWest, HIGH);
delay(2000);```