I am supposed to make some kind of a smart traffic light project.
I should allow the user to enter the time he desires for every led to light up.
What i am trying to reach right now is that after the user enters the time and save it in the variables the code continue to the void loop to use that time to light up the led.
Though i am still on the very first part of the project i am facing a problem to connect the two pieces of the code together " entering the time & lighting up the led " .
so before i describe the issue , here is the code :
#include <LiquidCrystal.h>
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'},
};
byte rowPins[ROWS] = {5,4,3,2};
byte colPins [COLS] = {8,7,6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd (11,12,13,14,15,16);
int HumanRedLed = 9;
int HumanGreenLed=10;
int CarRedLed = 17;
int CarYellowLed = 18;
int CarGreenLed = 19;
char RedTime;
char YellowTime;
char GreenTime;
char RedKey;
char YellowKey;
char GreenKey;
void setup(){
pinMode (HumanRedLed,OUTPUT);
pinMode (HumanGreenLed,OUTPUT);
pinMode (CarRedLed,OUTPUT);
pinMode (CarYellowLed,OUTPUT);
pinMode (CarGreenLed,OUTPUT);
digitalWrite(HumanRedLed, LOW);
digitalWrite(HumanGreenLed, LOW);
digitalWrite(CarRedLed, LOW);
digitalWrite(CarYellowLed, LOW);
digitalWrite(CarGreenLed, LOW);
RedTime=0;
RedKey=0;
YellowTime=0;
YellowKey=0;
GreenTime=0;
GreenKey=0;
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter Red Time");
lcd.setCursor(0,1);
lcd.print(RedTime);
while (RedKey==0) {
char RedKey= keypad.getKey();
if ( RedKey != NO_KEY){
if (RedKey != '#'){
RedTime = RedKey;
lcd.setCursor(0,1);
lcd.print(RedTime);
char RedKey = keypad.getKey();}
else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter Yellow Time");
lcd.setCursor(0,1);
while (YellowKey ==0){
char YellowKey = keypad.getKey();
if ( YellowKey != NO_KEY){
if ( YellowKey != '#'){
YellowTime = YellowKey;
lcd.setCursor(0,1);
lcd.print(YellowTime);
char YellowKey = keypad.getKey();}
else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter Green Time");
lcd.setCursor(0,1);
while ( GreenKey==0){
char GreenKey = keypad.getKey();
if ( GreenKey != NO_KEY){
if ( GreenKey != '#'){
GreenTime = GreenKey;
lcd.setCursor(0,1);
lcd.print (GreenTime);
char GreenKey = keypad.getKey();}
else {break;}
}}}}}}}}}
void loop (){
digitalWrite(CarRedLed, HIGH);
digitalWrite(HumanGreenLed, HIGH);}
The problem is that for a reason that i can't understand it never breaks the loop.
It freezes in the "green" section of the code , no matter how many times i press the # button.
What happens exactly is that after i write the "green" time and press # to save it and exit the loop ,nothing happens at all.
And when i press it twice , the second line in the lcd goes blank as if i didn't write the first number.
The code in the " void loop " section isn't the real code , it is just a simple one to see if the first part is working or not.
If it managed to break the loop then it will continue to the Void loop which lights up the led , and that never happens.
I have attached below the proteus simulation file for the code to help you to see what i mean.