ESP8266_ISR_Servo Library

How To Install Using Arduino Library Manager

This library enables you to use 1 Hardware Timer on an ESP8266-based board to control up to 16 independent servo motors.

Why do we need this ISR-based Servo controller?

Imagine you have a system with a mission-critical function, controlling a robot arm or doing something much more important. You normally use a software timer to poll, or even place the function in loop(). But what if another function is blocking the loop() or setup().

So your function might not be executed, and the result would be disastrous.

You'd prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.).

The correct choice is to use a Hardware Timer with Interrupt to call your function.

These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy.

Functions using normal software timers, relying on loop() and calling millis(), won't work if the loop() or setup() is blocked by certain operation. For example, certain function is blocking while it's connecting to WiFi or some services.

The catch is your function is now part of an ISR (Interrupt Service Routine), and must be lean / mean, and follow certain rules. More to read on:

Attach Interrupt/

Sample Code

#define TIMER_INTERRUPT_DEBUG       1
#define ISR_SERVO_DEBUG             1

#include "ESP8266_ISR_Servo.h"

// Published values for SG90 servos; adjust if needed
#define MIN_MICROS      800  //544
#define MAX_MICROS      2450

int servoIndex1  = -1;
int servoIndex2  = -1;

void setup() 
{
  Serial.begin(115200);
  Serial.println("\nStarting");

  servoIndex1 = ISR_Servo.setupServo(D8, MIN_MICROS, MAX_MICROS);
  servoIndex2 = ISR_Servo.setupServo(D7, MIN_MICROS, MAX_MICROS);
  
  if (servoIndex1 != -1)
    Serial.println("Setup Servo1 OK");
  else
    Serial.println("Setup Servo1 failed");

  if (servoIndex2 != -1)
    Serial.println("Setup Servo2 OK");
  else
    Serial.println("Setup Servo2 failed");
}

void loop() 
{
  int position;

  if ( ( servoIndex1 != -1) && ( servoIndex2 != -1) )
  {
    for (position = 0; position <= 180; position++) 
    { 
      // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      ISR_Servo.setPosition(servoIndex1, position);
      ISR_Servo.setPosition(servoIndex2, 180 - position);
      // waits 50ms for the servo to reach the position
      delay(50  /*15*/);
    }
    
    for (position = 180; position >= 0; position--) 
    { 
      // goes from 180 degrees to 0 degrees
      ISR_Servo.setPosition(servoIndex1, position);
      ISR_Servo.setPosition(servoIndex2, 180 - position);
      // waits 50ms for the servo to reach the position
      delay(50  /*15*/);
    }
  }
}