汽车计数器arduino程序,如何在汽车停在传感器上时停止计数。

时间:2018-03-11 16:44:36

标签: arduino embedded

我正在使用压力传感器(mpx5010gp)和连接到arduino板的塑料管来计算经过它的汽车数量。每当传感器值超过阈值或平均值时,动态计算,当汽车经过管道时,计数器会上升一个。我面临的问题是,如果某辆车停在管道上,计数器会继续增加。

这是计算的完成方式:

  void loop() {
  // put your main code here, to run repeatedly:

  sensorValue = analogRead(sensorPin);

  if(millis()<2000)
  {
      averageValue +=((float)sensorValue-averageValue)/10.f;  
  }else 
  {
    if(sensorValue>averageValue +4){
      count++;
      Serial.println(count);
      lcd.setCursor(0,1);
      lcd.print(count);
      averageValue =sensorValue+10;
      delay(2000);
    }
    else 
    {
        averageValue +=((float)sensorValue-averageValue)/100.f; 
    }     
  }
}

2 个答案:

答案 0 :(得分:3)

有两个带滞后的阈值,因此当超过高阈值时增加计数,并且不会进一步增加计数,直到水平低于下限阈值。

void setup()
{
    active = false ;
}

void loop()
{
    sensorValue = analogRead(sensorPin);
    if( sensorValue > high_threshold && !active )
    {
        count++ ;
        active = true ;
    }

    if( sensorValue < low_threshold )
    {
        active = false ;
    }
}

答案 1 :(得分:2)

仅计算上升沿(即从低到高的过渡)

顺便说一句,你对价值观的处理几乎是不可理解的。有时除以10.0。其他100次。等待2秒,在这里加4,在那里加10。

如果我对你的代码进行评分,我最多只能给它一个合格的分数。