// Pulse Monitor Test Script
float reads[samp_siz], sum;
float last, reader, start;
float first, second, third, before, print_value;
for (int i = 0; i < samp_siz; i++)
// calculate an average of the sensor
// during a 20 ms period (this will eliminate
// the 50 Hz noise caused by electric light
reader += analogRead (sensorPin);
while (now < start + 20);
reader /= n; // we got an average
// Add the newest measurement to an array
// and subtract the oldest measurement from the array
// to maintain a sum of last measurements
// now last holds the average of the values in the array
// check for a rising curve (= a heart beat)
if (!rising && rise_count > rise_threshold)
// Ok, we have detected a rising curve, which implies a heartbeat.
// Record the time since last beat, keep track of the two previous
// times (first, second, third) to get a weighed average.
// The rising flag prevents us from detecting the same rise more than once.
first = millis() - last_beat;
// Calculate the weighed average of heartbeat rate
// according to the three last beats
print_value = 60000. / (0.4 * first + 0.3 * second + 0.3 * third);
Serial.print(print_value);
// Ok, the curve is falling