Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit aaeadf6

Browse files
authored
v1.0.2
1. Enhance README.md 2. Add platformio.ini
1 parent 423f94a commit aaeadf6

File tree

3 files changed

+364
-35
lines changed

3 files changed

+364
-35
lines changed

README.md

Lines changed: 280 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## ESP8266_ISR_Servo Library
1+
# ESP8266_ISR_Servo Library
22

33
[![arduino-library-badge](https://www.ardu-badge.com/badge/ESP8266_ISR_Servo.svg?)](https://www.ardu-badge.com/ESP8266_ISR_Servo)
44
[![GitHub release](https://img.shields.io/github/release/khoih-prog/ESP8266_ISR_Servo.svg)](https://github.com/khoih-prog/ESP8266_ISR_Servo/releases)
@@ -7,52 +7,99 @@
77
[![GitHub issues](https://img.shields.io/github/issues/khoih-prog/ESP8266_ISR_Servo.svg)](http://github.com/khoih-prog/ESP8266_ISR_Servo/issues)
88

99

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

12-
***Why do we need this ISR-based Servo controller?***
13+
### Version v1.0.2
1314

14-
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().
15+
1. Add example using [Blynk] (http://docs.blynk.cc/) to control servos.
16+
2. Change example names to avoid duplication.
17+
18+
#### Version v1.0.1
19+
20+
1. Basic 16 ISR-based servo controllers using 1 hardware timer for ESP8266.
21+
22+
---
23+
24+
This library enables you to use **1 Hardware Timer** on an ESP8266-based board to control up to **16 independent servo motors**.
25+
26+
27+
#### Why do we need this ISR-based Servo controller?
28+
29+
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().
1530

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

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

20-
The correct choice is to use a Hardware Timer with Interrupt to call your function.
35+
The correct choice is to use a **Hardware Timer with Interrupt** to call your function.
36+
37+
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.
38+
39+
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.
40+
41+
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:
42+
43+
[HOWTO Attach Interrupt](https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/)
44+
45+
---
2146

22-
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.
47+
### Important Notes:
2348

24-
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.
49+
1. Inside the attached function, delay() won’t work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.
2550

26-
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:
51+
2. Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as volatile.
2752

28-
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
53+
---
54+
---
2955

30-
**Important Notes:**
31-
1. Inside the attached function, `delay()` won’t work and the value returned by `millis()` will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.
56+
## Prerequisite
3257

33-
2. Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as `volatile`.
58+
1. [`Arduino IDE 1.8.12+` for Arduino](https://www.arduino.cc/en/Main/Software)
59+
2. [`ESP8266 core 2.7.3+` for Arduino](https://github.com/esp8266/Arduino#installing-with-boards-manager) for ESP8266 boards.
3460

35-
### Installation
61+
---
3662

37-
It's better to use `Arduino Library Manager` to install this library.
63+
## Installation
3864

39-
Otherwise, you can install it using the following steps:
65+
### Use Arduino Library Manager
4066

41-
1. Navigate to (https://github.com/khoih-prog/ESP8266_ISR_Servo) page.
67+
The best and easiest way is to use `Arduino Library Manager`. Search for `ESP8266_ISR_Servo`, then select / install the latest version.
68+
You can also use this link [![arduino-library-badge](https://www.ardu-badge.com/badge/ESP8266_ISR_Servo.svg?)](https://www.ardu-badge.com/ESP8266_ISR_Servo) for more detailed instructions.
69+
70+
### Manual Install
71+
72+
Another way to install is to:
73+
74+
1. Navigate to [ESP8266_ISR_Servo](https://github.com/khoih-prog/ESP8266_ISR_Servo) page.
4275
2. Download the latest release `ESP8266_ISR_Servo-master.zip`.
4376
3. Extract the zip file to `ESP8266_ISR_Servo-master` directory
44-
4. Copy whole folder to Arduino libraries' directory such as `.Arduino/libraries/ESP8266_ISR_Servo-master`.
77+
4. Copy whole `ESP8266_ISR_Servo-master` folder to Arduino libraries' directory such as `~/Arduino/libraries/`.
78+
79+
### VS Code & PlatformIO:
80+
81+
1. Install [VS Code](https://code.visualstudio.com/)
82+
2. Install [PlatformIO](https://platformio.org/platformio-ide)
83+
3. Install **ESP8266_ISR_Servo** library by using [Library Manager](https://docs.platformio.org/en/latest/librarymanager/). Search for ESP8266_ISR_Servo in [Platform.io Author's Libraries](https://platformio.org/lib/search?query=author:%22Khoi%20Hoang%22)
84+
4. Use included [platformio.ini](platformio/platformio.ini) file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at [Project Configuration File](https://docs.platformio.org/page/projectconf.html)
85+
86+
---
4587

46-
### More useful Information
88+
## More useful Information
89+
90+
The **ESP8266 timers are badly designed, using only 23-bit counter along with maximum 256 prescaler**. They're only better than UNO / Mega.
4791

48-
The ESP8266 timers are badly designed, using only 23-bit counter along with maximum 256 prescaler. They're only better than UNO / Mega.
4992
The ESP8266 has two hardware timers, but timer0 has been used for WiFi and it's not advisable to use. Only timer1 is available.
50-
The timer1's 23-bit counter terribly can count only up to 8,388,607. So the timer1 maximum interval is very short.
51-
Using 256 prescaler, maximum timer1 interval is only 26.843542 seconds !!!
93+
94+
The timer1's 23-bit counter can terribly count only up to 8,388,607. So the timer1 maximum interval is very short.
95+
96+
Using 256 prescaler, **maximum timer1 interval is only 26.843542 seconds !!!**
5297

5398
The timer1 counters can be configured to support automatic reload.
5499

55-
### New from v1.0.1
100+
---
101+
102+
## New from v1.0.1
56103

57104
1. Add functions `getPosition()` and `getPulseWidth()`
58105
2. Optimize the code
@@ -72,11 +119,15 @@ This non-being-blocked important feature is absolutely necessary for mission-cri
72119
You'll see blynkTimer Software is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking task
73120
in loop(), using delay() function as an example. The elapsed time then is very unaccurate
74121

75-
### Supported Boards
122+
---
123+
124+
## Supported Boards
76125

77126
- ESP8266
78127

79-
### Usage
128+
---
129+
130+
### HOWTO Usage
80131

81132
How to use:
82133

@@ -140,29 +191,226 @@ void loop()
140191
}
141192
142193
```
143-
### TO DO
194+
195+
---
196+
---
197+
198+
### Examples:
199+
200+
1. [ESP8266_BlynkServoControl](examples/ESP8266_BlynkServoControl)
201+
2. [ESP8266_ISR_MultiServos](examples/ESP8266_ISR_MultiServos)
202+
3. [ESP8266_MultipleRandomServos](examples/ESP8266_MultipleRandomServos)
203+
4. [ESP8266_MultipleServos](examples/ESP8266_MultipleServos)
204+
5. [ISR_MultiServos](examples/ISR_MultiServos)
205+
6. [MultipleRandomServos](examples/MultipleRandomServos)
206+
7. [MultipleServos](examples/MultipleServos)
207+
208+
---
209+
210+
### Example [Argument_Complex](examples/Argument_Complex)
211+
212+
```cpp
213+
#ifndef ESP8266
214+
#error This code is designed to run on ESP8266 platform, not Arduino nor ESP32! Please check your Tools->Board setting.
215+
#endif
216+
217+
#define BLYNK_PRINT Serial
218+
219+
//See file .../hardware/espressif/esp32/variants/(esp32|doitESP32devkitV1)/pins_arduino.h
220+
#define LED_BUILTIN 2 // Pin D4 mapped to pin GPIO2/TXD1 of ESP8266, NodeMCU and WeMoS, control on-board LED
221+
//PIN_D0 can't be used for PWM/I2C
222+
#define PIN_D0 16 // Pin D0 mapped to pin GPIO16/USER/WAKE of ESP8266. This pin is also used for Onboard-Blue LED. PIN_D0 = 0 => LED ON
223+
#define PIN_D1 5 // Pin D1 mapped to pin GPIO5 of ESP8266
224+
#define PIN_D2 4 // Pin D2 mapped to pin GPIO4 of ESP8266
225+
#define PIN_D3 0 // Pin D3 mapped to pin GPIO0/FLASH of ESP8266
226+
#define PIN_D4 2 // Pin D4 mapped to pin GPIO2/TXD1 of ESP8266
227+
#define PIN_LED 2 // Pin D4 mapped to pin GPIO2/TXD1 of ESP8266, NodeMCU and WeMoS, control on-board LED
228+
#define PIN_D5 14 // Pin D5 mapped to pin GPIO14/HSCLK of ESP8266
229+
#define PIN_D6 12 // Pin D6 mapped to pin GPIO12/HMISO of ESP8266
230+
#define PIN_D7 13 // Pin D7 mapped to pin GPIO13/RXD2/HMOSI of ESP8266
231+
#define PIN_D8 15 // Pin D8 mapped to pin GPIO15/TXD2/HCS of ESP8266
232+
233+
#define USE_SPIFFS true
234+
//#define USE_SPIFFS false
235+
236+
#define USE_BLYNK_WM true // https://github.com/khoih-prog/Blynk_WM
237+
//#define USE_BLYNK_WM false
238+
239+
//LIBRARIES INCLUDED
240+
#include <ESP8266WiFi.h>
241+
242+
#if USE_BLYNK_WM
243+
#include <BlynkSimpleEsp8266_WM.h> // https://github.com/khoih-prog/Blynk_WM
244+
#else
245+
#include <BlynkSimpleEsp8266.h>
246+
247+
//BLYNK AUTHENTICATION TOKEN
248+
char auth[] = "******";
249+
250+
251+
// MY WIFI CREDENTIALS
252+
char ssid[] = "****";
253+
char pass[] = "****";
254+
255+
#endif
256+
257+
#define TIMER_INTERRUPT_DEBUG 1
258+
#define ISR_SERVO_DEBUG 1
259+
260+
#include "ESP8266_ISR_Servo.h"
261+
262+
// MG996R servo has a running current of 500mA to 900mA @6V and a stall current of 2.5A @ 6V
263+
// Power supply must be adequate
264+
// Published values for SG90 servos; adjust if needed
265+
#define MIN_MICROS 800 //544
266+
#define MAX_MICROS 2450
267+
268+
int servoIndex1 = -1;
269+
int servoIndex2 = -1;
270+
int servoIndex3 = -1;
271+
272+
int servo1Pin = PIN_D6; //SERVO1 PIN
273+
int servo2Pin = PIN_D7; //SERVO2 PIN
274+
int servo3Pin = PIN_D8; //SERVO3 PIN
275+
276+
BlynkTimer timer;
277+
278+
// These are Blynk Slider or any Widget (STEP, Numeric Input, being able to output (unsigned) int value from 0-180.
279+
// You have to map from 0-180 inside widget or in your code. Otherwise, the library will remap the input for you.
280+
#define BLYNK_VPIN_SERVO1_CONTROL V21
281+
#define BLYNK_VPIN_SERVO2_CONTROL V22
282+
#define BLYNK_VPIN_SERVO3_CONTROL V23
283+
284+
//READING FROM VIRTUAL PINS
285+
// SERVO1
286+
BLYNK_WRITE(BLYNK_VPIN_SERVO1_CONTROL)
287+
{
288+
ISR_Servo.setPosition(servoIndex1, param.asInt());
289+
}
290+
291+
//SERVO2
292+
BLYNK_WRITE(BLYNK_VPIN_SERVO2_CONTROL)
293+
{
294+
ISR_Servo.setPosition(servoIndex2, param.asInt());
295+
}
296+
297+
//SERVO3
298+
BLYNK_WRITE(BLYNK_VPIN_SERVO3_CONTROL)
299+
{
300+
ISR_Servo.setPosition(servoIndex3, param.asInt());
301+
}
302+
303+
void heartBeatPrint(void)
304+
{
305+
static int num = 1;
306+
307+
if (WiFi.status() == WL_CONNECTED)
308+
{
309+
if (Blynk.connected())
310+
Serial.print("B"); // B means connected to Blynk
311+
else
312+
Serial.print("H"); // H means connected to WiFi but no Blynk
313+
}
314+
else
315+
Serial.print("F"); // F means not connected to WiFi and Blynk
316+
317+
if (num == 80)
318+
{
319+
Serial.println();
320+
num = 1;
321+
}
322+
else if (num++ % 10 == 0)
323+
{
324+
Serial.print(" ");
325+
}
326+
}
327+
328+
void setup()
329+
{
330+
// Debug console
331+
Serial.begin(115200);
332+
Serial.println("\nStarting");
333+
334+
#if USE_BLYNK_WM
335+
Blynk.begin();
336+
#else
337+
Blynk.begin(auth, ssid, pass);
338+
#endif
339+
340+
servoIndex1 = ISR_Servo.setupServo(servo1Pin, MIN_MICROS, MAX_MICROS);
341+
servoIndex2 = ISR_Servo.setupServo(servo2Pin, MIN_MICROS, MAX_MICROS);
342+
servoIndex3 = ISR_Servo.setupServo(servo3Pin, MIN_MICROS, MAX_MICROS);
343+
344+
if (servoIndex1 != -1)
345+
Serial.println("Setup Servo1 OK");
346+
else
347+
Serial.println("Setup Servo1 failed");
348+
349+
if (servoIndex2 != -1)
350+
Serial.println("Setup Servo2 OK");
351+
else
352+
Serial.println("Setup Servo2 failed");
353+
354+
if (servoIndex3 != -1)
355+
Serial.println("Setup Servo3 OK");
356+
else
357+
Serial.println("Setup Servo3 failed");
358+
359+
timer.setInterval(30000L, heartBeatPrint);
360+
}
361+
362+
void loop()
363+
{
364+
Blynk.run();
365+
timer.run();
366+
}
367+
```
368+
369+
---
370+
371+
372+
## TO DO
144373
145374
1. Search for bug and improvement.
146-
2. Similar features for Arduino (UNO, Mega, etc...) and ESP32
147375
148376
149-
### DONE
377+
## DONE
378+
379+
1. Similar features for Arduino (UNO, Mega, etc...) and ESP32
380+
2. Add functions `getPosition()` and `getPulseWidth()`
381+
3. Optimize the code
382+
4. Add more complicated examples
383+
384+
---
385+
---
150386
151-
#### For current version v1.0.2
387+
### Version v1.0.2
152388
153-
1. Add example using [Blynk] (http://docs.blynk.cc/) to control servos. Change example names to avoid duplication.
389+
1. Add example using [Blynk] (http://docs.blynk.cc/) to control servos.
390+
2. Change example names to avoid duplication.
154391
155-
#### For current version v1.0.1
392+
#### Version v1.0.1
156393
157394
1. Basic 16 ISR-based servo controllers using 1 hardware timer for ESP8266.
158395
396+
---
397+
398+
### Issues ###
399+
400+
Submit issues to: [ESP8266_ISR_Servo issues](https://github.com/khoih-prog/ESP8266_ISR_Servo/issues)
401+
402+
---
403+
404+
## Contributing
159405
160-
### Contributing
161406
If you want to contribute to this project:
162407
- Report bugs and errors
163408
- Ask for enhancements
164409
- Create issues and pull requests
165410
- Tell other people about this library
166411
167-
### Copyright
412+
---
413+
414+
## Copyright
415+
168416
Copyright 2019- Khoi Hoang

library.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=ESP8266_ISR_Servo
22
version=1.0.2
33
author=Khoi Hoang
4-
maintainer=Khoi Hoang <khoih58@gmail.com>
4+
maintainer=Khoi Hoang <khoih.prog@gmail.com>
55
license=MIT
6-
sentence=This library enables you to use 1 Hardware Timer on an ESP8266 to control 16 or more servo motors.
7-
paragraph=Now these new 16 ISR-based Servo controllers just use one ESP8266 Hardware Timer. The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers. Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks.
6+
sentence=This library enables you to use Interrupt from Hardware Timers on an ESP8266 to control servo motors.
7+
paragraph=This library enables you to use 1 Hardware Timer on an ESP8266-based board to control up to 16 or more servo motors.
88
category=Timing Control Device Time Timer Servo Interrupt esp8266
99
url=https://github.com/khoih-prog/ESP8266_ISR_Servo
1010
architectures=esp8266

0 commit comments

Comments
 (0)