Hi,
I've been writing various midi sequencers on the arduino. To those unfamiliar with a midi sequencer, it
sends a value to the serial tx after every set period of time. For instance 120 times per minute (120 bpm).
Currently I do something like:
loop()
{
// check if needed time has passed, if so send the next step of the sequence
// check for button presses etc.
}
the main problem i see is that the checking of passed time is all done inside the main loop. as i add code to check various buttons, pots, etc. i am afraid it could affect the accuracy of the timing.
i have enough familiarity with the concept of interrupts that i'm wondering if i'd be better off to install an interrupt that goes off every x milliseconds and runs the code that sends the next value in the sequence.
Is this the best way to do this? Is it possible with the arduino?
my current code is :
extern volatile unsigned long timer0_overflow_count;
unsigned long hpticks (void)
{
return (timer0_overflow_count << 8) + TCNT0;
}
void loop() {
int t1, t2;
t1 = hpticks() * 4;
t2 = hpticks() * 4;
while (1) {
if ((t2 - t1) >= pulse_interval) {
process_pulse();
t1 = hpticks() * 4;
}
t2 = hpticks() * 4;
}
}
The code is stuff I found in other peoples examples of how to get more accrute time than milis(). I think I'm most of the way there.
All advice appreciated.