Using Interrupt with Motion Sensor

Hello,

I am using an Arduino Mega 2560 to create a situation where I have three sprayers, a motor, and an actuator running in a specific sequence. I would like the moving parts to stop if motion is detected within a certain distance of the product and then start back up where it left off when no motion is detected. I have the main sequence working correctly; but when I try to add the interrupt, I get a compile error that says the name of the interrupt function is “not declared in this scope.” From everything I have read, it does not look like I need to put the name of the interrupt function anywhere else but in the name of the function, i.e. “void AllStop” I am able to get the motion sensor to work by itself using a separate simple sketch, but when I try to add it to my product sketch I cannot get it to work. Do I need to add a certain library?

Could you please look at my code and tell me what I am missing? Thank you very much!

#include <IRremote.h>
#include<Servo.h>
int servoOnPin = 3;
int servoOffPin = 5;
int gateDown = 4;
int gateUp = 2;
int turfSprayer = 8;
int frontFlush = 10;
int backFlush = 12;
int recvPin = 14;
int MotionSensor = 18;
int servoOffPos = 100;
int servoOnPos = 110;
Servo myServoOn;
Servo myServoOff;
int turfMotorDelay = 14500;
int turfSprayDelay = 16000;
int frontFlushDelay = 5000;
int gateDelayMove = 4000;
int backFlushDelay = 6000;
int dt = 500;
int delaybetweenGateandMotor = 3000;
int delayTime = 2000;
int signal = 0;
volatile int PIRstate = LOW;
IRrecv irrecv(recvPin);
decode_results results;
IRsend irsend;
String msg1 = “Hello. AdvanTurf is waiting for signal.”;
String msg2 = “Gate is opening.”;
String msg3 = “Turf is turning and being cleaned.”;
String msg4 = “Gate is closing.”;
String msg5 = “System is flushing.”;
String msg6 = “Cycle is complete.”;
String msg7 = “Turf motor is stopping.”;
String msg8 = “Motion Detected - Cycle Interrupted”;

void setup() {

pinMode(gateUp, OUTPUT);
pinMode(gateDown, OUTPUT);
pinMode(turfSprayer, OUTPUT);
pinMode(frontFlush, OUTPUT);
pinMode(backFlush, OUTPUT);
pinMode(MotionSensor, INPUT_PULLUP);
myServoOn.attach(servoOnPin);
myServoOff.attach(servoOffPin);
irrecv.enableIRIn();
Serial.begin(9600);
attachInterrupt(5, AllStop, RISING); //using pin 18 which corresponds to 5 on Mega 2560

}
void loop() {
PIRstate = digitalRead(MotionSensor); //read motion sensor

if (PIRstate == LOW) {

Serial.println(msg1);

if (irrecv.decode(&results)) { //system starts with remote control button push
switch (results.value) {
case 0xFFA857: //establishes button on remote as blue+ button
delay(dt);

Serial.println(msg2);

digitalWrite(gateUp, HIGH); //gate open mode
digitalWrite(gateDown, LOW); //gate open mode
delay(delaybetweenGateandMotor); //time alloted for gate to open

Serial.println(msg3);
myServoOn.write(servoOnPos = 70);
delay(dt);
myServoOn.write(servoOnPos = 110); //turn motor on to turn turf
delay(gateDelayMove); //delay

digitalWrite(gateUp, HIGH);
digitalWrite(gateDown, HIGH); //stops gate

digitalWrite(turfSprayer, HIGH); //start turf sprayer
delay(turfMotorDelay);

Serial.println(msg7);
myServoOff.write(servoOffPos = 60);
delay(dt);
myServoOff.write(servoOffPos = 100); //motor stops
delay(dt);
digitalWrite(turfSprayer, LOW); //stop turf sprayer
delay(delayTime);
digitalWrite(frontFlush, HIGH); //start front flush sprayer
delay(dt);

Serial.println(msg4);
digitalWrite(gateUp, LOW); //close gate mode
digitalWrite(gateDown, HIGH); //close gate mode
delay(gateDelayMove);

digitalWrite(frontFlush, LOW); //turns front flush off
Serial.println(msg5);
delay(delayTime);
digitalWrite(backFlush, HIGH); //starts back flush
delay(backFlushDelay);
digitalWrite(backFlush, LOW); //stops back flush
Serial.println(msg6);
delay(delayTime);

digitalWrite(gateUp, LOW);
digitalWrite(gateDown, LOW);
break;
}
}

void AllStop() {
PIRstate=digitalRead(MotionSensor);

``if (PIRstate==HIGH){
Serial.println(msg10);
delayMicroseconds(50); // debounce
myServoOff.write(servoOffPos = 60);
delayMicroseconds(50);
myServoOff.write(servoOffPos = 100); //stops motor
delayMicroseconds(50);
digitalWrite(gateUp, LOW); //stops gate
digitalWrite(gateDown, LOW); //stops gate
digitalWrite(TurfSprayer, LOW); //stops turf sprayer
digitalWrite(FrontFlush, LOW); //stops front sprayer
digitalWrite(BackFlush, LOW); //stops back sprayer

}
}

irrecv.resume();

}
}

Match your { and your }.

It looks like your ISR is embedded in loop, which is not permitted.

Thank you! That was it. It compiled now. Thank you very much!