Do one thing for short button press, another thing for long press

My sketch is for doing a bunch of stuff for a normal press of a button, but if it's a long press a calibration routine is run instead. It is structured like this:

code that determines length of button press.
If pressed longer than 2 seconds, go to longpress() subroutine.
If pressed shorter than 2 seconds continue executing code in main loop.

I know it's a dumb question, but I can't figure out how to do it so that after completing the calibration routine it goes back to the start of loop() rather than back to where it jumped from and executing the default code for a short press. I tried putting a loop() instruction at the end of the calibration routine, but that doesn't work.


I hope you mean "function" where you wrote "subroutine" :wink:

And to fix the problem, rather then letting the "shorter than 2 seconds" continue, make it explicitly execute a function. And simply don't call that function if it's a longer press.

Sounds a bit vague but it's as accurate as your post without code. If you want detailed help, post your code. OR BETTER, post a MCVE.

This link has code for different button click types.


Use a button library. It makes it simple.
I'd recommend using "JC_Button". It is available in the IDE library manager.
github page is here: GitHub - JChristensen/JC_Button: Arduino library to debounce button switches, detect presses, releases, and long presses

Is this what septillion suggested?

void loop() {
  while (digitalRead(SW1) == HIGH) {}         // do nothing until SW1 closes
  startTime = millis();                                   // save switch closed time
  while (digitalRead(SW1) == LOW) {}          // do nothing until SW1 opens
  endTime = millis();                                    // save switch opened time
  switchTime = (endTime - startTime);
  if (switchTime < 2000)  normal();               // go to normal function
  if (switchTime >= 2000)  calibrate();          // go to calibrate function

I keep getting a compile error that says" ‘normal’ was not declared in this scope ". Don’t know what that means. There is a function “void normal()” further on that works just fine when you run it independently.

There is a function "void normal()" further on that works just fine when you run it independently.

I surely don't see it :wink: Please post all your code, or better, a MCVE. We're not Snippets-R-Us.

And it kind of is what I mean. A else-clause would have made more sense. Only, DON'T use a useless while! The loop() is already looping, why break that? Just only act when you need to but let the loop do it's looping thing.