help noob with variables

trying to build home security system with 3 or more states(ARMED AWAY, DISARMED, ARMED STAY). code compiles without errors but the var "state" doesn't change when button on keypad is pressed.

any help is much appreciated

#include <LCD4Bit.h>

LCD4Bit lcd = LCD4Bit(2);

void setup() {

  lcd.init();
  lcd.commandWrite(0x0F);
}  

int state = 1;
int keyvalue = 0;
int password = 0;



void loop() {

  Serial.begin(9600); //debuging purposes only
  //=================================================================================================================================
  //=====================================================================DISARMED====================================================
  //=================================================================================================================================
  while(state == 0)
  {
    lcd.clear();
    lcd.cursorTo(1,4);
    lcd.printIn("DISARMED");
    delay(1000);
    return;
  }


  //=================================================================================================================================
  //=====================================================================ARMED AWAY==================================================
  //=================================================================================================================================
  while(state == 1)
  {

    lcd.cursorTo(1,3);
    lcd.printIn("ARMED AWAY");

    //=====================check key===================================================================================================

    int keyvalue = analogRead(0);

    switch(keyvalue)
    {
    case 31:
      (password = password + 1);
    }

    //==================change state===================================================================================================

    if (password == 1)
    {
      lcd.clear();
      int state = 0;
    }

    return;
  }


  //===================================================================================================================================
  //=====================================================================ARMED STAY====================================================
  //===================================================================================================================================
  while(state == 2)
  {

    lcd.clear();
    lcd.cursorTo(1,3);
    lcd.printIn("ARMED STAY");
    delay(1000);
    return;
  }




}//end void loop

I'd recommend you write out, in plain English sentences, what needs to be done. Probably on the computer where you'll be able to edit, insert and delete the steps involved to complete the task.

Do this and post it so that we have have something in the way of an outline that both you and anyone that wishes to help can reference during your development,

My second recommendation is to not use numbers directly in your source and instead use constants and or enum. Numbers don't suggest what their being used for. Example:

enum { DISARMED = 0, ARMED_AND_PRESENT, ARMED_AND_AWAY };

can be used to define label that more meaningfully suggest function than the numbers 0, 1 and 2 for states.

You also need to take the Serial.begin(9600) out of the loop() function and put it in setup().

--
Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

I have a few suggestions. First, you have a double declaration of keyvalue. You declared it as an int between setup() and loop(), but you declared it again in loop(). You'd be better off using "if" instead of "while" for what you are doing. You don't need all the return statements. I hope this helps. Here is the code:

#include <LCD4Bit.h>

LCD4Bit lcd = LCD4Bit(2);

void setup() {

  lcd.init();
  lcd.commandWrite(0x0F);
  Serial.begin(9600); //debuging purposes only
}  

int state = 1;
int keyvalue = 0;
int password = 0;



void loop() {

  
  //=================================================================================================================================
  //=====================================================================DISARMED====================================================
  //=================================================================================================================================
  if(state == 0)
  {
    lcd.clear();
    lcd.cursorTo(1,4);
    lcd.printIn("DISARMED");
    delay(1000);
  }


  //=================================================================================================================================
  //=====================================================================ARMED AWAY==================================================
  //=================================================================================================================================
  while(state == 1)
  {

    lcd.cursorTo(1,3);
    lcd.printIn("ARMED AWAY");

    //=====================check key===================================================================================================

    int keyvalue = analogRead(0);

    switch(keyvalue)
    {
    case 31:
      password = password + 1;
      break;
    }

    //==================change state===================================================================================================

    if (password == 1)
    {
      lcd.clear();
      int state = 0;
    }

  }


  //===================================================================================================================================
  //=====================================================================ARMED STAY====================================================
  //===================================================================================================================================
  if(state == 2)
  {

    lcd.clear();
    lcd.cursorTo(1,3);
    lcd.printIn("ARMED STAY");
    delay(1000);
  }




}//end void loop

You may want to spend some more time looking at the reference page on the Arduino website. You called yourself a noob, and your code needed some work that more time looking through the refrence would help. I always have the Duemilanove schematic and the reference page open in my web browser.
[edit]Moved Serial.begin to setup in the code[/edit]

I'll throw my suggestion into Sean's code with:

#include <LCD4Bit.h>

enum { DISARMED = 0, ARMED_AND_PRESENT, ARMED_AND_AWAY };

LCD4Bit lcd = LCD4Bit(2);

void setup() {

  lcd.init();
  lcd.commandWrite(0x0F);
  Serial.begin(9600); //debuging purposes only
}  

int state = ARMED_AND_PRESENT;
int keyvalue = 0;
int password = 0;



void loop() {

  if(state == DISARMED)
  {
    lcd.clear();
    lcd.cursorTo(1,4);
    lcd.printIn("DISARMED");
    delay(1000);
  }


  while(state == ARMED_AND_PRESENT)
  {

    lcd.cursorTo(1,3);            lcd.printIn("ARMED AWAY");

    //=====================check key===================================================================================================

    int keyvalue = analogRead(0);

    switch(keyvalue)
    {
    case 31:
      password = password + 1;
      break;
    }

    //==================change state===================================================================================================

    if (password == 1)
    {
      lcd.clear();
      int state = DISARMED;
    }

  }

  if(state == ARMED_AND_AWAY)
  {

    lcd.clear();
    lcd.cursorTo(1,3);            lcd.printIn("ARMED STAY");
    delay(1000);
  }
} //end void loop
int keyvalue = analogRead(0);

    switch(keyvalue)
    {
    case 31:

analogRead returns a value in the 0-1023 range depending on the input voltage. It seems difficult to hit exactly 31. And even you do, it won't help:

if (password == 1)
    {
      lcd.clear();
      [glow]int[/glow] state = DISARMED;
    }

"int" here means that you create a new independent variable state, which will die at the following "}", so the state you are checking elsewhere is unaffected.

Thank you all for your help, I will work on these changes today and post my findings. I will also spend more time on the reference page and learn as much as I can, sometimes a fresh pair of eyes can help me out of a frustrating slump. ;D