Skip to content

Commit

Permalink
Do not auto-attach interrupt on construction
Browse files Browse the repository at this point in the history
This was causing issues on certain platforms where the platform's interrupt attachment system was not set up or available when the object was constructed in global scope (see dmadison#28).

Instead, the user must attach() the object themselves before they can read signals. The library examples have been updated accordingly.
  • Loading branch information
dmadison committed Jan 25, 2024
1 parent 7da0050 commit c8d7925
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/BasicAngle/BasicAngle.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ ServoInputPin<2> servo;

void setup() {
Serial.begin(115200);
servo.attach(); // attaches the servo input interrupt

while (servo.available() == false) {
Serial.println("Waiting for servo signal...");
Expand Down
1 change: 1 addition & 0 deletions examples/Calibration/Calibration.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ServoInputPin<2> servo;

void setup() {
Serial.begin(115200);
servo.attach(); // attaches the servo input interrupt

int center = servo.getRangeCenter(); // get center value of range
servo.setRange(center, center); // set min/max values to center
Expand Down
1 change: 1 addition & 0 deletions examples/Loopback/Loopback.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ int waitTime = 10; // milliseconds (ms)
void setup() {
Serial.begin(115200);

inputServo.attach(); // attaches the servo input interrupt
outputServo.attach(OutputPin); // attaches the servo on pin 9 to the servo object

while (inputServo.available() == false) {
Expand Down
2 changes: 2 additions & 0 deletions examples/PinChangeInt/PinChange/PinChange.ino
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ ISR(PCINT0_vect) { // pin change ISR handler for Arduino Uno pins D8 - D13
void setup() {
Serial.begin(115200);

// note that we do *not* need to call servo.attach(), because we are
// using our own custom interrupt service routine (ISR)
setInterrupt(); // set pin change interrupt (see above)

while (servo.available() == false) {
Expand Down
2 changes: 2 additions & 0 deletions examples/PinChangeInt/PinChangeLib/PinChangeLib.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ ServoInputPin<pin2> servo2;

void setup() {
Serial.begin(115200);
servo1.attach(); // attaches the first servo input interrupt
servo2.attach(); // attaches the second servo input interrupt

// wait for all servo signals to be read for the first time
while (!ServoInput.available()) {
Expand Down
2 changes: 2 additions & 0 deletions examples/RC_Receiver/RC_Receiver.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ ServoInputPin<ThrottleSignalPin> throttle(ThrottlePulseMin, ThrottlePulseMax);

void setup() {
Serial.begin(115200);
steering.attach(); // attaches the steering servo input interrupt
throttle.attach(); // attaches the throttle servo input interrupt

while (!ServoInput.available()) { // wait for all signals to be ready
Serial.println("Waiting for servo signals...");
Expand Down
1 change: 1 addition & 0 deletions examples/Remapping/Remapping.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ ServoInputPin<2> servo;

void setup() {
Serial.begin(115200);
servo.attach(); // attaches the servo input interrupt

while (servo.available() == false) {
Serial.println("Waiting for servo signal...");
Expand Down
1 change: 0 additions & 1 deletion src/ServoInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class ServoInputPin : public ServoInputSignal {
ServoInputPin<Pin>::PortRegister = SERVOINPUT_PIN_TO_BASEREG(Pin);
#endif
pinMode(Pin, INPUT_PULLUP);
attach();
}

ServoInputPin(uint16_t pMin, uint16_t pMax) : ServoInputPin() {
Expand Down

0 comments on commit c8d7925

Please sign in to comment.