|
1 | 1 | /* |
2 | 2 | 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. |
5 | 5 | 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 |
10 | 10 | 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 | +
|
16 | 12 | created 12 Sept. 2005 |
17 | | - modified 20 March 2012 |
| 13 | + modified 9 Sept. 2014 |
18 | 14 | by Tom Igoe |
19 | | - |
| 15 | +
|
20 | 16 | */ |
21 | 17 |
|
22 | 18 | 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 |
27 | 21 |
|
28 | 22 | void setup() { |
29 | 23 | Serial.begin(9600); |
30 | 24 | } |
31 | 25 |
|
32 | 26 | void loop() { |
33 | 27 | //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) { |
50 | 35 | Serial.println(peakValue); |
| 36 | + peakValue = 0; |
51 | 37 | } |
52 | | - |
53 | | - //reset peakValue, since youyou've finished with this peak: |
54 | | - peakValue = 0; |
55 | 38 | } |
56 | | - |
57 | | - //store the current sensor value for the next loop: |
58 | | - lastSensorValue = sensorValue; |
59 | 39 | } |
0 commit comments