|
|
做了一个由超声波SR和NANO组成的人体计数器;
组要实现人体通过时计数,设想当探测距离小于一定值时计数。
写了如下程序,但发现会重复计数。
Arduino有能对上升沿/或下降沿计数的机制吗?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int TrigPin = 2;
const int EchoPin = 3;
const int ifred=4;
float cm;
float cm2;
float cm3;
int count;
int Filter_Value2;
int Filter_Value3;
int value;
LiquidCrystal_I2C lcd(0x27,19,3); // set the LCD address to 0x27 for a 20 chars and 4 line display
void setup()
{
lcd.init(); // initialize the lcd
delay(10);
lcd.backlight(); //Open the backlight
lcd.clear();
delay(20);
value=40;
count=0;
pinMode(6, INPUT);//define pin 6 as input
lcd.setCursor(0,2); // Cow 2 line 0,
lcd.print("The Number is:"); // Print a message to the LCD.
}
void loop()
{
int n =digitalRead(6); //创建一个变量n,将4号数字口的状态采集出来赋值给他。
if (n==HIGH) //判断n是否为高电平,如果是执行下面的语句,不是则跳过。
{
lcd.clear();
delay(100);
count=0;
}
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm2 = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
Filter_Value2 = Filter(cm2); // 获得滤波器输出值
cm2 = (int(Filter_Value2 * 100.0)) / 100.0; //保留两位小数
if(cm2<0)
{cm2=0;}
if (cm2 < 50)
{
delay(150);
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm3 = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
Filter_Value3 = Filter(cm3); // 获得滤波器输出值
cm3 = (int(Filter_Value3 * 100.0)) / 100.0; //保留两位小数
if(cm3<0)
{cm3=0;}
if((cm3-cm2)>30)
{
count=count+1;
tone(4,400,500) ;
}
}
lcd.setCursor(0,0); // Cow 2 line 0,
lcd.print("The Distance is:"); // Print a message to the LCD.
lcd.setCursor(0,1); // Cow 2 line 0,
lcd.print(cm2); // Print a message to the LCD.
lcd.setCursor(8,1); // Cow 2 line 0,
lcd.print(cm3); // Print a message to the LCD.
lcd.setCursor(0,2); // Cow 2 line 0,
lcd.print("The Number is:"); // Print a message to the LCD.
lcd.setCursor(2,3); // Cow 2 line 0,
lcd.print(count); // Print a message to the LCD.
// Serial print //
/*
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
*/
}
// 限幅滤波法(又称程序判断滤波法)
#define FILTER_A 20
int Filter( int dist)
{
int Newdist;
//Newdist = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm;
Newdist=2547.8/((float)cm*0.49-10.41)-0.42;
if(((Newdist - dist) > FILTER_A) || ((dist - Newdist) > FILTER_A))
return dist;
else
return Newdist;
} |
|