Push button to interupt code1 with code2

I was wondering if it is possible for this to happen:

Code 1 is going on a loop. I press a push button (or I hold it down) and code 2 goes. I let go of the button and code 1 starts to loop again.

I have an Mega 2560.

Yes that is quite possible.

What programming have you done?


It is not that trivial if you want instant switching in the middle of a tasks and coming back exactly where you were when you release the button. If you can cope with ending the current task before switching then it's super easy.

  If button is pressed then
    Execute code 2
     Execute code 1
  End if

I am not actually doing the programming. I'm designing the project and wanted to check before asking the programmer to do it. But we are on limited time (and different time zones), so I thought I would check while I'm awake and he is not.

Yes, instantly switching in the middle of Task 1 is perfectly fine as it's an easy loop. Task 2 is more of a "when I hear someone say a password, I must push the button to make Task 2 happen, then go back to Task 1."

Also, how would we identify each code to be Task 1 vs Task 2? Are both tasks written into the same script, Task 1 on loop and Task 2 outside of the loop? I'm just gathering as much information as possible, and also so I can understand the basics of it.

You would write two functions within the same sketch, then call one when the button is pushed and the other when the button is released.

See J-M-Ls pseudo code


The key to doing this is to write functions that do a slice of the task and then return. Here's some sample code.

This sketh will blink the on-board LED on pin 13. Stick one end of a wire into pin5. When you touch the other end of that wire to ground, the blinking will be fast. When you don't, the blinking will be slow.

void setup() {
  pinMode(13, OUTPUT);
  pinMode(5, INPUT_PULLUP);

void loop() {
  if(digitalRead(5)==HIGH) {
  else {

void blink_LED_slowly() {
  digitalWrite(13, HIGH);
  digitalWrite(13, LOW);

void blink_LED_fast() {
  digitalWrite(13, HIGH);
  digitalWrite(13, LOW);

SciFiLover84: I am not actually doing the programming.

Yes, instantly switching in the middle of Task 1 is perfectly fine

I think it will be difficult to give specific advice if you are not the programmer.

A lot depends on what you mean by "instantly" - is it within a second, within 1/10th of a second, or 1/100th of a second (or what)

And it also depends on what is actually happening at the time the button is pressed and whether the program has been properly structured to faciliate the switch from code1 to code2.

In an Arduino what you refer to as code1 and code2 would be implemented as two different functions (or each of them might be represented by a collection of functions). Whatever function happens to be running when the button is pressed must run to completion before a different function is called. The trick, then, is to write the code so that each run through any function only takes a few microseconds or maybe a few milliseconds. Switching will then appear instantaneous to the human user. For an example of this look at the demo Several Things at a Time

There is one other possibility which is that a button could cause code2 to run instantly followed by an immediate return to continue where it left off in code1 - but only if code2 can complete very quickly - 100 microseconds would be a long time.


Unless you have a very heavy task that can’t be split into a continuous loop while checking from time to time the press of the button, the cheapest way ($ and time) to implement it is really to let a code1 terminate an iteration before doing code2. a delay in reaction to a press of a button in human time is not sensible to a few milli-seconds which is a long time for a computer even as small as an arduino.

So you should instruct the developper to plan “code1” in a way that it does not prevent the main run loop() to loop.

It is usually better to tell your programmer what you want to happen, rather than how the code is to accomplish it. It's isn't "when the button is pressed, I want this code to run", but "when the button is pressed, the output on the pins should be like this"