按钮和按钮中断按下时间超过3秒

时间:2013-09-18 22:30:56

标签: c arduino

在我的项目中,我有三个按钮,每个按钮触发一个功能。

这个功能必须做两件事,按下按钮时一个动作(实际工作),但我想添加第二个功能,如果按下按钮超过3秒,它会做一些事情,比如调用函数。

到目前为止,我正在初步确定中断:

attachInterrupt(0, footOne, Falling);

,功能是:

void footOne(){
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// Debounce
if (interrupt_time - last_interrupt_time > 200){
    //Do things
    if(debug==1){Serial.println("Button 1 pressed!");}
}

last_interrupt_time = interrupt_time;
}

现在我想知道如何更改功能以增加按下按钮超过3秒的可能性......

记住从中断调用此函数。

谢谢!

1 个答案:

答案 0 :(得分:1)

通过现在构建程序的方式,您需要一个外部RTC来执行此任务。一旦启动中断,arduino上的内部时钟就会受到影响,因此在离开中断之前无法可靠地使用。您的实现对时间敏感,是否需要中断?因为如果不是这样,轮询按钮的状态然后比较经过的时间将是一个非常简单的实现。但是,如果你想坚持使用中断,我会看看volatile变量作为主循环和中断函数之间的对话方式。

相关问题