【发布时间】:2016-03-07 09:57:45
【问题描述】:
目前正在开展使用 arduino UNO 和伺服电机打开带有访问代码的门的项目。正常操作需要使用工作正常的键盘输入访问代码。另一种选择需要按下一个按钮,该按钮会导致伺服电机旋转中断。我的问题是我的中断只工作一次,再也不会工作。加上我如何放置for循环以延迟旋转中断函数内的伺服电机。我知道这是不可能的,但我正在调用另一个具有 delayMicroseconds 的函数,但这一切都不起作用。下面是我的实现,请帮忙
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Servo.h>
Servo servo;
const int openButtonPin = 2;
void setup() {
// put your setup code here, to run once:
servo.attach(5);
pinMode(openButtonPin, INPUT); //Pin 2 is input
attachInterrupt(0, enforceOpenAccess, HIGH); // PIN 2
}
void(* resetFunc)(void) = 0;
void loop()
{
//My other keypad implementations go here
}
void myDelay(int x) // function to cause delay in the interrupt
{
for(int i = 0; i<x; i++)
{
delayMicroseconds(1000);
}
}
void enforceOpenAccess() // ISR
{
for(int k =0; k<=180; k+=2)
{
servo.write(k); //rotate the servo
myDelay(30); //delay the rotation of the servo
}
}
上面的代码是在 proteus 中模拟的 arduino UNO 上运行的,中断按钮是一个按钮。如果有其他实现方式但与我上面描述的行为相同,请提供帮助。非常感谢
【问题讨论】:
-
为什么服务例程在按下按钮时只运行一次而不再运行
-
在中断处理程序中做需要很长时间的工作是不好的。我认为你应该让你的中断处理程序只提高一个标志,
loop()应该轮询标志并在标志被提高时执行工作。
标签: arduino