我打開一個閥門讓流體流動。這裏測量的壓力是流體被拉入系統的壓力。我正在嘗試僅測量前10個Pdiff(PMax-PMin)的平均值。一旦計算出平均值,閥就關閉。如何多次計數峯值?
並根據這個平均值,閥門將再次打開和關閉一個峯值,然後爲2個峯值和3個峯值等。我將壓力值存儲在數組中,並將該值與其前後值進行比較,得到最大值和最小值。
我打開一個閥門讓流體流動。這裏測量的壓力是流體被拉入系統的壓力。我正在嘗試僅測量前10個Pdiff(PMax-PMin)的平均值。一旦計算出平均值,閥就關閉。如何多次計數峯值?
並根據這個平均值,閥門將再次打開和關閉一個峯值,然後爲2個峯值和3個峯值等。我將壓力值存儲在數組中,並將該值與其前後值進行比較,得到最大值和最小值。
您使用++peakcounter
增加您的peakcounter但隨後立即在if(peakcounter==0)
if塊設置peakcounter=0
既然您重置peakcounter,你永遠不會peakcounter == 2
if (valstate == false && Pdelta >= average)
{
{
++peakcounter; // keeps the count of how many times the value has gone
above average
}
// Checks for the number of times and then performs action
if (peakcounter == 1) {
digitalWrite(4, HIGH);
startTime = millis();
valstate = true;
peakcounter = 0; //the offending line
}
你需要什麼(注意:代碼沒有優化,我不完全理解你需要什麼,但是這應該解決你寫的問題)
int currentMax = 0;
// your code here....
if (valstate == false && Pdelta >= average){
++peakcounter;
if(peakcounter > currentMax){
// Checks for the number of times and then performs action
if (peakcounter == 1) {
digitalWrite(4, HIGH);
startTime = millis();
valstate = true;
peakcounter = 0;
currentMax++;
}
//the rest of your peakcount checking code here
}
您能否提供一個使用開關盒的例子 – Naley