I2C Message only sent if Serial Window open

An oddball problem!!

I have attached a file with the code that is part of my observatory automation project. Unfortunately it is too long to just paste into this posting.

This code runs on an Arduino Nano. One purpose of the code is to send a warning message, via I2C, to another Arduino board if the observatory dome has jammed. Jamming is detected by monitoring two pulse streams. The first stream are the pulses sent to a stepper motor that rotates the dome. These pulses are fed into Timer/Counter1 using the external clock input pin, T1. The Timer/Counter is operating in Compare mode and if the motor pulse count reaches a preset value then an interrupt is triggered and in the ISR, a safety warning flag is set.

The other pulse stream is from one channel of a rotary encoder. This is mechanically coupled to the dome separately from the motor. The pulses from the rotary encoder are detected using Pin Change interrupts. Each time the Pin Change interrupt occurs, the ISR resets T/C1 and sets the motor pulse count back to zero.

In the LOOP section of the code. the setting of the safety warning flag is detected by comparing with a previous value and if a change is found, an I2C message is sent to the other Arduino board.

Sorry about the long-winded explanation. The problem is easier to describe. Basically the code works as expected ONLY when the Serial output window for the jam detecting Arduino is open. I have no explanation for this at all. I ‘assume’ it has something to do with the fact that opening the Serial window resets the Arduino but I can’t find any reason why such a reset would be required. Also, if I open then close the Serial window, which will have reset the Arduino, the code doesn’t work.

Any suggestions or help would be very gratefully received as I am at a loss to know what to do.

Regards, Hugh

AB3_0528.ino (30.9 KB)

When in your program development did it quit working? Did it ever work like you wanted?


I have only just noticed the behaviour but the code for the 'jam detect' system is pretty new so it probably has always behaved this way. Today was the first tome I actually tested the code by physically pulling the rotary encoder from the dome when the dome was rotating. Previously I just tested the code to see if the Timer/Counter was working and would trigger an interrupt when expected.

I suggest removing the jam detect and check the I2C part separately.

edit: I have an I2C master and slave sketches running right now, and they are doing fine. If you would like to use them for testing, I'll post them. I have a Due as the master and a Mega as the slave, but I think the code will work on any Arduino board.

Hi Tim,

Thanks very much for your offer. I have had another look at my code and decided to refactor the message system to make it more modular and hopefully, more transparent which I hope will solve my problem. If not, I will post another question.

Regards, Hugh

check memory use.