Recipe 8 — Electrocardiogram-Style Strip Chart¶
An ECG-style strip chart is a good example of domain-specific plot configuration: the grid spacing, colors, units, baseline, and scroll rate all work together. This recipe shows how those calls combine to produce a recognizable medical-paper display. See Plot Styles.
Recipe 8 — pink-grid ECG strip chart with a baseline
#include <ViewPoint.h>
#include <math.h>
float phase = 0.0f;
float ecgSample(float p) {
if (p < 0.10f) return 0.15f * sinf((p / 0.10f) * PI);
if (p < 0.16f) return 0.0f;
if (p < 0.18f) return -0.10f * sinf(((p - 0.16f) / 0.02f) * PI);
if (p < 0.22f) return 1.20f * sinf(((p - 0.18f) / 0.04f) * PI);
if (p < 0.24f) return -0.20f * sinf(((p - 0.22f) / 0.02f) * PI);
if (p < 0.40f) return 0.05f;
if (p < 0.56f) return 0.30f * sinf(((p - 0.40f) / 0.16f) * PI);
return 0.0f;
}
void setup() {
view.begin();
view.setDelay(4);
view.setTitle("Heartbeat ECG");
view.setPlotTitle("Lead II");
view.setAxisLabels("Time", "Voltage");
view.setUnits("s", "mV");
view.setVerticalRange(-0.5, 1.5, 0.1, 0.5);
view.setGridColors(0xFFCCCC, 0xFF6666);
view.addHorizontalReferenceLine(0.0f);
view.trace("Lead II").setColor(0x00AA00);
}
void loop() {
view.addData("Lead II", ecgSample(phase));
view.send();
phase += 0.0048f;
if (phase >= 1.0f) phase -= 1.0f;
}
What the functions do
setVerticalRange(-0.5, 1.5, 0.1, 0.5)creates the small and large divisions expected on ECG plots.setGridColors(0xFFCCCC, 0xFF6666)gives the plot the medical paper look.addHorizontalReferenceLine(0.0f)marks the isoelectric baseline.
When to use this
- Adjust the vertical range when your signal is not in millivolts.
- Change
setDelay()and the phase increment together if you want a faster or slower sweep.
See also
examples/Cartesian/HeartbeatECG/HeartbeatECG.ino