Disabling interrupts and servo library

Was playing around with interrupts and I noticed that the following lines of code actually work. My question is why? Why disabling interrupts does not mess with the timing??
Does noInterrupts(); leave some interrupts unaffected??

According to the code here the library uses interrupts:

#include <Servo.h>
Servo myservo;  // create servo object to control a servo
int val;    // variable to read the value from the analog pin

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  val = 1700;     
  noInterrupts();      
  myservo.writeMicroseconds(val);  
  interrupts();
  delay(15);                           
}

Why would you expect it not to work? Your code momentarily disables interrupts to set up the servo's tick count, and then turns them right back on again. If you look in the source (for AVR at least) for writeMicroseconds, it allows for interrupts to have been enabled or disabled when it was called.

Now, if you were to tell me that this worked, that'd be interesting.

#include <Servo.h>
Servo myservo;  // create servo object to control a servo
int val;    // variable to read the value from the analog pin

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  val = 1700;     
  noInterrupts();      
  myservo.writeMicroseconds(val);  
}

void loop() {
}

You are very right. For some reason I assumed this was a blocking event, while I also stated that it uses interrupts... Oh well...
Thank you for the reply.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.