Here’s a thought which interrupted my beauty sleep at 5AM.
It’s a bit broad-brush, but the application is at the end.
It is interesting to reflect that this problem has been solved quite effectively already, in a mechanical clock. As I am sure you know better than me, a clock has two distinct parts – time, and strike (or bong, as we are calling it). The time side does just what the name says. If we ignore, for the moment, the arrangements for ‘warning’ (i.e. the just-before-the-strike arrangements preparing for the first strike in a particular sequence to be delivered swiftly) at the appropriate moment (quarters or hours), the time side trips the strike sequence. While the strike sequence is doing its thing, the time side carries on. There is also no possibility that a second strike sequence can be triggered during striking. After the trigger event, the strike does its job independently, including using a rack or a count wheel to determine how many strikes of the bell take place. When the strike sequence has finished, everything is reset, and the strike side waits for the next trigger.
One clear demonstration of this kind of signalling by the time mechanism to an independent strike mechanism is in a musical clock with lots of bells. Generally speaking, the whole of the musical side can be removed independently, leaving the time side just doing its own thing.
So; in the Arduino system, the RTC provides the time, and the Arduino can work out what that is in seconds, minutes, hours and so on, using an interrupt.
Perhaps that same method can be used to control an independent electronic strike ‘mechanism’.
One way is to try to have the Arduino fetch the time, decide when to trigger the bongs by looking for an appropriate time, and control the timing within the bong sequence. That’s a combined effort, but the time and strike ‘mechanisms’ are separate functions. It is true they run using just one processor, but that’s a convenience allowed by software. It does lead to the kinds of problems you (and we) have been experiencing, because we humans have to work out how t interleave what are actually separate functions hanging off just one processor.
A second way would be for the Arduino to fetch the time, decide when to trigger the bongs by looking for an appropriate time, then signal the number of bongs to another device which will sound the bongs and deal with their timing. The Arduino takes nothing to do with that, and just carried on dealing with the time. That’s pretty similar to what a mechanical clock does. You may say that is inefficient, but in a nod to established mechanical solutions, it can be implemented at low cost. Add a Nano or something similar, to handle the strikes.
Then, the Arduino repeatedly reads the time from the RTC and decides when, and how many, bongs should sound. It makes available a sound_the_bongs flag, and a number_of_bongs.
The second device does a somewhat similar job to the Arduino, to deal with bong number and duration. It waits for an interrupt generated by the Arduino sound_the_bongs flag, reads the number_of_bongs, and deals with the timing of signals to relays etc.
So the time and strike ‘mechanisms’ are essentially separate, and function much as in a mechanical clock.
The elements of most of the code required has already appeared in earlier posts.
If you have an Arduino and it is connected to an LCD display, for example, the LCD display essentially handles turning data into visible characters. The Arduino doesn’t do that part. All the Arduino does is trigger an update and make data available to the chip on the display. It’s the same in most computer systems, with the CPU passing signals to a graphics or sound or network card.
In theory, cunning programming should allow a single Arduino to handle both time and strike functions (see earlier post commenting on co-processing). In practice, I wonder if it is not simpler to separate them and use two processors.
Alternatively, fetch the time using an interrupt, then control the timing of the bonging using an internal timer on the same Arduino. That makes it appear as though the Arduino is doing two things at once. The wonder of speedy execution and juggling of electrons. It’s a mixture of hardware interrupts and software timers.
Then I fell asleep again, until I was interrupted by the alarm.
Marcus