It's pretty unlikely, but I do note that you continue to increment even after you have taken that first count. This means that it will eventually roll over to 0 again … but I'm not sure how this might be causing your bug.
I'm a little stumped. My next step would be to look at the actual numbers and to track the internal logic of the program, because what's probably going on is that something weird happens when the data goes up - up - the same - down - the same - up again, or something like that.
AH HA!!!!!
What might be causing this is when there's a maximum or minimum in the data whose 'peak' is more than one sample long at the same detected value. Your code simply won't "see" it, and the normal business that you always see maxima and minima in turn won't apply.
Consider this sequence of values:
100
99
100 // minimum of 99 gets locked in
100
5 // code does not see this maximum, because sendata[0] == sendata[1]
100 // code does not see the minumum of 5, because the minimum of 99 is locked in
90 // code sees the maximum of 100, but it is within tolerance of the previous minimum 99
I suggest you throw away samples that are identical to the previous sample. That means that you don't shift the senval values down. Careful! You still need to close the solenoid on time, so the code that does that has to be outside the 'ignore duplicates' if().
static boolean no_data_yet = true;
if (no_data_yet)
{
senval[2] = pressure_mmhg; // changed to 2 because reasons
no_data_yet = false;
}
else
{
if(pressure_mmhg != senval[2]) {
senval[0] = senval[1];
senval[1] = senval[2];
senval[2] = pressure_mmhg;
if (!foundMax && (senval[1] > senval[0]) && (senval[1] > senval[2]))
{
foundMax = true;
max = senval[1];
Serial.print("max ");
Serial.println(max);
}
else if (!foundMin && (senval[1] < senval[0]) && (senval[1] < senval[2]))
{
foundMin = true;
min = senval[1];
Serial.print("min ");
Serial.println(min);
}
if (foundMax && foundMin)
{
foundMax = false;//enable finding of next max
foundMin = false;
Pdelta = max - min;
Serial.print("Pdelta ");
Serial.println(Pdelta);
if ( Pdelta >= 20)
{
digitalWrite(4, HIGH);
startTime = millis();
solstate = true;
}
}
}
}
//keep solenoid triggered for period of time 500ms
if (solstate == true && millis() - startTime >= interval)
{
digitalWrite(4, LOW);
solstate = false;
}
Oh - there's another problem
I'd also suggest that if you find a min, THEN fnd a max, throw away the min value but don't throw away the max. And likewise the other way around. I suggest this because weird-shaped oscillations might cause things that should be caught to be dropped.
Consider this sequence of values
1
5
4 // max of 5 detected
5 // min of 4 detected - solenoid not triggered. The max and min values are thrown away
100
99 // max of 100 detected, but the jump between min of 4 and max of 100 is not
To fix, keep a boolean variable named "the_most_recent_event_i_detected_was_a_max" and after the solenoid decision throw away the other value.
It's a tricky old thing, algorithms.