# Using an ISR to start over on a Nano

**URL:** <https://forum.arduino.cc/t/using-an-isr-to-start-over-on-a-nano/1158993>\
**Category:** Programming\
**Created:** [August 17, 2023, 5:21am UTC](https://forum.arduino.cc/t/using-an-isr-to-start-over-on-a-nano/1158993 "2023-08-17T05:21:12Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [August 17, 2023, 12:59pm UTC](https://forum.arduino.cc/t/using-an-isr-to-start-over-on-a-nano/1158993/9 "2023-08-17T12:59:32Z")

</div>

There are many ways to handle this issue.

Here's one off the wall. It uses the setjmp/longjmp mechanism to switch by pushbutton on an interrupt, between as many _loop_() -like functions as you have room for. Try it out, the demo switches between three loop-like functions.

  

![Wokwi_badge](https://user-images.githubusercontent.com/63488701/212449119-a8510897-c860-4545-8c1a-794169547ba1.svg) [setjmp/longjmp demo](https://wokwi.com/projects/373311239978862593)

  

```cpp
// https://wokwi.com/projects/373311239978862593
// https://forum.arduino.cc/t/using-an-isr-to-start-over-on-a-nano/1158993

// also see post 30, and the thread that this came from
// https://forum.arduino.cc/t/question-that-probably-has-a-really-obvious-answer-but-im-just-too-dumb-to-find-it/887654/30

/* setjmp example */

# include <setjmp.h>

# define numLoops	3
# define myButton	3
# define theLED	LED_BUILTIN

static jmp_buf buf;

int whichLoop = 0;

unsigned char once = 0;

volatile unsigned char ignore = 0;

volatile int ignored = 0;

void myISR()
{
	if (ignore) {
		ignored++;
		return;
	}

	detachInterrupt(digitalPinToInterrupt(myButton));
	ignore = 1;
	
	/* and yank the big lever */
	longjmp(buf, 1);
}

void setup() {

	Serial.begin(115200);
	Serial.println("hello setjmp/longjmp world!\n");

	pinMode(myButton, INPUT_PULLUP);
	pinMode(theLED, OUTPUT);
}

void loop()
{
	for (; ; ) {
		while (1) {
			if (!setjmp(buf)) {

				EIFR |= (1 << digitalPinToInterrupt(myButton)); // clear flag on button interrupt
				attachInterrupt(digitalPinToInterrupt(myButton), myISR, FALLING);
// delay(20);
				ignore = 0;
				break; // normal return, just start looping
			}
			else { // big lever return, do whatever, then start mostly all over
				whichLoop++;
				if (whichLoop >= numLoops)
					whichLoop = 0;
	
				Serial.println("");
				if (ignored) { Serial.print(" ignored "); Serial.print(ignored); }
				Serial.print("switching to loop "); Serial.print(whichLoop);
				Serial.println("");
				delay(500); // very poor man's debounce - get off that switch
			}
		}
		for (; ; )
			loopDispatch();
	}
}

void loopDispatch() {
	switch (whichLoop) {
	case 0 :
		loop0();
		break;

	case 1 :
		loop1();
		break;

	case 2 :
		loop2();
		break;
	}
}

void loop0()
{
	static unsigned char toggle;

	digitalWrite(LED_BUILTIN, ++toggle & 0x1);

	delay(333);
}

void loop1()
{
	int counter = 0;

	for (; ; ) {
		Serial.print("loop 1 : ");
		Serial.println(counter);

		counter++;

		delay(33);
	}
}

void loop2()
{
	static int counter = 0;

	Serial.print("loop 2 : ");
	Serial.println(counter);

	counter++;

	delay(33);
}

```

  
There is only the need to get off the button in less than 500 milliseconds, otherwise you'll skip ahead in the cycle of loops.

It's a bit delicate, so don't get too creative with the mechanism.

Close readers will wonder about this

```arduino
void loop()
{
	for (; ; ) {

```

captivated loop with a infinite loop... read here:

> [@Question that probably has a really obvious answer but I'm just too dumb to find it](https://forum.arduino.cc/t/question-that-probably-has-a-really-obvious-answer-but-im-just-too-dumb-to-find-it/887654):
>
> Hi! So I am a total noob and am just starting out with Arduino and coding in general. I've recently bought a Neopixel LED strip and came to the conclusion that I want to build something with it. I'm thinking a control panel with buttons on it, with each button controlling a different routine that the strip loops through. If I press the first button the LED-s loop through routine nr.1, but as soon as I press another button, the strip should change to the respective routine. I'm thinking to use in…

  

a7

---

_[View the full topic](https://forum.arduino.cc/t/using-an-isr-to-start-over-on-a-nano/1158993)._
