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

Commit 4ae98f7

Browse files
authored
Add files via upload
1 parent aa5e334 commit 4ae98f7

File tree

3 files changed

+208
-4
lines changed

3 files changed

+208
-4
lines changed

examples/ISR_MultiServos/ISR_MultiServos.ino

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878

7979
#include "ESP8266_ISR_Servo.h"
8080

81+
// Published values for SG90 servos; adjust if needed
82+
#define MIN_MICROS 800 //544
83+
#define MAX_MICROS 2450
8184

8285
int servoIndex1 = -1;
8386
int servoIndex2 = -1;
@@ -87,8 +90,8 @@ void setup()
8790
Serial.begin(115200);
8891
Serial.println("\nStarting");
8992

90-
servoIndex1 = ISR_Servo.setupServo(D8);
91-
servoIndex2 = ISR_Servo.setupServo(D7);
93+
servoIndex1 = ISR_Servo.setupServo(D8, MIN_MICROS, MAX_MICROS);
94+
servoIndex2 = ISR_Servo.setupServo(D7, MIN_MICROS, MAX_MICROS);
9295

9396
if (servoIndex1 != -1)
9497
Serial.println("Setup Servo1 OK");
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/****************************************************************************************************************************
2+
* examples/MultipleRandomServos.ino
3+
* For ESP8266 boards
4+
* Written by Khoi Hoang
5+
*
6+
* Built by Khoi Hoang https://github.com/khoih-prog/ESP8266_ISR_Servo
7+
* Licensed under MIT license
8+
* Version: v1.0.0
9+
*
10+
* The ESP8266 timers are badly designed, using only 23-bit counter along with maximum 256 prescaler. They're only better than UNO / Mega.
11+
* The ESP8266 has two hardware timers, but timer0 has been used for WiFi and it's not advisable to use. Only timer1 is available.
12+
* The timer1's 23-bit counter terribly can count only up to 8,388,607. So the timer1 maximum interval is very short.
13+
* Using 256 prescaler, maximum timer1 interval is only 26.843542 seconds !!!
14+
*
15+
* Now these new 16 ISR-based PWM servo contro uses only 1 hardware timer.
16+
* The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
17+
* Therefore, their executions are not blocked by bad-behaving functions / tasks.
18+
* This important feature is absolutely necessary for mission-critical tasks.
19+
*
20+
* Notes:
21+
* Special design is necessary to share data between interrupt code and the rest of your program.
22+
* Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
23+
* variable can not spontaneously change. Because your function may change variables while your program is using them,
24+
* the compiler needs this hint. But volatile alone is often not enough.
25+
* When accessing shared variables, usually interrupts must be disabled. Even with volatile,
26+
* if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
27+
* If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
28+
* or the entire sequence of your code which accesses the data.
29+
*
30+
* Version Modified By Date Comments
31+
* ------- ----------- ---------- -----------
32+
* 1.0.0 K Hoang 04/12/2019 Initial release
33+
*****************************************************************************************************************************/
34+
35+
/****************************************************************************************************************************
36+
* This example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs.
37+
* Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet
38+
* and Blynk services. You can also have many (up to 16) timers to use.
39+
* This non-being-blocked important feature is absolutely necessary for mission-critical tasks.
40+
* You'll see blynkTimer is blocked while connecting to WiFi / Internet / Blynk, and elapsed time is very unaccurate
41+
* In this super simple example, you don't see much different after Blynk is connected, because of no competing task is
42+
* written
43+
*
44+
* From ESP32 Servo Example Using Arduino ESP32 Servo Library
45+
* John K. Bennett
46+
* March, 2017
47+
*
48+
* Different servos require different pulse widths to vary servo angle, but the range is
49+
* an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos
50+
* sweep 180 degrees, so the lowest number in the published range for a particular servo
51+
* represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top
52+
* of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,
53+
* 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800
54+
* degrees.
55+
*
56+
* Circuit:
57+
* Servo motors have three wires: power, ground, and signal. The power wire is typically red,
58+
* the ground wire is typically black or brown, and the signal wire is typically yellow,
59+
* orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw
60+
* considerable power, we will connect servo power to the VBat pin of the ESP32 (located
61+
* near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS.
62+
*
63+
* We could also connect servo power to a separate external
64+
* power source (as long as we connect all of the grounds (ESP32, servo, and external power).
65+
* In this example, we just connect ESP32 ground to servo ground. The servo signal pins
66+
* connect to any available GPIO pins on the ESP32 (in this example, we use pins
67+
* 22, 19, 23, & 18).
68+
*
69+
* In this example, we assume four Tower Pro SG90 small servos.
70+
* The published min and max for this servo are 500 and 2400, respectively.
71+
* These values actually drive the servos a little past 0 and 180, so
72+
* if you are particular, adjust the min and max values to match your needs.
73+
* Experimentally, 550 and 2350 are pretty close to 0 and 180.*
74+
*****************************************************************************************************************************/
75+
76+
#define TIMER_INTERRUPT_DEBUG 1
77+
#define ISR_SERVO_DEBUG 0
78+
79+
#include "ESP8266_ISR_Servo.h"
80+
81+
// Published values for SG90 servos; adjust if needed
82+
#define MIN_MICROS 800 //544
83+
#define MAX_MICROS 2450
84+
85+
#define NUM_SERVOS 6
86+
87+
typedef struct
88+
{
89+
int servoIndex;
90+
uint8_t servoPin;
91+
} ISR_servo_t;
92+
93+
ISR_servo_t ISR_servo[NUM_SERVOS] =
94+
{
95+
{ -1, D0 }, { -1, D1 }, { -1, D2 }, { -1, D5 }, { -1, D6 }, { -1, D7 }
96+
};
97+
98+
void setup()
99+
{
100+
Serial.begin(115200);
101+
Serial.println("\nStarting");
102+
103+
for (int index = 0; index < NUM_SERVOS; index++)
104+
{
105+
ISR_servo[index].servoIndex = ISR_Servo.setupServo(ISR_servo[index].servoPin, MIN_MICROS, MAX_MICROS);
106+
107+
if (ISR_servo[index].servoIndex != -1)
108+
Serial.println("Setup OK Servo index = " + String(ISR_servo[index].servoIndex));
109+
else
110+
Serial.println("Setup Failed Servo index = " + String(ISR_servo[index].servoIndex));
111+
}
112+
}
113+
114+
void loop()
115+
{
116+
int position; // position in degrees
117+
118+
position = 0;
119+
Serial.println("Servos @ 0 degree");
120+
for (int index = 0; index < NUM_SERVOS; index++)
121+
{
122+
ISR_Servo.setPosition(ISR_servo[index].servoIndex, position );
123+
}
124+
// waits 1s for the servo to reach the position
125+
delay(5000);
126+
127+
position = 90;
128+
Serial.println("Servos @ 90 degree");
129+
for (int index = 0; index < NUM_SERVOS; index++)
130+
{
131+
ISR_Servo.setPosition(ISR_servo[index].servoIndex, position );
132+
}
133+
// waits 1s for the servo to reach the position
134+
delay(5000);
135+
136+
position = 180;
137+
Serial.println("Servos @ 180 degree");
138+
for (int index = 0; index < NUM_SERVOS; index++)
139+
{
140+
ISR_Servo.setPosition(ISR_servo[index].servoIndex, position );
141+
}
142+
// waits 1s for the servo to reach the position
143+
delay(5000);
144+
145+
Serial.println("Servos sweeps from 0-180 degress");
146+
for (position = 0; position <= 180; position += 1)
147+
{
148+
// goes from 0 degrees to 180 degrees
149+
// in steps of 1 degree
150+
for (int index = 0; index < NUM_SERVOS; index++)
151+
{
152+
ISR_Servo.setPosition(ISR_servo[index].servoIndex, position );
153+
}
154+
// waits 1s for the servo to reach the position
155+
delay(50);
156+
}
157+
delay(5000);
158+
159+
Serial.println("Servos sweeps from 180-0 degress");
160+
for (position = 180; position >= 0; position -= 1)
161+
{
162+
// goes from 0 degrees to 180 degrees
163+
// in steps of 1 degree
164+
for (int index = 0; index < NUM_SERVOS; index++)
165+
{
166+
ISR_Servo.setPosition(ISR_servo[index].servoIndex, position );
167+
}
168+
// waits 1s for the servo to reach the position
169+
delay(50);
170+
}
171+
delay(5000);
172+
173+
Serial.println("Servos, index depending, be somewhere from 0-180 degress");
174+
for (position = 0; position <= 180; position += 1)
175+
{
176+
// goes from 0 degrees to 180 degrees
177+
// in steps of 1 degree
178+
for (int index = 0; index < NUM_SERVOS; index++)
179+
{
180+
ISR_Servo.setPosition(ISR_servo[index].servoIndex, (position + index * (180 / NUM_SERVOS)) % 180 );
181+
}
182+
// waits 1s for the servo to reach the position
183+
delay(50);
184+
}
185+
delay(5000);
186+
187+
Serial.println("Servos, index depending, be somewhere from 180-0 degress");
188+
for (position = 180; position >= 0; position -= 1)
189+
{
190+
// goes from 0 degrees to 180 degrees
191+
// in steps of 1 degree
192+
for (int index = 0; index < NUM_SERVOS; index++)
193+
{
194+
ISR_Servo.setPosition(ISR_servo[index].servoIndex, (position + index * (180 / NUM_SERVOS)) % 180 );
195+
}
196+
// waits 1s for the servo to reach the position
197+
delay(50);
198+
}
199+
delay(5000);
200+
201+
}

examples/MultipleServos/MultipleServos.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979
#include "ESP8266_ISR_Servo.h"
8080

8181
// Published values for SG90 servos; adjust if needed
82-
#define MIN_MICROS 1000
83-
#define MAX_MICROS 2000
82+
#define MIN_MICROS 800 //544
83+
#define MAX_MICROS 2450
8484

8585
#define NUM_SERVOS 6
8686

0 commit comments

Comments
 (0)