jorismo
November 15, 2022, 9:05pm
1
Hi all,
long time since I'm programming with Arduino.
Currently I'm working on an project where 2 buttons are included.
Based on which button is pressed first another subprogram must be chosen.
For example:
If button 1 is pressed before button 2, then the text 'Time' should be printed via the serial monitor. (after button 2 is pressed)
If button 2 is pressed befor button 1 then the text 'Speedy' should be printed via the serial monitor. (after button 1 is pressed)
I suppose that I should use the 'Case' function, but I'm not sure about it.
Thanks for your help!!!!
I would probably use the if, else if, else structure for that.
alto777
November 15, 2022, 9:46pm
3
jorismo:
For example:
What if we press both buttons at once?
What if we press button A a few times before pressing button B?
Can you draw a flowchart of how this might work?
Can you a variable in a way that could "know" or "remember" that a certain button had been pressed?
So remember button presses and see if you have a new button and already the other was pressed.
a7
const byte Button1Pin = 4;
const byte Button2Pin = 5;
bool Button1WasPressed = false;
bool Button2WasPressed = false;
void setup()
{
pinMode(Button1Pin, INPUT_PULLUP);
pinMode(Button2Pin, INPUT_PULLUP);
}
void loop()
{
bool button1IsPressed = digitalRead(Button1Pin) == LOW;
bool button2IsPressed = digitalRead(Button2Pin) == LOW;
if (button1IsPressed != Button1WasPressed)
{
// State changed
Button1WasPressed = button1IsPressed;
if (Button1WasPressed)
{
// Just pressed
if (Button2WasPressed)
{
// Both pressed, button2 pressed first
Serial.println("Speedy");
}
}
}
if (button2IsPressed != Button2WasPressed)
{
// State changed
Button2WasPressed = button2IsPressed;
if (Button2WasPressed)
{
// Just pressed
if (Button1WasPressed)
{
// Both pressed, button1 pressed first
Serial.println("Time");
}
}
}
}
What if you just press one? Will wait for the other to be pressed?
Pressed first and released? Or pressed first and held down?
jorismo
November 16, 2022, 9:04pm
7
Hi all,
wow thanks for all your support!
Well If button A is pressed it won't be pressed again before button B is pressed and vice versa.
If you only pressed A then it must wait till button B is pressed before anything else can happen.
Thanks!
Joris
I asked not about presses. I asked about releases. You didn't really address that.
jorismo
November 18, 2022, 12:05pm
9
Hi Aarg,
sorry, my bad!
The button A is pressed and released then button B is pressed, and when B is pressed then the program should start printing the text.
The same in the opposite direction, B is pressed and released afterwards A is pressed and the other program should print the text.
Thanks!
Your keypress definition is "conventional" regards press/release operation. So key sense/debounce code will be easy to write or copy. Or, you can use a button library if the professor will allow it.
You have 3 states:
waiting for any key
waiting for the A key
waiting for the B key
This is an extremely simple state machine problem.
jorismo
November 18, 2022, 2:32pm
11
Thanks for your reply.
I'm currently 35 years old (born in 1987), not in school anymore.
But I'm not that good at Arduino for the moment
Thanks
Joris
Then what do these buttons do? I would have been happier to see a reply to my technical advice, though...
FYI, I was your age when I started work on my degree. So no harm, no foul, I hope...