本帖最后由 A书生 于 2015-1-15 15:18 编辑
zoologist 发表于 2015-1-14 14:51
计数脉冲可以用 pulseIn()
有一种用法是
int pin = 3;
int i;
int j;
int n=0;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(pin,INPUT);
j=digitalRead(pin);
}
void loop()
{
// put your main code here, to run repeatedly:
i=digitalRead(pin);
if(i != j)
{
n++;
j=i;
Serial.println(n);
}
}
我现在是这样写的,每次有电平的变化就会显示一次数字,就像数数一样,一个个的出现 ,我现在想每秒显示一次,然后清零,再重新数,就是显示出来每秒有多少次变化,该怎么做?谢谢你。
/*****检测一分钟内pin脚接收到低电平脉冲个数
****/
int pin = 3;
int n = 0;
double lasttime = 0;
double nowtime;
double time = 0;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(pin, INPUT);
digitalWrite(pin,HIGH);
}
void loop()
{
// put your main code here, to run repeatedly:
nowtime = millis();
if (nowtime - lasttime < 60000)
{
if (digitalRead(pin) == LOW & (millis() - time) < 150) //防抖动处理
{
n++;
}
time = millis();
}
else{
lasttime = millis();
Serial.println(n);
n = 0;
}
}
试试这个程序吧,在Arduino1.5.8编译通过。 |