goto statement in ISR?

i want to use goto command for interrupt, like when trigger interrupt, goto statement runs. i have tried but didint work, iread alot of articals comments blogs of arduino interrupt and all of thems points that keep your ISR as short as possible. using goto statement is a solution for me or any solutions?

You can use a goto in an interrupt routine, but I'd be hard pressed to think of a reason why you would want or need to. The normal method is to use attach to have a small function called when an interrupt is triggered. That function usually does little but set a global volatile flag/count or two that can be checked in the loop function.

z_e_u_s: i want to use goto command for interrupt, like when trigger interrupt, goto statement runs. i have tried but didint work, iread alot of articals comments blogs of arduino interrupt and all of thems points that keep your ISR as short as possible. using goto statement is a solution for me or any solutions?

The best way to handle a interrupt ISR is to just have it set a flag variable and return to the main sketch loop. Then the loop can test the flag variable, reset the flag and call functions as desired.

Lefty

i want use goto keep short ISR but in my program have 2 interrupts,

z_e_u_s: i want use goto keep short ISR but in my program have 2 interrupts,

Keep in mind that while your ISR is active all other interrupts are disabled, and doing a goto a function does not reenable them, only by returning from the ISR will the global interrupts be reenabled. The general rule for ISR is keep it short and fast, do the real work in the main sketch.

Lefty

Please post your code!

The use of goto in higher level languages is not advised, often there are equally good or better alternatives.

Be aware that you NEVER should goto somewhere outside the interruopt routine.

Rob

z_e_u_s:
i want use goto keep short ISR but in my program have 2 interrupts,

And how do you think goto is going to keep your ISR short? I hope you don’t think that any code you goto isn’t part of your ISR.

@Jraskell

And how do you think goto is going to keep your ISR short?

how about :

void IRQ()
{
goto end;

end:
}

;)

robtillaart: how about :

void IRQ()
{
goto end;

end: }



;)

How about:

void IRQ()
{
return;
}

:P

[quote author=Nick Gammon link=topic=68841.msg509258#msg509258 date=1312922021]

robtillaart: how about :

void IRQ()
{
goto end;

end: }



;)

How about:

void IRQ()
{
return;
}

:P

[/quote]

How about:

void IRQ(){}

Or perhaps:


xxx:
void ISR(){
goto xxx;
}

is it possible or using return where i am retun?

z_e_u_s: iread alot of articals comments blogs of arduino interrupt and all of thems points that keep your ISR as short as possible.

jraskell: Or perhaps:


Yes, it doesn't get much shorter than that. :P

z_e_u_s: xxx: void ISR(){ goto xxx; }

is it possible or using return where i am retun?

No. Why do you want to?

ok everybody change my mind not to use goto but i have long function when intterupt occurs i want to a do long function again this is my problem how can i do? firsty goto is solution but not.

Zeus,

After the interrupt completes the sketch will continue were it was interrupted. If that was in the long function it automatically continue there.

Or do you want to start your long function from the beginning when an interrupt occurs? Very unusual but ????

yes i want to start agin that function

What does this "long" function do? Can you post it? Is it long in physical lines of code, or long in that it does something that takes a long time?

yes it takes long time i want to explain my code but i'm working autonomous robot, when collosion detect, robot stop motors 100ms then goback very short distance and turnleft ===> this is my long function robot use switch sensor to detect collisons, some of my tests show that sometimes more than one crash happen in long function ends, i used interrupt, but this time i'm get stuck in ISR, i want to when switch interrupts, start long function again.

i want to when switch interrupts, start long function again.

What long function? I think it is time you told us about this long function.

It is also likely that you need to re-write this long function so that it doesn't take so long.

Then you would need to rewind the stack and ensure that all non atomic variable accesses are either cleaned up or do not cause any strange side effects. Rewinding the stack is never achieved with goto.

void ISR() {
  goStop();
  delay(100);
  goBackward();    
  delay(100);
  goStop();
  delay(100);
  goLeft();
  delay(100);
  goStop();
  delay(100);
  digitalWrite(redledPin,HIGH); 
  delay(100); 
}

this my ISR code...