Skip to content

Commit d989bcf

Browse files
authored
Merge pull request #38 from tfry-git/tfry-git-patch-1-1
Allow setting list of pins to sample after construction
2 parents c506380 + 4c36569 commit d989bcf

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

src/AdvancedADC.h

+9
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,19 @@ class AdvancedADC {
4242
adc_pins[n_channels++] = analogPinToPinName(p);
4343
}
4444
}
45+
AdvancedADC(): n_channels(0), descr(nullptr) {}
4546
~AdvancedADC();
4647
bool available();
4748
SampleBuffer read();
4849
int begin(uint32_t resolution, uint32_t sample_rate, size_t n_samples, size_t n_buffers);
50+
int begin(uint32_t resolution, uint32_t sample_rate, size_t n_samples, size_t n_buffers, size_t n_pins, pin_size_t *pins) {
51+
if (n_pins > AN_MAX_ADC_CHANNELS) n_pins = AN_MAX_ADC_CHANNELS;
52+
for (size_t i = 0; i < n_pins; ++i) {
53+
adc_pins[i] = analogPinToPinName(pins[i]);
54+
}
55+
n_channels = n_pins;
56+
return begin(resolution, sample_rate, n_samples, n_buffers);
57+
}
4958
int stop();
5059
};
5160

0 commit comments

Comments
 (0)