极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9932|回复: 2

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

[复制链接]
发表于 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);
                          }
                     }
                 }
回复

使用道具 举报

发表于 2015-9-16 16:10:12 | 显示全部楼层
Inside the attached function, delay() won't work and the value returned by millis() will not increment.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-9-18 08:53:31 | 显示全部楼层
求教怎么改,能让每10秒钟统计一次震动次数
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 21:48 , Processed in 0.051043 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表