arduino通过伺服电机中断
目前,我正在使用arduino UNO和伺服电机打开带有访问代码的门。正常操作需要使用工作正常的键盘输入访问码。另一个选项需要按下一个按钮,该按钮导致旋转伺服电机的中断。我的问题是我的中断只起作用一次,再也不起作用了。另外,我如何将for循环设置为在中断功能内延迟旋转伺服电机。我知道这是不可能的,但我正在调用另一个具有延迟微秒的函数,但所有这些都不起作用。下面是我的实现,请帮忙arduino通过伺服电机中断,arduino,Arduino,目前,我正在使用arduino UNO和伺服电机打开带有访问代码的门。正常操作需要使用工作正常的键盘输入访问码。另一个选项需要按下一个按钮,该按钮导致旋转伺服电机的中断。我的问题是我的中断只起作用一次,再也不起作用了。另外,我如何将for循环设置为在中断功能内延迟旋转伺服电机。我知道这是不可能的,但我正在调用另一个具有延迟微秒的函数,但所有这些都不起作用。下面是我的实现,请帮忙 #include <Keypad.h> #include <LiquidCrystal.h>
#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
}
}
#包括
#包括
#包括
伺服;
常量int openButtonPin=2;
无效设置(){
//将安装代码放在此处,以便运行一次:
伺服。连接(5);
pinMode(openButtonPin,输入);//输入引脚2
attachInterrupt(0,强制OpenAccess,高);//引脚2
}
无效(*resetFunc)(无效)=0;
void循环()
{
//我的其他键盘实现在这里
}
void myDelay(int x)//导致中断延迟的函数
{
对于(inti=0;i来说,您发布的代码片段中有几个问题。为了完整起见,您应该发布循环函数,因为我们无法猜测您在其中编写了什么
只有一条评论:你放了一个pullup吗?否则使用INPUT\u pullup而不是INPUT作为按钮模式
主要的一点是,您为高模式附加了中断,这将在管脚上升时触发中断,而不是在上升沿。请使用宏数字管脚INTERRUP
映射到正确的管脚:
attachInterrupt(digitalPinToInterrupt(openButtonPin), enforceOpenAccess, RISING);
然后..让我们改进代码。只有在必须立即响应输入(=少于几毫秒)的情况下,您才应该在严格必要时使用中断。在这里,您不必这样做,因此最好检查循环中的按钮(更多关于以下转动电机)
这也将使您能够正确地解除按钮的抖动:
#include <Bounce2.h>
Bounce debouncer = Bounce();
void setup()
{
...
pinMode(openButtonPin, INPUT); //Pin 2 is input
debouncer.attach(openButtonPin);
debouncer.interval(5); // interval in ms
}
void loop()
{
debouncer.update();
if (debouncer.rose())
{
// Start turning the motor
}
...
}
这样,即使按下按钮,也不会阻塞主循环这就是我的循环()
char key=keypad.getKey();
如果(关键)
{
如果(j<10)
{
学生编号[j]=钥匙;
//holdMaskedNumber[j]='*';
lcd.setCursor(0,2);
打印(字符串(学生编号));
如果(j==9)
{
学生编号[9]='\0';
//holdMaskedNumber[9]=0;
lcd.clear();
//字符串编号=字符串(studentNumber);
//lcd.打印(数字);
//延迟(1000);
//lcd.clear();
lcd.打印(“访问代码”);
}
j++;
}
其他的
{
如果(i<5)
{
accessCode[i]=密钥;
holdMaskedCode[i]='*';
lcd.setCursor(1,2);
打印(字符串(holdMaskedCode));
如果(i==4)
{
holdMaskedCode[5]='\0';
accessCode[5]='\0';
//lcd.clear();
//lcd.setCursor(0,0);
//accessCodeString=字符串(accessCode);
//打印(accessCodeString);
//延迟(1000);
lcd.clear();
对于(int i=0;i)以及为什么当按下按钮时服务例程只运行一次,并且从不再次运行中断处理程序中花费很长时间的工作是不好的。我认为您应该让中断处理程序只触发一个标志,并且loop()
应该轮询该标志,如果触发该标志,则执行该工作。这就是我的循环中的内容()你的循环中有什么?我刚刚发布了它,再次检查我的答案,现在考虑你的答案,似乎我应该做的更好的方式是轮询而不是中断,是吗?通常你应该编辑你的问题而不是写答案来添加数据。无论如何,是的,首选的方式是轮询,因为它们是低优先级的任务。我的p参考的解决方案是第二个(即使用Bounce2
类来实现去抖动),非常感谢,这确实很有帮助,现在让我开始工作并更改代码
#include <Bounce2.h>
Bounce debouncer = Bounce();
void setup()
{
...
pinMode(openButtonPin, INPUT); //Pin 2 is input
debouncer.attach(openButtonPin);
debouncer.interval(5); // interval in ms
}
void loop()
{
debouncer.update();
if (debouncer.rose())
{
// Start turning the motor
}
...
}
#include <Bounce2.h>
Bounce debouncer = Bounce();
void setup()
{
...
pinMode(openButtonPin, INPUT);
attachInterrupt(digitalPinToInterrupt(openButtonPin), enforceOpenAccess, RISING);
}
void loop()
{
...
}
void enforceOpenAccess() // ISR
{
// Start turning the motor
}
unsigned long lastServoTime;
uint8_t servoPosition = 255;
const int timeBetweenSteps_in_ms = 30;
void loop()
{
...
if (servoPosition <= 180)
{ // servo should move
if ((millis() - lastServoTime) >= timeBetweenSteps_in_ms)
{
lastServoTime += timeBetweenSteps_in_ms;
servoPosition++;
if (servoPosition <= 180)
servo.write(servoPosition);
}
}
}
lastServoTime = millis();
servoPosition = 0;
servo.write(servoPosition);
char key = keypad.getKey();
if(key)
{
if(j < 10)
{
studentNumber[j] = key;
//holdMaskedNumber[j] = '*';
lcd.setCursor(0,2);
lcd.print(String(studentNumber));
if(j == 9)
{
studentNumber[9] = '\0';
//holdMaskedNumber[9] = 0;
lcd.clear();
//String number = String(studentNumber);
//lcd.print(number);
//delay(1000);
//lcd.clear();
lcd.print("Access Code");
}
j++;
}
else
{
if(i < 5)
{
accessCode[i] = key;
holdMaskedCode[i] = '*';
lcd.setCursor(1,2);
lcd.print(String(holdMaskedCode));
if(i == 4)
{
holdMaskedCode[5] = '\0';
accessCode[5] = '\0';
//lcd.clear();
//lcd.setCursor(0,0);
//accessCodeString = String(accessCode);
//lcd.print(accessCodeString);
//delay(1000);
lcd.clear();
for(int i =0; i<6; i++)
{
lcd.print("Please wait.");
delay(500);
lcd.clear();
lcd.print("Please wait..");
delay(500);
lcd.clear();
lcd.print("Please wait...");
delay(500);
lcd.clear();
}
digitalWrite(4, HIGH);
lcd.print("Access Granted");
for(int k =0; k<=180; k+=2)
{
servo.write(k);
delay(30);
}
resetFunc();
}
i++;
}
}
}