-
Notifications
You must be signed in to change notification settings - Fork 20
/
Servo_Sweep_Speed.ino
57 lines (49 loc) · 1.09 KB
/
Servo_Sweep_Speed.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
/*
Controls 2 servo positions and speed from 0-180 deg and back
https://wokwi.com/projects/351978833396630095
by dlloydev, December 2022.
*/
#include <Servo.h>
Servo myservo = Servo();
const int speedPin1 = 32, speedPin2 = 12;
const int servoPin1 = 14, servoPin2 = 13;
float speed1, speed2, pos1, pos2;
bool dir1, dir2;
void setup() {
}
void loop() {
speed1 = (analogRead(speedPin1)) / 4095.0; // 0-100% speed
speed2 = (analogRead(speedPin2)) / 2047.0; // 0-200% speed
myservo.write(servoPin1, pos1);
delay(5); // for simulator
myservo.write(servoPin2, pos2);
delay(5); // for simulator
if (pos1 < 0) pos1 = 0;
if (pos2 < 0) pos2 = 0;
if (dir1) {
if (pos1 <= 180.0) {
pos1 += speed1;
} else {
dir1 = false;
}
} else {
if (pos1 >= 0.5) {
pos1 -= speed1;
} else {
dir1 = true;
}
}
if (dir2) {
if (pos2 <= 180.0) {
pos2 += speed2;
} else {
dir2 = false;
}
} else {
if (pos2 >= 0.5) {
pos2 -= speed2;
} else {
dir2 = true;
}
}
} // loop speed determines the maximum servo speed limit