Recipe 7 — Oscilloscope-Style Centered View¶
An oscilloscope-style view needs a stable time window, predictable sample timing, and reference lines that make the input range easy to read. This recipe configures a single time-domain plot in microseconds with visible ground and mid-rail references.
#include <ViewPoint.h>
using namespace viewpoint;
const float VREF = 3.3f;
const uint32_t SAMPLE_RATE = 10000; // 10 kSPS
const int DISPLAY_SAMPLES = 500;
const uint32_t SAMPLE_INTERVAL_US = 1000000UL / SAMPLE_RATE;
uint32_t lastSampleUs = 0;
void setup() {
view.begin();
view.setDelay(0);
view.setTitle("Oscilloscope");
float time_span_us = DISPLAY_SAMPLES * (1.0f / SAMPLE_RATE) * 1e6f;
float half_span = time_span_us / 2.0f;
view.setPlotTitle("Oscilloscope");
view.setVerticalRange(-0.5, 3.5, 8);
view.setHorizontalRange(-half_span, half_span, 10);
view.setAxisLabels("Time", "Voltage");
view.setUnits("us", "V");
view.addHorizontalReferenceLine(VREF / 2.0f, colors::DimGray);
view.addHorizontalReferenceLine(0.0f, colors::DimGray);
}
void loop() {
uint32_t now = micros();
if (now - lastSampleUs < SAMPLE_INTERVAL_US) return;
lastSampleUs += SAMPLE_INTERVAL_US;
float volts = analogRead(A0) * VREF / 1023.0f;
view.addData("Probe", volts);
view.send();
}
What the functions do
setDelay(0)disables the library's built-in delay so themicros()gate controls sample cadence.setHorizontalRange(-half_span, half_span, 10)gives the time-domain view a stable left/right span around zero.setVerticalRange(-0.5, 3.5, 8)frames a 0–3.3 V ADC signal with headroom above and below the rails.- Reference lines at
VREF / 2.0fand0.0fmark mid-rail and ground, common scope landmarks for single-supply signals.
See also
examples/Cartesian/SignalAnalyzer_A0/SignalAnalyzer_A0.ino