2017-08-17 107 views
-1

我打開一個閥門讓流體流動。這裏測量的壓力是流體被拉入系統的壓力。我正在嘗試僅測量前10個Pdiff(PMax-PMin)的平均值。一旦計算出平均值,閥就關閉。如何多次計數峯值?

並根據這個平均值,閥門將再次打開和關閉一個峯值,然後爲2個峯值和3個峯值等。我將壓力值存儲在數組中,並將該值與其前後值進行比較,得到最大值和最小值。

+0

您能否提供一個使用開關盒的例子 – Naley

回答

1

您使用++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 
    } 
+0

如何糾正代碼,以便我計算一個峯值,重置它,然後計算2個峯值,重置它,計算3個峯值,等等...... – Naley

+0

我已經爲你編輯了答案。我強烈建議您瞭解功能,以便簡化代碼。函數會使你的代碼更加可讀和乾淨 –

+0

我試着將peakcounter與變量按照你的建議進行比較,它仍然不起作用。我正在計數峯值,並根據計數值打開閥門。首先我計算1個峯值,並打開閥門一段時間並關閉它。然後我計算2個峯值,打開閥門一段時間,關閉它,然後計算3個峯值等。 – Naley