Skip to content

Commit

Permalink
Updated peak detection example
Browse files Browse the repository at this point in the history
  • Loading branch information
tigoe committed Sep 9, 2014
1 parent 30e6035 commit 1621277
Showing 1 changed file with 19 additions and 39 deletions.
58 changes: 19 additions & 39 deletions PeakDetection/PeakDetection.ino
Original file line number Diff line number Diff line change
@@ -1,59 +1,39 @@
/*
Peak Finder
This example finds the peak value of an analog sensor over time.
This example finds the peak value of an analog sensor over time.
It assumes the sensor is moving in a simple curve.
The program checks to see that the current value is above a given threshold,
then checks to see if the value is greater than the previous value. if so,
then it saves the current value as a peak. When the current value goes below
The program checks to see that the current value is above a given threshold,
then checks to see if the value is greater than the previous value. if so,
then it saves the current value as a peak. When the current value goes below
the threshold again, it outputs the last peak value recorded.
This works only with sensors that settle at zero or below. Accelerometers
and other sensors that settle in the middle of their range require a
more complex solution.
created 12 Sept. 2005
modified 20 March 2012
modified 9 Sept. 2014
by Tom Igoe
*/

int peakValue = 0;
int sensorValue = 0;
int lastSensorValue = 0;
int threshold = 50; //set your own value based on your sensors
int noise = 5; //set a noise value based on your particular sensor
int threshold = 50; //set your own value based on your sensors
int noise = 5; //set a noise value based on your particular sensor

void setup() {
Serial.begin(9600);
}

void loop() {
//read sensor on pin A0:
sensorValue = analogRead(A0);

//check to see that it's above the threshold:
if ( sensorValue >= threshold + noise ) {
//if it's greater than the last eading,
// then make it our current peak:
if ( sensorValue >= lastSensorValue + noise ) {
peakValue = sensorValue;
}
//if the sensorValue is not above the threshold,
// then the last peak value you got would be the actual peak:
}
else {
if ( peakValue >= threshold ) {
//this is the final peak value; take action
Serial.print("peak reading: ");
int sensorValue = analogRead(A0);
// check if it's higher than the current peak:
if (sensorValue > peakValue) {
peakValue = sensorValue;
}
if (sensorValue <= threshold - noise ) {
if (peakValue > threshold + noise) {
Serial.println(peakValue);
peakValue = 0;
}

//reset peakValue, since youyou've finished with this peak:
peakValue = 0;
}

//store the current sensor value for the next loop:
lastSensorValue = sensorValue;
}

0 comments on commit 1621277

Please sign in to comment.