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.
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.
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?
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)
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.
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.
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.