-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathServo_Sweep.ino
58 lines (45 loc) · 1.67 KB
/
Servo_Sweep.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
ServoI2C Sweep demo
(c) juh 2022
This is a 1:1 equivalent of the Servo library's Sweep example
https://docs.arduino.cc/learn/electronics/servo-motors
Needs the ServoI2C.h module enabled in the target's firmware_modules.h.
*/
#include <Wire.h>
#include <ServoI2C.h>
uint8_t i2cAddress = 0x08;
I2Cwrapper wrapper(i2cAddress); // each target device is represented by a wrapper...
ServoI2C myservo(&wrapper); // ...that the servo needs to communicate with the controller
int pos = 0; // variable to store the servo position
void setup()
{
Wire.begin();
Serial.begin(115200);
// Wire.setClock(10000); // uncomment for ESP8266 targets, to be on the safe side
if (!wrapper.ping()) {
halt("Target not found! Check connections and restart.");
} else {
Serial.println("Target found as expected. Proceeding.\n");
}
wrapper.reset(); // reset the target device
myservo.attach(9); // attaches the servo on _the target's_ pin "9" to the servo object
}
void loop()
{
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
void halt(const char* m) {
Serial.println(m);
Serial.println("\n\nHalting.\n");
while (true) {
yield(); // prevent ESPs from watchdog reset
}
}