I have a pressure sensor from which am able to get max and min values and the difference (pdiff = max -min) .
I want to calculate the average of first 10 values of pdiff. And I want to calculate the average only once.( just for the first 10 values )
Based on the average value of pdiff, I want to open a valve once.
And then for 2 values of pdiff , the valve opens and 3 values and so on...
I have attached a picture for better understanding.
This is for counting one peak.
How do I count 2 peak and open the valve and then 3 peaks and so on?
I want to open the valve for a certain time when I have 2 peaks equal to or greater than the average value And then close it. And then open again when I have 3 peaks and close it and 4 peaks and so on...
I attached a picture which sort of gives a clear understanding of what am trying to do.
Also, the average would be calculated again and reset after 10 peak values .
I plan to open the valve for 1 peak, close it and then open for 2 peaks and close it and open for 3 peaks and close it , 4 peaks and close it. When I close it after the 4 peaks, the average would have been calculated again (4+3+2+1) and reset average value .
How do I get to calculate the average only for the first 10 peaks ?
Why do you want to assign the value true to average? average is a float. It doesn't make sense to compare a float to true, even if you use the correct operator.
Your handling of peakCounter and the resetting to 0 looks wrong, but more fundamentally, the value of peakCounter, has nothing to do with the peaks you measure using the Pval and determining max and min to define a pressure cycle.
Looking at the code in reply #10, you do indeed continue to take pressure values and find max and min after the average is determined but it doesn't look like you don't do anything with them.
You want to be counting the actual peaks with peakCounter and making your decisions on the valve based on those counts.