#include <LiquidCrystal.h>
#include <Keypad.h>
------//Declare keypad rows/cols
int time=9000;
int m = 0; //reset
int state = 1; // Keypad State
void setup(){
lcd.setCursor(0,0);
lcd.print("If you are ready");
lcd.setCursor(4,1);
lcd.print("for voting");
delay(3000);
lcd.clear();
lcd.setCursor(2,0);
lcd.print("Press on #");
lcd.setCursor(2,1);
lcd.print("To Proceed");
delay(2000);
while (state = 1)
{
char key = kpd.getKey(); //GETS VALUE FROM KEYPAD KEYPRESS
if(key) //WRITES KEYPRESS
{
lcd.setCursor(0,1);
lcd.print(key);
}
if (key=='#') //IF # IS PRESSED
{
lcd.clear();
lcd.print(" Please,"); //
delay(3000);
lcd.setCursor(1,0);
lcd.print(" ");
lcd.setCursor(2,0);
lcd.print("Select Either"); //
lcd.setCursor(2,1);
lcd.print("Kim or Levine");
delay(5000);
lcd.clear();
//state=2; // when I try this without break, to skip the above when run again, after I press #, it stops at lcd.clear but doesn't continue with void loop ?
break; // i must use break to continue to void loop, Is this the best way to getout of "while" ?
}
// delay(10000); // I get an error if I use a delay here ?
else if (key != time) // if key is not pressed within 10secs. dp the below stuff, but its not working
{
lcd.clear();
//delay(time);
lcd.setCursor(0,0);
lcd.print("you must Press it");
lcd.setCursor(0,1);
lcd.print("to continue");
delay(5000);
m=0; // resets void setup from beginning, but its not working
}
}
}
void loop(){
// My stuff ;
}
Just looking for some answers to my lame questions and thanks
while (state ==1){ // may i ask, why was it not working when it was "state=1" ?
lcd.print(" Please,"); //
delay(3000);
lcd.setCursor(1,0);
lcd.print(" ");
lcd.setCursor(2,0);
lcd.print("Select Either"); //
lcd.setCursor(2,1);
lcd.print("Kim or Levine");
delay(5000);
lcd.clear();
state=2; // it can proceed to void loop if i press # now , without using break .
// break ; // Is it a problem if i used break, which is more convenient, ?
just, Is my else if statement not written correctly, i know its poor style written ?
thanks for answering
So it would make more sense , if i write it this way ?
while(state=1){
// stuff
state==2; // skip stuff next time it runs
}
?
while (state=2){
// different stuff // do this and skip the above stuff, when ran a 2nd time
state==1 ; // did i just tell it to run stuff, when ran a 3rd time ? is this correct?
}
I see, it will always be true, my goal was to say, if # is not pressed within 9000 , restart from beginning.
Should I create a variable that shows, when the button is pressed and another when its not pressed ?
thanks guys :).
LarryD:
char key = kpd.getKey();
key is type character
int time=9000;
time is type integer
else if (key != time)
this is always true since they aren't the same type
C allows you to compare different integer types.
Of course, the "char" variable can never hold a value as big as 9000, but that doesn't stop you comparing.
Thanks everyone for clearing things out, I get it now, reading the state first then I can change the value of the state , if needed, just like true and false :), and thank you larryd for the helpful link (a pretty helpful site) and your suggestion as well, but just , wouldn't unsigned int (0 -> 65535), be enough ?
Both millis() and micros() return unsigned longs.
You should get in the habit of using unsigned longs when using with these.
If you don't you will run into problems eventually.