Hi, I remember reading something saying that you shouldnt use delays inside interrupts. In my code I several internal interrupts to simplify the code.
Is this true even if I dont use any external interrupts?
What about lcd.print("..."); code inside ISR? Is it also a NO NO?
To explain what I'm doing is, printing date and time on the screen and checking button states every time loop turns. If lets say Menu button is pressed, Arduino jumps to an ISR to print something on LCD, activate an LED or relay etc. There are several sub-menus so every time I press menu button it jumps to another ISR. And it stays in the same ISR until menu button is pressed again by using while loop.
every time I press menu button it jumps to another ISR.
Do you really mean that, or do you mean that every time you press the menu button a different function (not an ISR) is called ?
I seem to remember commenting about this earlier today. Was that on another thread of yours ?
larryd:
See Trap #4
Gammon Forum : Electronics : Microprocessors : Arduino programming traps, tips and style guide
Dear Larryd, thanks for the link. I see that it strictly warns for not using delays inside interrupt. About printing inside, it warns not to Serialprint inside interrupt. However it does not say anything related lcd.print function. Is it also not good to use lcd.print?
UKHeliBob:
Do you really mean that, or do you mean that every time you press the menu button a different function (not an ISR) is called ?
I seem to remember commenting about this earlier today. Was that on another thread of yours ?
I might be using wrong term, what I meant is something like below. In this example ButtonA() and AlarmActivated() should be an ISR, am I right?
void loop
{...
....
...
if button A is pressed go to ButtonA()
....
...
lcd.print date
..
lcd.print time
...
if now.alarm== alarmMinute... go to AlarmActivated()
void ButtonA()
lcd.print("ButtonA is pressed")
void AlarmActivated()
lcd.print("Alarm is Activated")
Additionally I attached my full code. Note that my main loop just checks menu value and jump to another ISR(or function)
Irrigation_and_Curtain_System.ino (14.9 KB)
There are no external interrupts or interrupt service routines (ISR's) used by them in your code. Your button presses are calling functions. lcd.print() is fine to be called within a function.
What about lcd.print("..."); code inside ISR? Is it also a NO NO?
Yes. Your lcd is i2c and uses the Wire library. That protocol uses interrupts which are disabled inside of an ISR.
A parallel lcd might print within an ISR, but lcd printing is a slow operation and is not really suitable for an ISR.
Before going any further we need clarity on whether Interrupt Service Routines (ISRs) or functions are being used
As cattledog says
There are no external interrupts or interrupt service routines (ISR's) used by them in your code.
so what are being called are functions. Please stop referring to them as ISRs unless you change your code to use interrupts.
Functions such as delay(), millis(), Serial.print() and lcd.print() work in functions but not in ISRs
I totally got what you are saying. My main mistake was thinking that I was using ISRs while actually I was using functions. For now it is clear for me. Thank you very much for your support.
The rule of thumb in interrupts is...
Do as little as possible inside the ISR.
Capture the state that changed - perhaps as a flag or counter.
The. return to the main loop, and whenever needed,check that flag or counter and act as needed.
A bit like being interrupted at a dinner party.
Tap on shoulder... you: “hang on I’ll talk to you in a minute”.
Continue original conversation.
Only when original conversation is finished - you go look for the person who tapped on your shoulder.
(May not work exactly this way in the 21st century)