'Using Interrupts to keep the servo running for a set time

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);
}}


Solution 1:[1]

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);
}}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VonSquiggs