Hello,
I’m working on a musical sequencer project and could use a second opinion on some code. I’m using an Arduino Due and I’m taking advantage of the DueTimer.h library.
Originally, my code was organized like this:
Timer0.attachInterrupt(clockTickInterrupt).setFrequency(192).start();
void loop()
{
// poll inputs
}
void clockTickInterrupt()
{
// step sequencer and output sequencer notes
}
This worked great. But I wanted to add a feature where the sequence could be changed based on some input information. Those changes could be (but aren’t always) a tiny bit CPU intensive and only need to happen before the sequencer outputs notes. One solution would be to put that code in the clockTickInterrupt, like so:
Timer0.attachInterrupt(clockTickInterrupt).setFrequency(192).start();
void loop()
{
// poll inputs
}
void clockTickInterrupt()
{
// do calculations <==== added here
// step sequencer and output sequencer notes
}
But those calculations are not always going to take the same amount of time, and putting them here will cause my sequencer’s timing to be jittery.
Another option would be to add the calculations to the main loop:
void loop()
{
// poll inputs
// do calculations <==== added here
}
void clockTickInterrupt()
{
// step sequencer and output sequencer notes
}
But now I’m running the calculations a lot more than necessary. I only need to run them once per interrupt cycle.
So, my solution is to add another interrupt:
Timer0.attachInterrupt(clockTickInterrupt).setFrequency(192).start();
Timer1.attachInterrupt(computationInterrupt).setFrequency(384).start();
void loop()
{
// poll inputs
}
void clockTickInterrupt()
{
// step sequencer and output sequencer notes
}
void calculationInterrupt()
{
// do calculations <==== added here
}
Since the calculationInterrupt is set to happen at twice the speed of the clockTickInterrupt, it’s certain to run before the clockTickInterrupt. The only drawback, that I can see, is that the calculations get run twice when they only need to be run once. I maybe able to run the calculationInterrupt at the same speed as the clockTickInterrupt. I haven’t thought that entirely through yet.
Any suggestions? Does it sound like I’m on the right track?
Cheers,
Bret