-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ino
208 lines (149 loc) · 4.18 KB
/
server.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <Servo.h>
Servo servo1;
Servo servo2;
int ser1 = 0;
int ser2 = 0;
int motA = 0;
int motB = 0;
int trig1 = 6;
int trig2 = 2;
int motA1 = 7;
int motA2 = 12;
int motB1 = 8;
int motB2 = 9;
int motAE = 10;
int motBE = 11;
void setup()
{
Serial.begin(9600);
servo1.attach(2);
servo2.attach(3);
pinMode(trig1, OUTPUT);
pinMode(trig2, OUTPUT);
pinMode(motAE, OUTPUT); // Motor Enable pins
pinMode(motBE, OUTPUT);
pinMode(motA1, OUTPUT); // Motor poles config
pinMode(motA2, OUTPUT);
pinMode(motB1, OUTPUT);
pinMode(motB2, OUTPUT);
}
void loop() {
while (Serial.available() > 0) {
int value0 = Serial.parseInt();
int value1 = Serial.parseInt();
int value2 = Serial.parseInt();
int value3 = Serial.parseInt();
int value4 = Serial.parseInt();
if ( Serial.read() == '\n' ) {
int axisy = value0;
int axisx = value1;
if ( value2 == 1 ) {
digitalWrite(trig1,HIGH);
} else {
digitalWrite(trig1,LOW);
}
if ( value3 == 1 ) {
//Move Servos
ser1 = map(axisy, 0, 265, 0, 179); // scale it to use it with the servo (value between 0 and 180)
servo1.write(ser1); // sets the servo position according to the scaled value
ser2 = map(axisx, 0, 265, 45, 135); // scale it to use it with the servo (value between 0 and 180)
servo2.write(ser2); // sets the servo position according to the scaled value
}
else {
if ( axisy > 140 ) {
// go forward
motA = map(axisy, 140, 265, 50, 265);
motB = map(axisy, 140, 265, 50, 265);
if ( axisx < 100 ) {
// go left
int motBvalue = map(axisx, 0, 100, 50, 265);
motB = motB - motBvalue;
}
else if ( axisx > 140 ) {
// go right
int motAvalue = map(axisx, 140, 265, 265, 50);
motA = motA - motAvalue;
}
else {
// forward both (DO NOTHING as value is already set)
}
motorfwd(motB,motA);
}
else if (axisy < 100 ) {
// go forward
motA = map(axisy, 0, 100, 265, 50);
motB = map(axisy, 0, 100, 265, 50);
if ( axisx < 100 ) {
// go left
int motAvalue = map(axisx, 0, 100, 265, 50);
motA = motA - motAvalue;
}
else if ( axisx > 140 ) {
// go right
int motBvalue = map(axisx, 140, 265, 50, 265);
motB = motB - motBvalue;
}
else {
// forward both (DO NOTHING as value is already set)
}
motorbwd(motB,motA);
}
else {
if ( axisx < 100 ) {
// rotate on spot left
motA = map(axisx, 0, 100, 265, 50);
motorlef(motA, motA);
}
else if ( axisx > 140 ) {
// rotate on spot right
motA = map(axisx, 140, 265, 50, 265);
motorrig(motA, motA);
}
else {
motorstop(motA,motB);
}
}
}
}
}
}
void motorfwd(int motAspd, int motBspd) {
digitalWrite(motA1, HIGH);
digitalWrite(motA2, LOW);
analogWrite(motAE, motAspd);
analogWrite(motBE, motBspd);
digitalWrite(motB1, LOW);
digitalWrite(motB2, HIGH);
}
void motorbwd(int motAspd, int motBspd) {
analogWrite(motAE, motAspd);
digitalWrite(motA1, LOW);
digitalWrite(motA2, HIGH);
analogWrite(motBE, motBspd);
digitalWrite(motB1, HIGH);
digitalWrite(motB2, LOW);
}
void motorrig(int motAspd, int motBspd) {
analogWrite(motAE, motAspd);
digitalWrite(motA1, LOW);
digitalWrite(motA2, HIGH);
analogWrite(motBE, motBspd);
digitalWrite(motB1, LOW);
digitalWrite(motB2, HIGH);
}
void motorlef(int motAspd, int motBspd) {
analogWrite(motAE, motAspd);
digitalWrite(motA1, HIGH);
digitalWrite(motA2, LOW);
analogWrite(motBE, motBspd);
digitalWrite(motB1, HIGH);
digitalWrite(motB2, LOW);
}
void motorstop(int motAspd, int motBspd) {
analogWrite(motAE, 0);
digitalWrite(motA1, LOW);
digitalWrite(motA2, LOW);
analogWrite(motBE, 0);
digitalWrite(motB1, LOW);
digitalWrite(motB2, LOW);
}