|
| 1 | +/* ADC Multi Channel sampling usage demo |
| 2 | + * |
| 3 | + * Queries for pin numbers to sample on the Serial Monitor, then records and prints three readings on |
| 4 | + * each of those pins at a leisurely rate of 2 Hz. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <Arduino_AdvancedAnalog.h> |
| 8 | + |
| 9 | +AdvancedADC adc; |
| 10 | +uint64_t last_millis = 0; |
| 11 | +pin_size_t active_pins[AN_MAX_ADC_CHANNELS]; |
| 12 | +int num_active_pins = 0; |
| 13 | +const int samples_per_round = 3; |
| 14 | + |
| 15 | +void queryPins() { |
| 16 | + Serial.println("Enter pins to sample (number only, e.g. 3,4 for A3, and A4). Enter to repeat previous round."); |
| 17 | + |
| 18 | + int old_num_active_pins = num_active_pins; |
| 19 | + num_active_pins = 0; |
| 20 | + String buf; |
| 21 | + int c; |
| 22 | + do { |
| 23 | + c = Serial.read(); |
| 24 | + if (c < 0) continue; |
| 25 | + |
| 26 | + if (c == ',' || c == '\n') { |
| 27 | + buf.trim(); |
| 28 | + if (buf.length()) { |
| 29 | + active_pins[num_active_pins++] = buf.toInt() + A0; |
| 30 | + buf = String(); |
| 31 | + } |
| 32 | + } else { |
| 33 | + buf += (char) c; |
| 34 | + } |
| 35 | + } while (!(c == '\n' || num_active_pins >= AN_MAX_ADC_CHANNELS)); |
| 36 | + |
| 37 | + // No (valid) input? Repeat previous measurement cylce |
| 38 | + if (!num_active_pins) { |
| 39 | + num_active_pins = old_num_active_pins; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +void setup() { |
| 44 | + Serial.begin(9600); |
| 45 | + while (!Serial) {}; |
| 46 | +} |
| 47 | + |
| 48 | +void loop() { |
| 49 | + queryPins(); |
| 50 | + if (num_active_pins) { |
| 51 | + // Resolution, sample rate, number of samples per buffer per channel, queue depth, number of pins, array of pins. |
| 52 | + if (!adc.begin(AN_RESOLUTION_16, 2, 1, samples_per_round, num_active_pins, active_pins)) { |
| 53 | + Serial.println("Failed to start analog acquisition!"); |
| 54 | + while (1); |
| 55 | + } |
| 56 | + |
| 57 | + for (int i = 0; i < samples_per_round; ++i) { |
| 58 | + while(!adc.available()) {}; // Your code could do something useful while waiting! |
| 59 | + |
| 60 | + SampleBuffer buf = adc.read(); |
| 61 | + |
| 62 | + for (int i = 0; i < num_active_pins; ++i) { |
| 63 | + Serial.print(buf[i]); |
| 64 | + Serial.print(" "); |
| 65 | + } |
| 66 | + Serial.println(); |
| 67 | + |
| 68 | + // Release the buffer to return it to the pool. |
| 69 | + buf.release(); |
| 70 | + } |
| 71 | + |
| 72 | + adc.stop(); |
| 73 | + } |
| 74 | +} |
0 commit comments