Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
5 years ago.
How to use only one button to display different things on nokia display?
My question is simple, but I dont know how to realize it? So I can use only one button(on LPC1114).
If button is pressed , I need to do one thing, and if same button is pressed again I need to do another thing. And over and over again like that. So to understand me better I will explain you like this:
Press button -> do thing 1 Press button -> do thing 2 Press button -> do thing 1(again) Press button -> do thing 2 Press button -> do thing 1 Press button -> do thing 2
and so on. So I dont realize how to do this if I am using only one button. Thanks!
2 Answers
5 years ago.
Hi, you can add a simple counter into your "button pressed" check statement or something else.
Great example is from Graham S. https://os.mbed.com/questions/74042/Multi-Function-PUSH-button/
Best regards J.
5 years ago.
What you are describing is a simple state machine. You use a variable to track the current state of the system and depending on the value of that variable act appropriately.
// define an enum to track what to do next. // since there are only 2 states we could use a bool variable // but that then doesn't allow adding more modes // or we could use an integer with different values for each // state but then we need a set of defines or constants // to track which value is for which state. // an enum is effectively an int and set of defines combined. enum state_e { action1 , action2 } nextButtonAction; void doNextButtonAction (void) { switch (nextButtonAction) { case action1: default: // default should never be hit but include it to be safe. doAction1(); nextButtonAction = action2; break; case action2: doAction2(); nextButtonAction = action1; break; } } main () { // initalise nextButtonAction = action1; while (true) { if (button) { // button is pressed doNextButtonAction(); while (button) // wait for button to be released wait_us(10); } } }