|
|
想每隔一定时间统计一次震动次数C,用了震动传感器,我写的这个程序不对,求教应该怎么写
int x; //震动次数
int c; //统计次数
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2,INPUT);//用2号口 接受中断
x=0;
attachInterrupt(0,z,RISING);//每当2号口变化时,触发中断函数 Z
}
void z() //中断函数
{
if (digitalRead(2)==HIGH) //每当2号口输出变为0时,x值加1?
{
delay(50);
if (digitalRead(2)==HIGH) //消抖动
{
x++;
}
}
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long nowtime=millis(); //获取当前的系统运行时间长度
if(nowtime>10000);
{
c=x; //每10秒统计一次串口震动次数
Serial.print(c); //打印串口震动次数?
delay(1);
Serial.println();
while(digitalRead(2)==LOW)
{
delay(1);
}
}
} |
|