aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorMarti Bolivar <mbolivar@leaflabs.com>2011-08-31 11:35:27 -0400
committerMarti Bolivar <mbolivar@leaflabs.com>2011-08-31 13:06:07 -0400
commit5478c61a471f0a6b0668aeaecb027609effdee90 (patch)
treef2261ac66530d69f66f89f89d13910fb04dc80ac /examples
parentb8545bfea262e3fcd4e3796c8d61fdf3c6f9cb70 (diff)
downloadlibrambutan-5478c61a471f0a6b0668aeaecb027609effdee90.tar.gz
librambutan-5478c61a471f0a6b0668aeaecb027609effdee90.zip
test-session.cpp: Multiple ADC noise measurements by default.
Each call to measure_adc_noise() now does N_ADC_NOISE_MEASUREMENTS (currently 40) samples, instead of just 1.
Diffstat (limited to 'examples')
-rw-r--r--examples/test-session.cpp35
1 files changed, 20 insertions, 15 deletions
diff --git a/examples/test-session.cpp b/examples/test-session.cpp
index 97d5754..ea75410 100644
--- a/examples/test-session.cpp
+++ b/examples/test-session.cpp
@@ -12,6 +12,9 @@
// Default USART baud rate
#define BAUD 9600
+// Number of times to sample a pin per ADC noise measurement
+#define N_ADC_NOISE_MEASUREMENTS 40
+
uint8 gpio_state[BOARD_NR_GPIO_PINS];
const char* dummy_data = ("qwertyuiopasdfghjklzxcvbnmmmmmm,./1234567890-="
@@ -768,21 +771,23 @@ void measure_adc_noise(uint8 pin) {
// Variance algorithm from Welford, via Knuth, by way of Wikipedia:
// http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#On-line_algorithm
- for (int i = 1; i <= N; i++) {
- x = analogRead(pin);
- delta = x - mean;
- mean += delta / i;
- M2 = M2 + delta * (x - mean);
- }
-
- SerialUSB.print("header: D");
- SerialUSB.print(pin, DEC);
- SerialUSB.print("\tn: ");
- SerialUSB.print(N, DEC);
- SerialUSB.print("\tmean: ");
- SerialUSB.print(mean);
- SerialUSB.print("\tvariance: ");
- SerialUSB.println(M2 / (float)(N-1));
+ for (int sample = 0; sample < N_ADC_NOISE_MEASUREMENTS; sample++) {
+ for (int i = 1; i <= N; i++) {
+ x = analogRead(pin);
+ delta = x - mean;
+ mean += delta / i;
+ M2 = M2 + delta * (x - mean);
+ }
+ SerialUSB.print("header: D");
+ SerialUSB.print(pin, DEC);
+ SerialUSB.print("\tn: ");
+ SerialUSB.print(N, DEC);
+ SerialUSB.print("\tmean: ");
+ SerialUSB.print(mean);
+ SerialUSB.print("\tvariance: ");
+ SerialUSB.println(M2 / (float)(N-1));
+ }
+
pinMode(pin, OUTPUT);
}