如何多次计算峰值?

时间:2017-08-17 18:59:01

标签: arduino counting

我打开一个让流体流动的阀门。这里测量的压力是流体被拉入系统的压力。我试图测量前10个Pdiff(PMax-PMin)的平均值。计算平均值后,阀门关闭。

并且基于该平均值,阀门将再次打开和关闭1个峰值,然后是2个峰值,以及3个峰值,依此类推。我将压力值存储在一个数组中,并将该值与其前后值进行比较,得到最大值和最小值。

1 个答案:

答案 0 :(得分:1)

您使用++peakcounter增加您的峰值计数器,但随后在peakcounter=0

的if块中立即设置if(peakcounter==0)

由于你重置了峰值计数器,你永远不会达到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
   }