How to prompt for a button press

Hi All! My first post, be gentle with me! 8)

I'm designing a sports timing system for a friend, and it needs to have two distinctive modes of operation. I have a Uno running a 16x2 character display and with three pushbuttons. The system needs to start up with a banner advert (easy) and then prompt for the user to press one button or the other to choose a mode. The modes are very similar, and it's preferable for the program to get trapped permanently in one mode or the other. I wondered what the best way of doing this is? I wondered about defining mode A as a function() and mode be as anotherfunction() but I'm not sure how to get the Arduino to ask the question and wait forever for the button press?

Also, If I use function calls in that way, I'm worried that once the function finishes, it'll return to the question, when instead it needs to cycle that function endlessly. I suppose I want to be able to choose between two variants of void loop(). I'd welcome advice on how best to go about this!

Many thanks! Dave H

Have a look at the use of the switch/case.

Some pseudo code to illustrate the principle

set state to 0
Show prompt on the screen

start of loop
  switch using state
    case 0
      if the mode 1 button is pressed 
        set state to 1
      else if mode 2 button is pressed
        set state to 2
      end of if
     end of case 0
     
     case 1
        //code for mode 1 actions goes here
     end of case 1
    
     case 2
        //code for mode 2 actions goes here
     end of case 2
     
  end of switch  
end of loop

Hi,

but I'm not sure how to get the Arduino to ask the question and wait forever for the button press?

e.g:

int bottonState1=digitalRead(YOURBOTTON1);
int bottonState2=digitalRead(YOURBOTTON2);

if (bottonState1-oldbottonState1==-1) { //if the user has pressed and left the botton1
  function1(); //execute your function to to something
}
oldbottonState1=bottonState1;

if (bottonState2-oldbottonState2==-1) { //if the user has pressed and left the botton2
  function2(); //execute your function to to something
}
oldbottonState2=bottonState2;

For timed actions, please read: arduino.cc/en/Tutorial/BlinkWithoutDelay?

EDIT:

Don't to forget to debounce your buttons... Have a look at: http://playground.arduino.cc/code/bounce

Yours, Stefan

Thankyou so much, both! I did remember to debounce the buttons, but I did it in hardware ;)

But how do you get it to run either function1() or function2() in an endless loop? I don't want them to return :P Cheers, DH

grooeygroo: But how do you get it to run either function1() or function2() in an endless loop? I don't want them to return

I think you are wrong in wanting to do this but an endless loop is easy

while(1==1){
// do stuff forever
}

The modes are very similar, and it's preferable for the program to get trapped permanently in one mode or the other.

Why should you lock up your processor?

But how do you get it to run either function1() or function2() in an endless loop? I don't want them to return

Why?

PS: If you want to have the same screen you do not have to endless loop! You only must not change the display data!

Yours, Stefan

Thanks Mike, that's what I needed :o)

Stefan, it's ok, the endless function is a long program. Mode 1 is a normal stopwatch counter, mode 2 is a stopwatch preceded by a 45-second countdown. So, each mode has to cycle endlessly to keep time, and we want it to remain in whichever mode until power-off. It works very well at the moment, but I've not implemented mode 2 yet because I didn't know how to select one loop mode, if that makes sense! Thanks again both, DH

But how do you get it to run either function1() or function2() in an endless loop? I don't want them to return

Mike has shown you a way to do this but you don't need to.

Look at my pseudo code. If state equals 1 or 2 then the code for either case will be run each time through loop() which will achieve the effect that you want. Moreover, should you ever need to introduce another button to exit either mode then it is easy to test for it in the switch/case. If the new button press changed the case to 0, for example, you could return to prompting the user to select a mode, or the new button could switch between modes.