2017-08-17 104 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