Going back to loop after hardware interrupt

Hi, I am new to arduino forums so will try my best to be clear in what I am asking. I have built an 8x8x8 RGB Led cube that at the moment is running of an arduino mega. There is a number of different patterns that the cube can display and they run in sequence, however I have included a button that will allow the user to select which pattern the cube will display rather than displaying them in sequence. Every time the button is pressed the display pattern is changed to the next one in sequence. To do this I have attached the button to int 1 on the mega and set a global variable stateChange to advance by one every time the button is pressed and then a case statement selects which pattern is displayed. This works fine but for one problem. Every time the button is pressed I have to wait for the current display pattern to finish before it moves on to the next pattern. I want the current display pattern to stop and the new pattern to start immediately when the button is pressed.
Every time the button is pressed the stateChange variable is incremented, what I would like to do is instead of going back to where the interrupt was called to exit all procedures and start from the loop() procedure again. Is this possible? I know I could implement a flag variable and poll for that in the for loops of the various patterns and if set to true could exit the for loops but that would involve a lot of polling and may slow the execution of the program down considerably. Alternatively is it possible to reset the arduino but somehow have my stateChange variable persistent in the memory and did not get reset? Any help would be appreciated.

John

Hello and welcome,

I'm not totally sure to understand your problem, have you tried calling loop() ?

void loop()
{
   ...
   if (buttonpressed)
   {
       ...
       loop();
   }
}

And you can save/read your stateChange variable in EEPROM but only do this when necessary

have you tried calling loop() ?

No!

So many things wrong there - SerialEvent not getting called is the least of your worries - recursion will kill your stack in microseconds.

If you want "loop()" to repeat, just return from it, but never call it yourself unless you really know what you're doing.

Hi I have not actually thought of this but I think that this might cause the program to be unstable. When the button is pressed it calls a hardware interrupt which just increments a counter variable. The counter variable is used to select the display pattern. I think calling loop from inside an interrupt routine could be a bad idea. But thanks for your comment.

John

This is the programming section, so normally, we like to see some code.
It may be that you're getting multiple interrupts from switch bounce.

Hi thanks for your reply, but I am not sure what you mean by "return from it" The button is attached to an interrupt so if I put a return in that interrupt routine will the program return back to the start of loop() rather than where the interrupt was called?

If you want "loop()" to repeat from the top, then instead of calling "loop()" again as guix suggested,
simply "return".

void loop ()
{
  ...
  ...

  if (someCondition) {
    return;
  }
  ...
...
}

Hi again the problem is not switch bounce the code is working the problem is i want the display pattern to switch immediately rather than waiting for the current pattern to finish its sequence and then switching. I know I have not included code in this explanation as I don't have the code to hand but tonight I will try and post the code. The basic problem is I don't want to return to where the interrupt was called I want it to go back and start from the loop() function.

The basic problem is I don't want to return to where the interrupt was called

What you want and what the processor is capable of look to be mutually incompatible.
That isn't how interrupts work.

An interrupt is just that - a interruption to your normal flow of work or events.

You don't sit watching TV, hear the doorbell ring, go to answer the fron door and when you've finished, go back to baking your cake, do you?
(In the world of operating systems, yes, maybe you do, but not on bare-bones microcontrollers)

I'll wait for code.

Checking a global flag and exiting in the innermost loop of your pattern generators is the best way to cleanly transition between patterns.

If you really want to kludge the restart, look into using the watchdog timer to reboot the processor, and use the EEPROM to save which pattern you want for the next boot cycle.

-br

Checking a global flag and exiting in the innermost loop of your pattern generators is the best way to cleanly transition between patterns.

Depending on how the code is structured, simply setting a bunch of variables in the interrupt may be the simplest way of restarting.

But we won't know until we see the code.

Hi Guys sorry did not have time to post code but I have given it some more thought and I think what I want to do interrupts just cant do so I am going to go down the rout of storing my pattern variable in EEPROM and doing a software reset every time the mode button is pressed. This approach should work fine for what I want to do. Thanks for all who responded.

John

... and doing a software reset every time the mode button is pressed ...

A software reset? What is that? Resets are done by hardware. Full stop, no arguments.

It is possible to reset the arduino with software. One method is to connect the reset pin to another of the arduino pins and set it as an output pin then all you need to do is set that pin to low and the arduino will reset. There is another method that I have read about as well somewhere I think there is a discussion somewhere in these forums about it.

John

That method is not recommended because the output pins immediately go tri-state before they have time to hold reset low long enough.

I don't like lazy programming, but if you're going to "fix" your problem via reset, use the watchdog; that's what it is there for.