Servo in ISR - Weird Behavior

I'm new to Arduino in C. I've done a decent amount in assembly in the last few months. I'm struggling to understand why the following code doesn't work.

If I just move this bit of code into the loop() it works fine. When I move it to an ISR that I would like to trigger through an external device the servo wigs out and just sits there and "buzzes" and doesn't move. If I just toggle a light in the same ISR with the same pins it works just fine. What is different about servos and ISRs that would cause this to not work.

(Note this is a smaller piece of a larger project)

#include <Servo.h>

//Create Servo Object
Servo servo;
volatile int angle = 0;
void setup() {
pinMode(13, OUTPUT);
pinMode(21, INPUT);
attachInterrupt(digitalPinToInterrupt(21), ServoMove, FALLING);
servo.attach(8);
servo.write(angle);
Serial.begin(9600);
}

void loop() {
}

ServoMove{
for(angle = 0; angle < 180; angle++)
{
servo.write(angle);
delay(5);
}
// now scan back from 180 to 0 degrees
for(angle = 180; angle > 0; angle--)
{
servo.write(angle);
delay(45);
}
}

Bhysjulien:
I'm struggling to understand why the following code doesn't work.

Study Gammon Forum : Electronics : Microprocessors : Interrupts to find out what you are doing wrong.

Interrupts are disabled while an ISR is executing. If the Servo library uses interrupts and the interrupts are disabled that could cause the described behavior. Instead of putting that code in the ISR, set a flag in the ISR and, in loop(), call a function to move the servo if the flag is set and then clear the flag.

I should note... when i run the code I do properly declare my function for the ISR. Please disregard the syntax errors.

If you have syntactically correct code, why not post that instead? And please use the code tags, it's the </> button in the text editor.

Okay! Thank you! That makes sense. That explains a lot. And thank you for the link. That will really help. It looks like when using C++ and libraries you have a lot less control over the interrupt flag. I figured if I couldn't fix it I would just have to set a flag that triggers a function. I will have to rethink my approach a little bit but at least I know what I need to do. This actually explains some funny behavior I was getting in another piece of my project.

Again thanks for the help!

You should note too that delay() does not work within an ISR