Skip to content

Commit 951c6da

Browse files
Balamurugan RBala050814
authored andcommitted
feat(example): Add writeMicroseconds_Example sketch (issue #51)
1 parent 2d43890 commit 951c6da

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
77.3 KB
Loading
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <Servo.h> // Corrected: Include the library first
2+
3+
/* * --------------------------------------------------------------------------
4+
* writeMicroseconds_Example
5+
* --------------------------------------------------------------------------
6+
* The writeMicroseconds() function is part of the Servo Library.
7+
* It sets the servo's angle by accepting a pulse width duration measured in microseconds (µs).
8+
* * CORE TIMING VALUES (Note: Values can vary slightly by servo model):
9+
* * 1. Minimum Position (0 degrees):
10+
* - Value: 544 microseconds (us)
11+
* - Result: Servo moves to its extreme counter-clockwise limit (0°).
12+
* * 2. Center Position (90 degrees):
13+
* - Value: 1500 microseconds (us)
14+
* - Result: Servo moves to its neutral, center position (90°).
15+
* * 3. Maximum Position (180 degrees):
16+
* - Value: 2400 microseconds (us)
17+
* - Result: Servo moves to its extreme clockwise limit (180°).
18+
*
19+
* * BASIC WORKING PRINCIPLE:
20+
* Servo motors use Pulse Width Modulation (PWM). The duration (width) that the
21+
* control wave is held HIGH directly determines the commanded angle. This function
22+
* allows for precise, non-standard movement by letting the user specify the exact time duration.
23+
*/
24+
25+
// Create a servo Object. We'll use myservo for standardization.
26+
Servo myservo; // Correction: Added standard name and semicolon
27+
28+
void setup() {
29+
// 1. Attach the servo object to a PWM pin (e.g., Digital Pin 9).
30+
myservo.attach(9);
31+
32+
// 2. Initialize Serial Monitor to explain the timing values.
33+
Serial.begin(9600);
34+
Serial.println("--- Servo Microseconds Example Running ---");
35+
}
36+
37+
void loop() {
38+
// 1. Write CENTER position (Standard 90 degrees)
39+
myservo.writeMicroseconds(1500);
40+
Serial.println("Position: Center (1500 us)");
41+
delay(1500); // Wait 1.5 seconds
42+
43+
// 2. Write MINIMUM position (Standard 0 degrees)
44+
myservo.writeMicroseconds(544);
45+
Serial.println("Position: Min (544 us)");
46+
delay(1500);
47+
48+
// 3. Write MAXIMUM position (Standard 180 degrees)
49+
myservo.writeMicroseconds(2400);
50+
Serial.println("Position: Max (2400 us)");
51+
delay(1500);
52+
}

0 commit comments

Comments
 (0)