I have just written some code that sends a set of data over the serial port every 100ms using timer1 interrupts. I have set pin 13 to toggle every time it runs to get feedback on what it is doing. The problem is that this appears to toggle pin 13 once (ie turn it on) then stop, Pin 13 ceases to toggle and no data appears in the serial monitor. The problem appears to be with the for loop because without it the ISR runs fine. Any ideas on how to fix this?
mgiigm:
I have just written some code that sends a set of data over the serial port every 100ms using timer1 interrupts. I have set pin 13 to toggle every time it runs to get feedback on what it is doing. The problem is that this appears to toggle pin 13 once (ie turn it on) then stop, Pin 13 ceases to toggle and no data appears in the serial monitor. The problem appears to be with the for loop because without it the ISR runs fine. Any ideas on how to fix this?
Would I be right in thinking that sei() is user to enable interrupts?
If so this should be the same as the interrupts() function. I tried adding sei() in it's place and, unfortunately, it made no difference
PaulS:
ISRs need to be fast. Serial printing is not fast, and is generally not something you should do in an ISR.
The ISR sets a flag; loop() sees that the flag is set, and sends the data.
that's like a chicken and egg problem. If you ISR is just going to set a flag and then you check the flag in the loop, then you might as well just get rid of the whole ISR and just poll.
I think if what you need to do takes too long in an ISR or does something that will interfere with interrupts, then that means you should not be using ISR.
the code above inside loop() would seem more appropriate.
I think if what you need to do takes too long in an ISR or does something that will interfere with interrupts, then that means you should not be using ISR.
In general, I agree. The exception, as in this case, is when something needs to be done on a relatively regular basis. Having a timer generate a "It's time to do your thing" flag is easy. Then, loop() doesn't need to figure out if it's time to do something. It knows, because the flag is set.
mgiigm:
Would I be right in thinking that sei() is user to enable interrupts?
If so this should be the same as the interrupts() function. I tried adding sei() in it's place and, unfortunately, it made no difference
Huh. I suppose so. I guess I was confused because the code didn't look like a sketch. Rather, it looked like AVRlib style C code.
I don't know if I'll be much help, but I have an alternate solution that you can consider, as long as you don't use delay() in your loop. It uses millis() to repeat an action.
And I'm not sure that the digitalRead(13); will work either. If it doesn't do something else (clue: it requires a global or static boolean variable, and toggling it afterward.)
That's very similar to what I had before but i was trying to get it out of the loop. Also i didn't know you could feed a function as an argument into another function. Handy thing to know
The last code worked great and I ended up using something very similar in the final sketch, I won't post it because all of it except a few lines have nothing to do with this post and they are already covered in this post.
dkl65:
I don't know if I'll be much help, but I have an alternate solution that you can consider, as long as you don't use delay() in your loop. It uses millis() to repeat an action.