No doubt my question is basic for most of you, but all the same:
I'm designing a timer for my photographic enlarger.
Everything works fine so far but now I want to include a sort of clear function that stops anything the timer is doing and resets some values .
Of course I can use a reset but in that case all values will be initialized.
The Clear button is attached to ground and input 2 so that pressing will set input 2 to ground.
I have the following instructions in my sketch:
In the Setup: attachInterrupt (0, Clear, LOW);
Furthermore:
void Clear()
{
for (int i=0; i<=2; i++) { //switch off the LED's
analogWrite (LED[i], 0);}
for (int i=0; i<9; i++) { // set the reading of all switches to false
Sw[i] = false;}
BasicDsp(); // call the function to display the basic settings if the timer
loop(); // go to the loop function to start reading buttons etc.
}
It doesn't work, doesn't stop the timer from counting down during exposure,e etc.
I tried setting pin 2 as inputpin pulling it up to HIGH but wothout succes
What's wrong?
I supect that calling loop() inside an interrupt is probably a bad plan
you'd be better setting a ClearNow flag in the interrupt and handing it in your main loop
The point is that the interrupt should stop all activity, whatever it was doing.
Mostly this would be something in a for-loop in some function.
When i 'break' this loop in the interrupfunction the sketch will go back to the statement following the for-loop.
This is not what I want. I want to exit the function altogether
Must I now put an interrupt test in every function?
Seems a bit clumsy.
Why do you call loop at all? When your interrupt fires and calls your routine, I would call cli() at the beginning of your function, then sei() at the end of your function, to prevent any other interrupt whilst your routine executes.
SPlatten:
Why do you call loop at all? When your interrupt fires and calls your routine, I would call cli() at the beginning of your function, then sei() at the end of your function, to prevent any other interrupt whilst your routine executes.
That is how ISR routines work automatically, disable all interrupts globally upon entry, enable global interrupts when returning, you don't have do that yourself.
swdick:
The point is that the interrupt should stop all activity, whatever it was doing.
Mostly this would be something in a for-loop in some function.
...
Must I now put an interrupt test in every function?
Seems a bit clumsy.
Are you using delay() as well? Otherwise any for-loop will be over so fast you won't need to worry. In another thread I showed you could sort 800 numbers in 10 milliseconds. So the processor will get its stuff done fast enough for you. But if you have: delay (10000); then you need to restructure.
I've covered this in some depth here:
You should just be doing (relatively) minor things in functions, and then going back to the main loop for another "event". Something like: