john969 发表于 2015-9-16 13:01:04

求教计数程序,请老师指点下

想每隔一定时间统计一次震动次数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);
                        }
                     }
               }

Super169 发表于 2015-9-16 16:10:12

Inside the attached function, delay() won't work and the value returned by millis() will not increment.

john969 发表于 2015-9-18 08:53:31

求教怎么改,能让每10秒钟统计一次震动次数
页: [1]
查看完整版本: 求教计数程序,请老师指点下