This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestFourServoMotors.ino
123 lines (109 loc) · 2.86 KB
/
TestFourServoMotors.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// This code is about testing the servo motors before build the drone.
// This code does not include controller.
// With this code, we control the servo motors via the computer.
// This code is written in Arduino IDE
#include <Servo.h>
Servo servoM1;
Servo servoM2;
Servo servoM3;
Servo servoM4;
int pos1 = 0;
int pos2 = 0;
int pos3 = 0;
int pos4 = 0;
bool isRunning = false; // To check the servo motors are working or not.
int maxPos = 120;
int minPos = 0;
int stepSize = 0;
int currentSpeed = 0;
void setup() {
servoM1.attach(8); //PIN 8 on arduino mega
servoM2.attach(9); //PIN 9 on arduino mega
servoM3.attach(10); //PIN 10 on arduino mega
servoM4.attach(11); //PIN 11 on arduino mega
servoM1.write(pos1);
servoM2.write(pos2);
servoM3.write(pos3);
servoM4.write(pos4);
Serial.begin(115200); // To start the serial communication. 115200 baud rate is for arduino mega.
Serial.flush();
}
void loop() {
if (Serial.available()) {
Serial.print("setup\n");
int data = Serial.read();
while(isRunning){
if (pos1 < currentSpeed) {
pos1 += 1;
} else if (pos1 > currentSpeed) {
pos1 -= 1;
}
if (pos2 < currentSpeed) {
pos2 += 1;
} else if (pos2 > currentSpeed) {
pos2 -= 1;
}
if (pos3 < currentSpeed) {
pos3 += 1;
} else if (pos3 > currentSpeed) {
pos3 -= 1;
}
if (pos4 < currentSpeed) {
pos4 += 1;
} else if (pos4 > currentSpeed) {
pos4 -= 1;
}
servoM1.write(pos1);
servoM2.write(pos2);
servoM3.write(pos3);
servoM4.write(pos4);
delay(15);
Serial.print("stop ready\n");
if(Serial.available()){
data=Serial.read();
if (data == 's') { // Stop the servo motors
Serial.print("start ready\n");
Serial.flush();
currentSpeed = 0;
while(pos1>0 || pos2>0 || pos3>0 || pos4>0)
{
servoM1.write(pos1);
servoM2.write(pos2);
servoM3.write(pos3);
servoM4.write(pos4);
delay(15);
pos1-=1;
pos2-=1;
pos3-=1;
pos4-=1;
}
isRunning = false;
}
if (data == 'u') { //Speed up the servo motors
Serial.print("speeddup\n");
Serial.flush();
if(currentSpeed<=120){
currentSpeed+=20;
}
}
if (data == 'd') { //Slow down the servo motors
Serial.print("speeddown\n");
Serial.flush();
if(currentSpeed>=60){
currentSpeed-=20;
}
}
}
}
Serial.print("start ready\n");
if (data == 'r') { //To ready before the start the servo motors.
isRunning = true;
currentSpeed=40;
Serial.flush();
}
}
else
{
Serial.println("1\n");
}
}