[SOLVED]Unexpeced exit of while loop. analogRead returning 0 the culprit?

Hardware: Arduino Uno Rev 3
LCD Display: 2 Line 16 character with 5 buttons
similar to but not not exactly like this one: http://www.dfrobot.com/wiki/index.php?title=Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009)
The program is meant to offer a ‘step through’ menu with some options dependent on button pushed.

I’m a newb but have resisted starting threads a couple of dozen times now trying to work this stuff through myself. I think I’ve hit the wall with my limited coding and Arduino experience on this one. I don’t know why my code is exiting the while loop in my setzero() function at the bottom of the program when I press the ‘right’ button on the display. It is undefined. It will be defined later but I prefer to know why this is happening in case I’m just masking a problem and it will come back to bite me later. If I press other undefined buttons, noting happens.

Following are some Serial print captures.

Left(undefined) button pressed: while continues
end of while 1021
end of while 505
end of while 1023

Down button(undefined) pressed: while continues
end of while 1023
end of while 1023
end of while 327
end of while 328
end of while 1022

Right button(undefined) pressed: exits while
end of while 1023
end of while 0
We are in the start function

That should not match the exit condition for the loop:
while (ButtonVoltage != SELECT || ButtonVoltage != UP)

My only guess at this point is the analog read is returning a ZERO and that the while loop is exiting on that. Since it is analog I don’t seem to be able to increment it. I’ve read up on pointers and think this might be a way around that if it is the problem but I still have the analogRead returning ZERO and reading about pointers is different than applying them.

My first, and previous only other thread, turned out to be a problem with my Linux distro package so I downloaded the ‘official’ binaries and have been using those ever since.

// Analog buttons are converted to 1024 bit values along a
// voltage divider circuit.  Low value wins so you can't press
// two keys at once. 
// Observed values:
//     NONE:    1023
//     SELECT:  741
//     LEFT:    505
//     DOWN:    328
//     UP:      133
//     RIGHT:   0

#include <LiquidCrystal.h>
/*  I was getting some unexpected behavior so I decided to 'peg'
 the values rather than just doing some 'less thans'. Not sure it
 will help but it shouldn't hurt,  much.... :/                  */
#define NONE (ButtonVoltage > 1000)//1023
#define SELECT (ButtonVoltage <= 999 && ButtonVoltage >= 650)//741
#define LEFT (ButtonVoltage <= 649 && ButtonVoltage > 450)//505
#define DOWN (ButtonVoltage <=449 && ButtonVoltage > 250)//328
#define UP (ButtonVoltage <=249 && ButtonVoltage > 50)//133
#define RIGHT (ButtonVoltage <= 50)//0
#define BUTTONREAD (ButtonVoltage = analogRead(0))

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int ButtonVoltage = analogRead(0);

int Backlight = 10;
int fadeValue = 255;

void setup() {
  Serial.begin(9600);

  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
}

void loop() {
  start ();
}


void start(){
  delay(100);    //latch function in before it sees button values
  lcd.setCursor (0,0);
  lcd.print("< Set Backlight ");
  lcd.setCursor (0,1);
  lcd.print("V to conitnue   ");

  BUTTONREAD;
  Serial.println("We are in the start function");
  if(NONE) { 
    start();
  }
  else if (LEFT){
    backlight();
  }
  else if (DOWN){
    setzero();
  }
}

/*  Function to set the LCD backlight */
void backlight(){
  delay(100);  //debounce to latch in function
  lcd.setCursor (0,0);
  lcd.print("^ for + V for - ");
  lcd.setCursor (0,1);
  lcd.print("> to return     ");
  //.............define loop exit conditions for while.....)
  while (ButtonVoltage != RIGHT || ButtonVoltage != SELECT){
    BUTTONREAD;
    //menu selections start here
    if (SELECT){      //select always returns home from anywhere
      start();
    }
    if (RIGHT){      //goes 'back' to start in this case
      start();
    }
    else if (UP){    //increase backlight
      fadeValue = fadeValue +1;
      if (fadeValue > 254) { 
        fadeValue = 255; 
      }
      analogWrite (Backlight, fadeValue);
      delay (25);
    } 
    else if (DOWN){  //decrease backlight
      fadeValue = fadeValue -1;
      if (fadeValue < 1) { 
        fadeValue = 0; 
      }
      analogWrite (Backlight, fadeValue);
      delay (25);
    }
  }
}

/*  Function to set the sled workpiece to zero in
 reference to the cutter position           */
void setzero(){
  lcd.setCursor (0,0);
  lcd.print("^ back to start ");
  lcd.setCursor (0,1);
  lcd.print("< left   right >");
  delay(50);                         //latch value before analogRead
  Serial.println("should not see ButtonVoltage");
  //.......set conditions that will exit while loop....
  while (ButtonVoltage != SELECT || ButtonVoltage != UP){
    //menu selections start here
    BUTTONREAD;
    if (SELECT){  //always back to start
      start();
    }
    else if (UP){  //one level up to start
      start();
    }
    else if (DOWN){
      ButtonVoltage = 10;
    }
    else {
      Serial.print("end of while   ");
      Serial.println(ButtonVoltage);
      delay(500);
    }

  }
}
  while (ButtonVoltage != RIGHT || ButtonVoltage != SELECT){

This expands to

while (ButtonVoltage != (ButtonVoltage <= 50)|| ButtonVoltage != (ButtonVoltage <= 999 && ButtonVoltage >= 650)){

Comparing a sensor value to the boolean result of the testing of the sensor value doesn’t really make sense.

Arrch:

  while (ButtonVoltage != RIGHT || ButtonVoltage != SELECT){

This expands to

while (ButtonVoltage != (ButtonVoltage <= 50)|| ButtonVoltage != (ButtonVoltage <= 999 && ButtonVoltage >= 650)){

Comparing a sensor value to the boolean result of the testing of the sensor value doesn’t really make sense.

Well that not only makes sense but “while (!SELECT || !UP){” works fine. Thanks. I’ve been reading up on coding but it seems it will take a little longer to sink in.