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.
7 years, 7 months ago.
Push Buttons Problem Help !!!
Hello all, I'm new here in mbed and I need help it is for my project at school.
So my problem is that I have two push buttons, my purpose is when I press once (without to keep it press) the first button should turn on the one of the void functions, with the second button turn on the next void functions and when I press both buttons together to turn on another void functions. And no matter which button is pressed and which function is switched on, it must remain, until another button is pressed. The big problem is that my functions are big and I can't interupt them with pressing differend button.
Now I can swich between the void functions only when the procces is gone through the whole function. And if I press the button exactly when the functions is finish before to start again.
Please Help Me
My program now:
int main() { pc.baud(9600);// set the baud rate enum {performNothing, performButton1, performButton2, performButton3}; bool wasEitherButtonPressed = false; int performFunc = performNothing; while (true) { // Capture current button state bool isB1Pressed = (button1 == 1) ? true : false; bool isB2Pressed = (button2 == 1) ? true : false; bool isEitherButtonPressed = isB1Pressed || isB2Pressed; // If either one or both buttons are pressed after neither // being pressed, pick a new function to perform. if (!wasEitherButtonPressed && isEitherButtonPressed) { if(isB1Pressed && isB2Pressed) { performFunc = performButton3; } else if (isB2Pressed) { performFunc = performButton2; } else { performFunc = performButton1; } } //Perform the same function (until a new button press) if (performFunc == performButton1) { pressure_mode(); } else if (performFunc == performButton2) { massage_mode(); } else if (performFunc == performButton3) { static_mode(); } // Remember the button state for the next time around the loop wasEitherButtonPressed = isEitherButtonPressed; } }
1 Answer
7 years, 7 months ago.
You can use interrupts to detect the button presses while in other code.
The easiest thing is to just remember that you saw the button press and then clear that once you've worked out the correct action to take.
Stopping the current task and switching immediately is harder.
Also note that this will treat button 1 and then button 2 as being the same as both at once if they are both pressed while busy in a task. If you want to only run the both button task if both are pressed at the same time you need to check in the interrupt functions to see if both are down and set a third flag indicating that. You also need to define what to do if one is pressed and then the other.
InterruptIn button1(pin?); InterruptIn button2(pin?); volatile bool button1WasPressed = false; volatile bool button2WasPressed = false; void button1Pressed () { button1WasPressed = true; } void button2Pressed () { button2WasPressed = true; } int main() { pc.baud(9600);// set the baud rate button1.rise(&button1Pressed); // whenever button 1 is pressed call the function button2.rise(&button2Pressed); // whenever button 2 is pressed call the function enum {performNothing, performButton1, performButton2, performButton3}; bool wasEitherButtonPressed = false; int performFunc = performNothing; while (true) { bool isEitherButtonPressed = button1WasPressed || button2WasPressed; // If either one or both buttons are pressed after neither // being pressed, pick a new function to perform. if (!wasEitherButtonPressed && isEitherButtonPressed) { if(button1WasPressed && button2WasPressed) { performFunc = performButton3; } else if (button2WasPressed) { performFunc = performButton2; } else { performFunc = performButton1; } } // clear the flags button2WasPressed = false; button1WasPressed = false; //Perform the same function (until a new button press) if (performFunc == performButton1) { pressure_mode(); } else if (performFunc == performButton2) { massage_mode(); } else if (performFunc == performButton3) { static_mode(); } // Remember the button state for the next time around the loop wasEitherButtonPressed = isEitherButtonPressed; } }