-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobot.ino
258 lines (228 loc) · 5.43 KB
/
Robot.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <Servo.h>
#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11);
// Pinii motor 1
#define mpin00 5
#define mpin01 6
// Pinii motor 2
#define mpin10 3
#define mpin11 2
int trigPin = 9; // Trigger
int echoPin = 7; // Echo
char Incoming_value;
Servo srv;
void setup() {
// configurarea pinilor motor ca iesire, initial valoare 0
digitalWrite(mpin00, 0);
digitalWrite(mpin01, 0);
digitalWrite(mpin10, 0);
digitalWrite(mpin11, 0);
pinMode (mpin00, OUTPUT);
pinMode (mpin01, OUTPUT);
pinMode (mpin10, OUTPUT);
pinMode (mpin11, OUTPUT);
// pin LED
pinMode(13, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600); // Interfata Serial 0, pentru PC
BTserial.begin(9600);
//Serial1.begin(9600); // Interfata Serial 1, pentru Bluetooth
delay(500);
srv.attach(8);
//srv.write(90);
}
// Funcție pentru controlul unui motor
// Intrare: pinii m1 și m2, direcția și viteza
void StartMotor (int m1, int m2, int forward, int speed)
{
if (speed == 0) // oprire
{
digitalWrite(m1, 0);
digitalWrite(m2, 0);
}
else
{
if (forward)
{
digitalWrite(m2, 0);
analogWrite(m1, speed); // folosire PWM
}
else
{
digitalWrite(m1, 0);
analogWrite(m2, speed);
}
}
}
// Funcție de siguranță
// Execută oprire motoare, urmată de delay
void delayStopped(int ms)
{
StartMotor (mpin00, mpin01, 0, 0);
StartMotor (mpin10, mpin11, 0, 0);
delay(ms);
}
// Utilizare servo
// Poziționare în trei unghiuri
// La final, rămâne în mijloc (90 grade)
void playWithServo(int pin)
{
srv.attach(pin);
srv.write(0);
delay(1000);
srv.write(180);
delay(1000);
srv.write(90);
delay(1000);
srv.detach();
}
void loop() {
// Cod avertizare
// Blink lent
// for (int i = 0; i < 10; i++)
// {
// digitalWrite(13, 1);
// delay(200);
// digitalWrite(13, 0);
// delay(200);
// }
// Blink rapid. Scoateți cablul USB!!!!
// for (int i = 0; i < 10; i++)
// {
// digitalWrite(13, 1);
// delay(100);
// digitalWrite(13, 0);
// delay(100);
// }
// digitalWrite(13, 1);
// Pornirea motorului Servo
//playWithServo(8);
//spate(255, 500);
Incoming_value = 'S';
// if (BTserial.available() > 0) {
//
// Incoming_value = BTserial.read(); //Read the incoming data and store it into variable Incoming_value
// Serial.write(Incoming_value);
// //Print Value of Incoming_value in Serial monitor
// // Serial.print("\n");
// }//New line
delay(500);
//srv.write(90);
if (Serial.available() > 0) {
Incoming_value = Serial.read(); //Read the incoming data and store it into variable Incoming_value
Serial.write(Incoming_value);
//Print Value of Incoming_value in Serial monitor
// Serial.print("\n");
if (Incoming_value == 'F') { //Checks whether value of Incoming_value is equal to 1
digitalWrite(13, HIGH); //If value is 1 then LED turns ON
Serial.println(distanta());
if (distanta() > 20)
fata(128, 325);
}
else if (Incoming_value == 'B') { //Checks whether value of Incoming_value is equal to 0
digitalWrite(13, LOW); //If value is 0 then LED turns OFF
spate(128, 250);
}
else if (Incoming_value == 'L') {
digitalWrite(13, LOW);
srv.write(180);
delay(700);
if (distanta() > 20 ) {
stanga(128, 400);
delay(100);
fata(128, 250);
}
srv.write(90);
}
else if (Incoming_value == 'R') {
srv.write(0);
delay(700);
if (distanta() > 20 ) {
dreapta(128, 400);
delay(100);
fata(128, 250);
}
srv.write(90);
}
else if (Incoming_value == 'O') {
srv.write(45);
delay(700);
if (distanta() > 20 ) {
dreapta(128, 250);
delay(100);
fata(128, 250);
}
srv.write(90);
}
else if (Incoming_value == 'N') {
digitalWrite(13, LOW);
srv.write(135);
delay(700);
if (distanta() > 20 ) {
stanga(128, 250);
delay(100);
fata(128, 250);
}
srv.write(90);
}
}
//250 ms - 45 grade
//400 ms - 90 grade
//128, 250 - 10cm fata
// delay(5000);
// dreapta(128, 400);
// delay(1000);
// Serial.println(distanta());
// if (distanta() > 20)
// fata(128, 250);
//
// srv.write(0);
// delay(1000);
// if (distanta() > 20 ) {
// dreapta(128, 400);
// delay(100);
// fata(128, 250);
// }
// srv.write(90);
}
int distanta() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
int duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
int cm = (duration / 2) / 29.1;
return cm;
}
void fata(int x, int del) {
//Fata
StartMotor (mpin00, mpin01, 1, x);
StartMotor (mpin10, mpin11, 1, x);
delay (del);
delayStopped(20);
}
void spate(int x, int del) {
//Fata
StartMotor (mpin00, mpin01, 0, x);
StartMotor (mpin10, mpin11, 0, x);
delay (del);
delayStopped(20);
}
void stanga(int x, int del) {
//Fata
StartMotor (mpin00, mpin01, 0, x);
StartMotor (mpin10, mpin11, 1, x);
delay (del);
delayStopped(20);
}
void dreapta(int x, int del) {
//Fata
StartMotor (mpin00, mpin01, 1, x);
StartMotor (mpin10, mpin11, 0, x);
delay (del);
delayStopped(20);
}