Hi!
I've got a problem I've isolated as best I can to something "quirky" in the Arduino Due ADC that I can't quite figure out.
I've got a project where I need to rapidly sample ADC channels 0, 1, 4, and 5.
I have found that on the Due, I can get away with sequential ADC reads without an intolerable amount of noise, so the below loop is what I'm using to collect 4000 sequential samples at full resolution and format them into an array to send to my computer over the serial port interleaved.
uint8_t analog[16000];
for(int i=0; i<4000; i++) {
unsigned int analogvalue4 = analogRead(A0);
unsigned int analogvalue5 = analogRead(A1);
analog[4*i] = (analogvalue4 >> 8) & 0xFF;
analog[4*i+1] = analogvalue4 & 0xFF;
analog[4*i+2] = (analogvalue5 >> 8) & 0xFF;
analog[4*i+3] = analogvalue5 & 0xFF;
delayMicroseconds(ADCdelay);
}
SerialUSB.write(analog, 16000);
The results from this close match my scope, even with ADCdelay at just 2 microseconds. Some popping, but I can filter that out in postprocessing and since I'm measuring phase I'd rather have more accurate timing than less noise.
However, this code breaks everything for reasons that escape me.
uint8_t analog[16000];
analogRead(A4);
delay(1);
for(int i=0; i<4000; i++) {
unsigned int analogvalue4 = analogRead(A0);
unsigned int analogvalue5 = analogRead(A1);
analog[4*i] = (analogvalue4 >> 8) & 0xFF;
analog[4*i+1] = analogvalue4 & 0xFF;
analog[4*i+2] = (analogvalue5 >> 8) & 0xFF;
analog[4*i+3] = analogvalue5 & 0xFF;
delayMicroseconds(ADCdelay);
}
SerialUSB.write(analog, 16000);
Even with a full millisecond of delay between doing a single shot measurement on A4, for some reason now A1 is giving me results that look like they are either A4 of a signal strongly influenced by A4.
Anyone have any ideas? I tried to find a way to... disconnect A4 or something, but didn't find any obvious way to do that. I tried setting the channel to an output (which I'm not sure is feasible anyway since the sensor is also a voltage source), but even that didn't help.
Ultimately I want to do something like this:
uint8_t analog[32000];
for(int i=0; i<4000; i++) {
unsigned int analogvalue0 = analogRead(A0);
unsigned int analogvalue1 = analogRead(A1);
analog[8*i] = (analogvalue0 >> 8) & 0xFF;
analog[8*i+1] = analogvalue0 & 0xFF;
analog[8*i+2] = (analogvalue1 >> 8) & 0xFF;
analog[8*i+3] = analogvalue1 & 0xFF;
delayMicroseconds(ADCdelay);
}
delay(1);
for(int i=0; i<4000; i++) {
unsigned int analogvalue4 = analogRead(A4);
unsigned int analogvalue5 = analogRead(A5);
analog[8*i+4] = (analogvalue4 >> 8) & 0xFF;
analog[8*i+5] = analogvalue4 & 0xFF;
analog[8*i+6] = (analogvalue5 >> 8) & 0xFF;
analog[8*i+7] = analogvalue5 & 0xFF;
delayMicroseconds(ADCdelay);
}
SerialUSB.write(analog, 32000);
In order to measure the relative phase on A0/A1 first and then A4/A5 after a delay for letting things settle (although that shouldn't really be necessary), but obviously that's completely out of the question if just doing one read on A4 breaks A0 and A1... very odd.