Skip to content

Commit 1621277

Browse files
committed
Updated peak detection example
1 parent 30e6035 commit 1621277

1 file changed

Lines changed: 19 additions & 39 deletions

File tree

PeakDetection/PeakDetection.ino

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,39 @@
11
/*
22
Peak Finder
3-
4-
This example finds the peak value of an analog sensor over time.
3+
4+
This example finds the peak value of an analog sensor over time.
55
It assumes the sensor is moving in a simple curve.
6-
7-
The program checks to see that the current value is above a given threshold,
8-
then checks to see if the value is greater than the previous value. if so,
9-
then it saves the current value as a peak. When the current value goes below
6+
7+
The program checks to see that the current value is above a given threshold,
8+
then checks to see if the value is greater than the previous value. if so,
9+
then it saves the current value as a peak. When the current value goes below
1010
the threshold again, it outputs the last peak value recorded.
11-
12-
This works only with sensors that settle at zero or below. Accelerometers
13-
and other sensors that settle in the middle of their range require a
14-
more complex solution.
15-
11+
1612
created 12 Sept. 2005
17-
modified 20 March 2012
13+
modified 9 Sept. 2014
1814
by Tom Igoe
19-
15+
2016
*/
2117

2218
int peakValue = 0;
23-
int sensorValue = 0;
24-
int lastSensorValue = 0;
25-
int threshold = 50; //set your own value based on your sensors
26-
int noise = 5; //set a noise value based on your particular sensor
19+
int threshold = 50; //set your own value based on your sensors
20+
int noise = 5; //set a noise value based on your particular sensor
2721

2822
void setup() {
2923
Serial.begin(9600);
3024
}
3125

3226
void loop() {
3327
//read sensor on pin A0:
34-
sensorValue = analogRead(A0);
35-
36-
//check to see that it's above the threshold:
37-
if ( sensorValue >= threshold + noise ) {
38-
//if it's greater than the last eading,
39-
// then make it our current peak:
40-
if ( sensorValue >= lastSensorValue + noise ) {
41-
peakValue = sensorValue;
42-
}
43-
//if the sensorValue is not above the threshold,
44-
// then the last peak value you got would be the actual peak:
45-
}
46-
else {
47-
if ( peakValue >= threshold ) {
48-
//this is the final peak value; take action
49-
Serial.print("peak reading: ");
28+
int sensorValue = analogRead(A0);
29+
// check if it's higher than the current peak:
30+
if (sensorValue > peakValue) {
31+
peakValue = sensorValue;
32+
}
33+
if (sensorValue <= threshold - noise ) {
34+
if (peakValue > threshold + noise) {
5035
Serial.println(peakValue);
36+
peakValue = 0;
5137
}
52-
53-
//reset peakValue, since youyou've finished with this peak:
54-
peakValue = 0;
5538
}
56-
57-
//store the current sensor value for the next loop:
58-
lastSensorValue = sensorValue;
5939
}

0 commit comments

Comments
 (0)