I am trying to implement interrupts to run my servo for 1 minute after the button on the controller is pressed. But with my current sketch, the servo only moves if the button is continuously held down. I am not sure where I am going wrong.
#include <IRremote.h>
#include <Servo.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;
// Interval is how long we wait
// add const if this should never change
int interval=60000;
// Tracks the time since last event fired
unsigned long previousMillis=0;
void setup()
{
pinMode(RECV_PIN, INPUT);//define the interrupt pin as an input
Serial.begin(9600);
attachInterrupt(0,InterruptServiceRoutine,CHANGE);
}
void loop(){
// Get snapshot of time
unsigned long currentMillis = millis();
if (IREventFlag==1){
servo_move();
// How much time has passed, accounting for rollover with subtraction!
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
IREventFlag=0;//reset the flag for the next interrupt
previousMillis = currentMillis;// Use the snapshot to set track time until next event
}
}
}
void InterruptServiceRoutine(){
if(digitalRead(RECV_PIN)==HIGH){
digitalWrite(servoPin, HIGH);
IREventFlag=1;
}
if(digitalRead(RECV_PIN)==LOW){
digitalWrite(servoPin, LOW);
}
}
//runs 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);
}
}