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 threshold again, it outputs the last peak value recorded.
Wiring/Arduino:
/*
Peak Finder
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 threshold again, it outputs the last peak value recorded.
created 12 Sept. 2005
modified 9 Nov. 2006
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
void setup() {
Serial.begin(9600);
}
void main() {
//read sensor on pin RA0:
sensorValue = analogRead(0);
//check to see that it's above the threshold:
if ( sensorValue >= threshold + noise ) {
//if it's greater than the last reading,
// 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 we got would be the actual peak:
}
else {
if ( peakValue >= threshold ) {
//this is the final peak value; take action
Serial.print("peak reading: ");
Serial.print(peakValue, DEC);
}
//reset peakValue, since we've finished with this peak:
peakValue = 0;
}
//store the current sensor value for the next loop:
lastsensorValue = sensorValue;
}
PicBasic Pro:
' Define ADCIN parameters
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 20 ' Set sampling time in uS
PeakValue var word
SensorValue var word
LastSensorValue var word
Threshold var word
Noise var word
' serial pins and data reate:
tx var portc.6
rx var portc.7
n9600 con 16468
Threshold = 50 ' set your own value based on your sensors
PeakValue = 0 ' initialize peakValue
noise = 5 ' set a noise value based on your particular sensor
' Set PORTA to all input
TRISA = %11111111
' Set up ADCON1
ADCON1 = %10000010
Main:
' read sensor on pin RA0:
ADCin 0, sensorValue
' check to see that it's above the threshold:
If sensorValue >= threshold + noise then
' if it's greater than the last reading,
' then make it our current peak:
If sensorValue >= lastSensorValue + Noise then
PeakValue = sensorValue
endif
' if the sensorValue is not above the threshold,
' then the last peak value we got would be the actual peak:
Else
If peakValue >= threshold then
' this is the final peak value; take action
serout2 tx, n9600, ["peak reading", DEC peakValue, 13,10]
endif
' reset peakValue, since we've finished with this peak:
peakValue = 0
Endif
' store the current sensor value for the next loop:
lastSensorValue = sensorValue
Goto main