use pushbutton interrupt to switch between 2 codes & run in loop

Hello Im trying to do my 1st project so Im really new to this but I'm stuck on this one problem. Basically what I have is two different functions that transmit IR pulses on pin 3, one is really short and other one is very long code. What Im trying to achieve is use a switch / push button to switch back and forthbetween each code and have each one be able to repeat non stop, as in a loop.. What I tried to do was run my 1st code in loop, then call interrupt isr routine to run 2nd code and use goto statement to make that repeat non stop. While this does work the problem I have is I cant come back to my 1st code. If I don't use goto then it does come back to loop but I need it to keep repeating.

One of my codes is basically this

void function() {
digitalWrite(LED, HIGH)
delayMicroseconds(12)
digitalWrite(LED, LOW)
delayMicroseconds(1)}

For this code I want only these 4 lines to keep repeating without checking for any conditions, flags, (that's why I used interrupt) because if I add more line(s) to this it disturbs the timing and it affects the IR led range. The other code also emits IR light but its very long, so how can I make both these to loop separately depending on switch position?? It will be great if someone can post a code for example.

You could use a variable function pointer, so loop () calls a function via the variable.
The interrupt assigns values to the variable.

If you use millis() or micros() to manage your timing there should be no problem with extra code disturbing the timing.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

The same techniques work with micros() if you need more precision.

...R

Thanks for replying, I'll look into how millis() or micros() is used, but I still need a way to switch between both codes.

@AWOL, I don't know about variable function pointer, can you give like an example how it would look in code.

I'll look into how millis() or micros() is used, but I still need a way to switch between both codes.

Using miilis() or micros() for timing allows you to manage the timing without blocking code execution so that you can check the state of an input pin very frequently and execute different code when the state changes.

If you don’t block the code with (stupid) delay() you have alllllllll the time in the world to poll the switch :slight_smile:

kashi786:
Thanks for replying, I'll look into how millis() or micros() is used, but I still need a way to switch between both codes.

Using millis() for non-blocking timing will facilitate that.

Every iteration through loop() check the button. Something like this

void loop() {
   buttonState = digitalRead(buttonPin);
   if (buttonState != prevButtonState) {
      if (buttonState == HIGH and chosenFunction == 'B') {
          chosenFunction = 'A';
      }
      else if (buttonState == HIGH and chosenFunction == 'A') {
          chosenFunction = 'B';
      }
   }
   prevButtonState = buttonState;
 
   if (chosenFunction == 'A) {
       // call one function
   }
   else {
      // call the other function
   }
}

...R

ok i get it now, thanks guys for explaining, I will try to test my code using millis or micros... I guess if I can continously listen for switch, then i don't have to use interrupt also.

I guess if I can continously listen for switch, then i don't have to use interrupt also.

That's correct