Return Sintax

Just a quick question. I am developing a project of multiple timers using an RF Robot LCD shield. My question is this;

int ReadButtons()
{
  ButtonState = analogRead(A0);                //Read the value from the sensor 
  delay(5);                                 //Debounce delay
  int k = (analogRead(A0) - ButtonState);      //Gives the button a slight range to allow for a little contact resistance noise
  if (5 < abs(k)) return NoButton;          //Double checks the keypress. If the two readings are not equal +/-k value after debounce delay, it tries again.
  if (ButtonState > 1000) return NoButton;     //No button
  if (ButtonState < 50) return BtnRight;       //Right button
  if (ButtonState < 555) return BtnLeft;       //Left button
  if (ButtonState < 380) return BtnSave;       //Save button
  if (ButtonState < 195) return BtnBack;       //Back button
  return NoButton;                          //Default if no buttons are pressed

The above is a section of my code for the buttons. Note that I use a return then the name of the button. Now in the loop I want call on each button could I write something like this;

if (ButtonState == BtnSave)
{
do something here
}
else if
(ButtonState == BtnBack)
{
do something here
}

I would greatly help please.

  if (ButtonState < 555) return BtnLeft;       //Left button
  if (ButtonState < 380) return BtnSave;       //Save button
  if (ButtonState < 195) return BtnBack;       //Back button

If ButtonState is say 379 or 194 what will be returned ?

if (ReadButtons() == BtnSave)
{
do something here
}
else if
(ReadButtons() == BtnBack)
{
do something here
}

Or better yet:

int ButtonState = ReadButtons();  // This is a NEW ButtonState Variable, not the same one in the function, just same name
if (ButtonState == BtnSave)
{
do something here
}
else if
(ButtonState == BtnBack)
{
do something here
}

If ButtonState is say 379 or 194 what will be returned ?

Nothing. Those resistances are for the remainder buttons on the shield which I wont be using.

Thanks for the help with the code. Much appreciated.

You logic is broken:

  if (ButtonState > 1000) return NoButton;     //  Works OK
  if (ButtonState < 50) return BtnRight;         //  Works OK
  if (ButtonState < 555) return BtnLeft;         //  Works OK
  if (ButtonState < 380) return BtnSave;       //  Will never be executed, because 380 is also < 555
  if (ButtonState < 195) return BtnBack;       //  Will never be executed, because 195 is also < 380

You must always work either UP through the values IN ORDER, testing for </<=, or DOWN through the values, testing for >/>=.

Regards,
Ray L.

Gotcha. Thanks.