使用中断使伺服器保持运行设定的时间

发布于 2025-01-17 09:13:02 字数 1569 浏览 3 评论 0原文

我的目标是按下控制器上的按钮来激活伺服器 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦萦几度 2025-01-24 09:13:02

通过添加一些布尔值变量,我们可以确保在上升的边缘按钮上仅调用“ servo_move”功能(当按钮以前处于未经压力状态时,然后按下)。

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;
bool risingEdgeDetection = True;


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 && risingEdgeDetection == True){
    servo_move();
    IREventFlag=0;//reset the flag for the next interrupt
    risingEdgeDetection = False;
  }
}

void InterruptServiceRoutine(){
  if(digitalRead(RECV_PIN)==HIGH)   //when button is not pressed the output is HIGH
  {
    risingEdgeDetection = True;
    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);
}}

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).

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;
bool risingEdgeDetection = True;


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 && risingEdgeDetection == True){
    servo_move();
    IREventFlag=0;//reset the flag for the next interrupt
    risingEdgeDetection = False;
  }
}

void InterruptServiceRoutine(){
  if(digitalRead(RECV_PIN)==HIGH)   //when button is not pressed the output is HIGH
  {
    risingEdgeDetection = True;
    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);
}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文