unsigned long CountMeasureTimeTotal = 10000;
unsigned long MeasureTime = millis();
int PeakValueTrigger = 500;
int lastPeakValue;
uint8_t CountStarted = false;
int PeakCount;
unsigned long CountMeasureTime;
# define analogPin A2
void setup()
{
Serial.begin(115200);
Serial.println("hello there\n");
}
void loop()
{
int peakValue = analogRead( analogPin);
// Serial.print(" "); Serial.println(peakValue);
if ( (peakValue > PeakValueTrigger) && (lastPeakValue < PeakValueTrigger) && CountStarted == false )
{
PeakCount = 0; // ++; ??
CountStarted = true;
CountMeasureTime = millis();
lastPeakValue = peakValue;
}
if ( (peakValue >= PeakValueTrigger) && (lastPeakValue < PeakValueTrigger) && CountStarted )
{
if ( ((millis() - MeasureTime) < CountMeasureTimeTotal) && CountStarted ) {
PeakCount++;
Serial.println(PeakCount);
if (PeakCount >= 5) {
Serial.println(" 5 times in 10 seconds");
lastPeakValue = peakValue;
PeakCount = 0;
CountStarted = false;
}
//collect the measurements whiles the time has not expired
}
}
if ( ((millis() - MeasureTime) >= CountMeasureTimeTotal) && CountStarted )
{
// stop the count when measurement time expires
CountStarted = false;
}
lastPeakValue = peakValue;
delay(50); // relly should do this properly
}