Skip to content

Commit 139e749

Browse files
author
Kevin J Walters
committed
Adjustments to make it work on Arduino Uno as well as ESP32.
Have to use dtostrf on Arduino Uno as print functions like snprintf do not support %f. Using custom type to deal with float on Uno and double on ESP32.
1 parent 0bad0fb commit 139e749

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

adc/adc-comparison/adc-test-1/adc-test-1.ino

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// Print values from ADC to compare between boards
33
// Port of CircuitPython version intended for ESP boards
44

5+
#ifdef ESP32
6+
#define fptype double
7+
#else
8+
#define fptype float
9+
#endif
510

611
int input_pin = 4; // GP4 on ESP32
712
unsigned int sample_sizes[] = {1, 8, 50};
@@ -12,23 +17,32 @@ int dummy_out_value = -1;
1217
char output_line[OUTPUT_LINE_LEN]; // not NUL terminated
1318

1419
#define TIME_FN() (micros() - program_start_time)
15-
double time_mul = 1e-6;
20+
fptype time_mul = 1e-6;
1621
unsigned long program_start_time = 0L;
1722
unsigned long last_loop = 0L;
1823
unsigned long interval = 25L * 1000L; // 25ms
1924

25+
#ifdef ESP32
2026
#define SAMPLE_BITS 12
27+
#else
28+
#define SAMPLE_BITS 10
29+
#endif
2130
double cp_mul = 65535.0 / ((1 << SAMPLE_BITS) - 1);
2231

2332

2433
void setup() {
34+
#ifndef ESP32
35+
analogReference(EXTERNAL); // Set to 3.3V
36+
#endif
2537
Serial.begin(115200);
2638
while (!Serial); // wait for it to be ready
2739

40+
#ifdef ESP32
2841
// ESP32 defaults to 12bit -11dB
2942
// Some reports that 11bit has better performance >3.1V
3043
analogSetWidth(SAMPLE_BITS);
3144
analogReadResolution(SAMPLE_BITS);
45+
#endif
3246
program_start_time = TIME_FN();
3347

3448
delay(30 * 1000); // 30 second pause before starting
@@ -51,21 +65,35 @@ void loop() {
5165
for (int i=0; i < sample_sizes[s_idx]; i++) {
5266
sample_sum += analogRead(input_pin);
5367
}
54-
double in_value = (double)sample_sum / (double)sample_sizes[s_idx];
68+
fptype in_value = (fptype)sample_sum / (fptype)sample_sizes[s_idx];
69+
#ifdef ESP32
5570
snprintf(output_line,
56-
sizeof(output_line) / sizeof(output_line[0]),
71+
sizeof(output_line),
5772
"%f,%d,%u,%f",
58-
(double)in_start_t * time_mul,
73+
(fptype)in_start_t * time_mul,
5974
dummy_out_value,
6075
sample_sizes[s_idx],
6176
in_value * cp_mul); // convert to 0-65535.0 range
62-
77+
#else
78+
// 1+5+1+6+1 = 14
79+
static char str_float1[14], str_float2[14];
80+
dtostrf((fptype)in_start_t * time_mul, 5, 6 ,str_float1);
81+
dtostrf(in_value * cp_mul, 5, 6 ,str_float2);
82+
snprintf(output_line,
83+
sizeof(output_line),
84+
"%s,%d,%u,%s",
85+
str_float1,
86+
dummy_out_value,
87+
sample_sizes[s_idx],
88+
str_float2);
89+
#endif
6390
// Pad white whitespace but leave room for CRLF
6491
for (int i = strlen(output_line); i < (OUTPUT_LINE_LEN - 2) ; i++) {
6592
output_line[i] = ' ';
6693
}
94+
// This is not NUL terminated
6795
output_line[OUTPUT_LINE_LEN - 2 ] = '\r'; // CR
68-
output_line[OUTPUT_LINE_LEN - 2 ] = '\n'; // LF
96+
output_line[OUTPUT_LINE_LEN - 1 ] = '\n'; // LF
6997
Serial.write(output_line, OUTPUT_LINE_LEN);
7098
}
7199
}

0 commit comments

Comments
 (0)