A quick question in my while loop ?

Hi,

I had a question about something in my code,

#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 :slight_smile:

while (state = 1)
You mean
while (state == 1)

Yes, no wonder it was stuck in while

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 :slight_smile: ?
thanks for answering

state = 1 sets the variable state to 1, state == 1 tests the value.

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

@JimboZA

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?
}

@LarryD

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 :).

while(state=1)No. You need to test the value of state not set it to 1

while (state == 1)
{
   //do stuff until state = 2
}

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.

Glad you are starting down the Arduino road.

I urge you to look at this discussion that Nick put together; it will help with the programming.
traps, tips and style guide

else if (key != time)

millis() Returns the number of milliseconds since the Arduino board began running the current program.

You could
change
int time=9000;
TO
unsigned long time = 9000;

else if (millis() > time) // may have to increase, time, to a larger value since you have so many delays()

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 ?

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.

I understand now,

Thank you and everyone else :_