Loading...
Searching...
No Matches
AnalogBurstBackground.ino

Samples A3 continuously in the background with analogStartBurst() and analogBurstDone(), in blocks of 1000 samples at 10000 samples per second. One buffer fills while the other is processed ("double buffering"), and the built-in LED blinks to show that loop() is never blocked. Prints the mean and the RMS of each block, in millivolts.

Output
Serial Monitor at 115200 baud, ten lines a second, for example:
Mean 1023.6 mV, RMS 0.41 mV
// Samples A3 in the background while the sketch keeps working. Each block
// of 1000 samples (0.1 s at 10000 samples/s) is captured into one buffer
// while the previous block, in the other buffer, is processed, so sampling
// restarts within microseconds ("double buffering"). The built-in LED blinks
// to show that loop() is not blocked.
//
// For each block, prints the mean and the RMS of the signal around the mean
// (the AC part), in millivolts.
const pin_size_t PIN = A3;
const uint32_t RATE_HZ = 10000;
const size_t BLOCK = 1000;
uint16_t buffers[2][BLOCK];
int filling = 0; // buffer the burst is writing to
unsigned long last_blink = 0;
void processBlock(const uint16_t* block) {
const float mv_per_step = analogFullScaleMillivolts(PIN) / 4096.0f;
float sum = 0;
for (size_t i = 0; i < BLOCK; i++) sum += block[i];
const float mean = sum / BLOCK;
float squares = 0;
for (size_t i = 0; i < BLOCK; i++) squares += (block[i] - mean) * (block[i] - mean);
Serial.print("Mean ");
Serial.print(mean * mv_per_step, 1);
Serial.print(" mV, RMS ");
Serial.print(sqrtf(squares / BLOCK) * mv_per_step, 2);
Serial.println(" mV");
}
void setup() {
Serial.begin(115200);
while (!Serial) {}
analogStartBurst(PIN, buffers[filling], BLOCK, RATE_HZ);
}
void loop() {
if (analogBurstDone()) {
const int full = filling;
filling = 1 - filling;
analogStartBurst(PIN, buffers[filling], BLOCK, RATE_HZ); // next block straight away
processBlock(buffers[full]);
}
// Other work carries on while the samples are taken.
if (millis() - last_blink >= 250) {
last_blink = millis();
}
}
@ OUTPUT
Definition Common.h:40
SerialUSBClass Serial
USB serial port, the one the Serial Monitor uses.
void analogReadResolution(int bits)
Sets the number of bits analogRead() returns, for every analog pin.
bool analogBurstDone()
Whether the burst started by analogStartBurst() has finished.
uint32_t analogStartBurst(pin_size_t pin, uint16_t *buffer, size_t count, uint32_t sample_rate_hz)
Starts a burst like analogReadBurst(), but returns at once; the samples are taken in the background.
uint32_t analogFullScaleMillivolts(pin_size_t pin)
Voltage that reads as full scale on an analog pin.
void digitalWrite(pin_size_t pinNumber, PinStatus status)
Sets an output pin HIGH (3.3 V) or LOW (0 V).
PinStatus digitalRead(pin_size_t pinNumber)
Reads the level on a pin.
void pinMode(pin_size_t pinNumber, PinMode pinMode)
Sets a pin as an input or an output.
void loop(void)
Written by the sketch: runs over and over after setup(), on core 0.
void setup(void)
Written by the sketch: runs once after reset, on core 0.
unsigned long millis(void)
Milliseconds since the board started.
uint8_t pin_size_t
Type for Arduino pin numbers (uint8_t).
Definition Common.h:112
#define LED_BUILTIN
On-board LED.
Definition pins_arduino.h:63