2017-11-14 10:10:49 [显示全部楼层]
4243浏览
查看: 4243|回复: 1

[求助] 求助 总是不定时触发中断,怎么解?附程序

[复制链接]
想用Arduino主板做一个求助器,功能,按一下按钮灯闪蜂鸣器闪,再按一次停止工作。
现在问题是一会儿能实现,一会儿又不能。不定时自动触发中断,有时还没碰到按钮他就自己触发中。
按钮是自弹式。哪位大侠指点下。
程序如下:
[mw_shl_code=cpp,true]int ledPin = 10;
    int button = 2;
    int beep=7;
    float sinVal;  
   volatile int state=LOW;           
int toneVal;
    void setup()
    {
            pinMode(ledPin, OUTPUT);
              pinMode(button, INPUT); //按钮设置为输入模式
               pinMode(beep, OUTPUT);  // 蜂鸣器引脚设置
             attachInterrupt(0,blink,RISING);
             Serial.begin(9600);
    }
void loop()
{
  for(int x=0;x<3;x++){
                   digitalWrite(ledPin,state);            //设置LED 为开
                   delay(150);                           //延时150毫秒
                   digitalWrite(ledPin,LOW);             //设置LED 为关
                   delay(100);                           //延时100毫秒
           }
  if(state==HIGH)
  {  for(int x=0; x<180; x++)
           {
                        //将sin函数角度转化为弧度
                        sinVal = (sin(x*(3.1412/180)));
                       //用sin函数值产生声音的频率
                       toneVal = 2000+(int(sinVal*1000));
                      //给引脚8一个
                       tone(beep, toneVal);
                       delay(2);
             }   
  }
             else
            {  noTone(beep);}

  }  
void blink()
{
  state=!state;

}[/mw_shl_code]

Rockets  NPC

发表于 2017-11-14 10:54:33

回帖奖励 +1 创造力

建议加一段按钮的去噪算法。
回复

使用道具 举报

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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail