Reject one isolated Arduino sensor spike with a median
Consider three consecutive readings of 490, 900, and 510.
0 of 3 complete · Progress saved on this device
Work out the example
Consider three consecutive readings of 490, 900, and 510. Sort the values and take the middle one. A single large spike has less influence on this estimate than on an arithmetic mean.
Decide what to check next
Use a fresh window for the comparison, then test a sustained step. After enough readings change, the median moves too. Two outliers in a three-sample window can still dominate. Keep the raw samples when investigating real faults.
Compare with the serial result
It prints the middle sample, 510.
Open the exact Uno example
It prints the middle sample, 510.
void setup() {
Serial.begin(9600);
int a=490, b=900, c=510;
if (a>b) { int tmp=a; a=b; b=tmp; }
if (b>c) { int tmp=b; b=c; c=tmp; }
if (a>b) { int tmp=a; a=b; b=tmp; }
Serial.println(b);
}
void loop() {}Verified with [email protected] on Uno R3 (2026-12-05 UTC). This check did not use physical hardware.