使用中断使伺服器保持运行设定的时间
我的目标是按下控制器上的按钮来激活伺服器 1 分钟。我希望伺服器以 1 为增量从 0 到 20。按下控制器上的按钮后,我希望伺服器重复此循环 1 分钟。这是我到目前为止的代码。但目前,只要按钮保持按下状态,伺服器就会移动。
This makes the servo move as long as the button remains pressed, but I am trying to have the servo activate and run for a minute once the button is pressed
*******************************************/
#include <IRremote.h>
#include <Servo.h>
#include <TimerOne.h>
#define interruptNumber 0
#define RECV_PIN 2
#define button1 0xFF30CF
#define servoPin 10
Servo myservo;
IRrecv irrecv(RECV_PIN);
decode_results results;
volatile byte IREventFlag;
void setup()
{
pinMode(RECV_PIN, INPUT);//define the interrupt pin as an input
Timer1.initialize(100);
Serial.begin(9600);
Timer1.attachInterrupt(InterruptServiceRoutine);
}
void loop(){
if (IREventFlag==1){
servo_move();
IREventFlag=0;//reset the flag for the next interrupt
}
}
void InterruptServiceRoutine(){
if(digitalRead(RECV_PIN)==HIGH) //when button is not pressed the output is HIGH
{
digitalWrite(servoPin, LOW);
}
if(digitalRead(RECV_PIN)==LOW) //presseing button causes output to go LOW
{
digitalWrite(servoPin, HIGH);
IREventFlag=1;//set the flag to tell the main loop that a touch event occurred.
}
}
//runs servo for 1 min and goes between 0 and 20 deg in increments of 1
void servo_move(){
for (int i = 0; i<20; i++){
myservo.write(i);
delay(10);
}
for(int i = 20; i>0; i--){
myservo.write(i);
delay(10);
}}
My goal is to press a button on the controller to activate the servo for 1 minute. I want the servo to go from 0 to 20 in increments of 1. Upon pressing the button on the controller, I want this cycle for the servo to repeat for 1 minute. This is the code I have so far. But for right now this makes the servo move as long as the button remains pressed.
This makes the servo move as long as the button remains pressed, but I am trying to have the servo activate and run for a minute once the button is pressed
*******************************************/
#include <IRremote.h>
#include <Servo.h>
#include <TimerOne.h>
#define interruptNumber 0
#define RECV_PIN 2
#define button1 0xFF30CF
#define servoPin 10
Servo myservo;
IRrecv irrecv(RECV_PIN);
decode_results results;
volatile byte IREventFlag;
void setup()
{
pinMode(RECV_PIN, INPUT);//define the interrupt pin as an input
Timer1.initialize(100);
Serial.begin(9600);
Timer1.attachInterrupt(InterruptServiceRoutine);
}
void loop(){
if (IREventFlag==1){
servo_move();
IREventFlag=0;//reset the flag for the next interrupt
}
}
void InterruptServiceRoutine(){
if(digitalRead(RECV_PIN)==HIGH) //when button is not pressed the output is HIGH
{
digitalWrite(servoPin, LOW);
}
if(digitalRead(RECV_PIN)==LOW) //presseing button causes output to go LOW
{
digitalWrite(servoPin, HIGH);
IREventFlag=1;//set the flag to tell the main loop that a touch event occurred.
}
}
//runs servo for 1 min and goes between 0 and 20 deg in increments of 1
void servo_move(){
for (int i = 0; i<20; i++){
myservo.write(i);
delay(10);
}
for(int i = 20; i>0; i--){
myservo.write(i);
delay(10);
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过添加一些布尔值变量,我们可以确保在上升的边缘按钮上仅调用“ servo_move”功能(当按钮以前处于未经压力状态时,然后按下)。
With the addition of some boolean variable, we can make sure to only call the 'servo_move' function on a rising edge button press (when the button was previously in an unpressed state and is then pressed).