Is there any way in Wiring to do the kind of two stage analog read you can do on a PIC chip where you start a conversion, then keep doing something else while you test busy or wait for an interrupt, then read the final value when it's ready?
I'm currently working on a project to build a position sensitive drum pad using the arduino board, and although I don't need this function yet, I can see it would be very useful when trying to control more than one pad from the same board.
If there isn't any way to do this at the moment, are there any plans to include it in the future, or else how difficult would it be to write a library to do this?
Is there a way to use the input capture pin together with the analogue inputs?
What I'm really looking for is 4 functions like this:
void AnalogStart(pin) // begins analog conversion on a pin
char AnalogBusy(pin) // tests whether conversion is complete
int AnalogResult(pin) // reads result of completed conversion
void AnalogInterrupt(pin,func) // sets func to be the interrupt handler for analog conversions on a pin.
OK. Thanks for this. I had a feeling it must be possible but didn't see how to do it in wiring, so I guess the answer is to write my own library when I need it.
int analogRead(uint8_t pin)
{
uint8_t low, high, ch = analogInPinToBit(pin);
// set the analog reference (high two bits of ADMUX) and select the
// channel (low 4 bits). this also sets ADLAR (left-adjust result)
// to 0 (the default).
ADMUX = (analog_reference << 6) | (pin & 0x0f);
// without a delay, we seem to read from the wrong channel
//delay(1);
// start the conversion
sbi(ADCSRA, ADSC);
// ADSC is cleared when the conversion finishes
while (bit_is_set(ADCSRA, ADSC));
// we have to read ADCL first; doing so locks both ADCL
// and ADCH until ADCH is read. reading ADCL second would
// cause the results of each conversion to be discarded,
// as ADCL and ADCH would be locked when it completed.
low = ADCL;
high = ADCH;
// combine the two bytes
return (high << 8) | low;
}
It's pretty obvious from here how to adapt it to do what I need, so thanks for the pointer.