Dear forum readers!
I have a problem using Wire library and interrupts together. Below you will find short program which represent the problem: program newer print "Test point: 2". I use Arduino Mega 2560. If I remove part one (// Wire) or part two (// Interrupts) everything is OK.
Hope you have some suggestions how to solve this problem or what to try.
This just tells the realtime clock that you are going to read or write to/from its register zero, but then you do neither. It is also NOT a good idea to send to the clock so frequently because it will make the clock drift much faster than it would normally.
This prints a message, turns interrupts on (why?), waits a second, turns interrupts off (WHY?) and prints another message (with interrupts off, maybe it doesn't print).
I think the RTC might, by default, output a 1Hz signal. If it does, and if you have the SQW signal connected to the correct pin, an interrupt will occur once every second. This will cause the interrupt routine to increment puls but you don't do anything with it so I can't see how you know whether or not the clock is interrupting. Turning off interrupts is also going to complicate things.
Sorry that I forget to point out (it was obviously for me) that the program does nothing sensible but it is just (minimal) example that using Wire library together with interrupts don't work in the provided example.
I can provide (quite longer) program which do something useful but this doesn't make sense for me. Maybe it is better to imagine that the first part of the program (// Wire) reads time values from the DS1307 chip (as in example in void get_time() on this page: Simple Labs' Quick Start Kit for Arduino: Simple Labs' Quick Start Kit for Arduino - DS1307 Real Time Clock IC Interfacing- How To?) and the second part of the program (// Interrupt) counts the number of interrupts (actually rising signal, let say from the hall effect sensor) from Arduino digital input D2 (interrupt 0 location on Arduino Mega) in the time period of one second.
Maybe it is necessary that I mention that the second part of the program has nothing to do with the first (so measuring interrupts from hall effect sensor and reading time values from DS1307 chip are two separate things). I just want to have a program that first: reads values from clock and second: count the number of interrupts in one second period. But I have no ideas what this program does not work ...
Suggestions are welcomed!
Thanks,
Rok
thank you for this suggestion!
If I remove cli(); program is working.
But in this case the hall effect sensor will call interrupt() void all the time. Generally it is nothing wrong with this but it is not optimal (program will have to deal with interrupts when this is not necessary).
Hi
I am using arduino uno and faced similar kind of issue when when I was trying to periodically update display from DS1307 using TimerOne interrupt. I have got one solution that might be helpful.
After 50 days the millis() goes to 0. So the arduino need to be restarted.
The timer interrupt is not accurate. It executes all remaining loop code every time then only it can goes to the timerISR again. So the execution time of whole loop should be much lower than timer interval. In case of updating time from RTC generally we update display in 0.5 sec and if the execution time of loop is few mili sec then this solution might be useful.