I'm a software developer who has recently become interested in the Arduino, and now I'm trying my hand at interrupts.
A decent exercise in this area, I figured, would be to write some interrupt-driven code to handle a string of HC-SR04 ultrasonic transducers. I figure I can have each one of them attached to a ring buffer, and round-robin them in the background. Foreground code can then grab copies of the ring buffers whenever needed.
If you haven't worked with the HC-SR04 before, the deal is that you pulse its trigger pin for 10us, and then wait for a pulse from its echo pin, measure the length of that pulse, and derive distance-to-obstacle proportionally.
Since I have many transducers and only two external-interrupt pins, I figured I'd put a multiplexer between the HC-SR04s and pin 2.
So this is what I think I want to do, simplified and in pseudocode, for just one transducer, assuming that somehow we can get an interrupt to kick this all off:
ISR A:
Set trigger pin HIGH
Set alarm for 10us on ISR B
RETI
ISR B:
Set multiplexer on pin 2 to listen to correct echo pin
Set trigger pin LOW
Set watch for pin 2 to go HIGH on ISR C
RETI
ISR C:
Start timer
Set watch for pin 2 to go LOW on ISR D
RETI
ISR D:
Stop timer
Set to ignore activity on pin 2
Compute distance 0-255, apply to ring buffer
Advance pointers and whatnot to next transducer
Set alarm for whatever portion of 50ms has not yet elapsed on ISR A
I figure I can use something like this to get 20 readings per second, or 20 / n readings per second from each of n transducers. (My thinking is that pulsing more than one at once might cause crosstalk. Plus, it'd be hard.)
But to do this, I'm going to have to point Timer 2 at a succession of different ISRs--most clearly ISR A and ISR B, but I'm also planning to use it to time out the other steps if needed. That means I can't use the ISR() macro, because it's a compile-time thing.
I'm sure I can use absolute memory pointers, if I have to, to waller around with interrupt vectors, but is there an easier, prettier way to do this? Or perhaps there isn't, because nobody who really knew anything about interrupts would even think of approaching it this way?