-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathesperot_bluetooth_intelligente.ino
More file actions
376 lines (338 loc) · 8.2 KB
/
esperot_bluetooth_intelligente.ino
File metadata and controls
376 lines (338 loc) · 8.2 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//QUESTA VERSIONE FUNZIONA COL SERIALE
#define MOTORLATCH 12
#define MOTORCLK 4
#define MOTORENABLE 7
#define MOTORDATA 8
#define MOTOR1_A 2
#define MOTOR1_B 3
#define MOTOR2_A 1
#define MOTOR2_B 4
#define MOTOR3_A 5
#define MOTOR3_B 7
#define MOTOR4_A 0
#define MOTOR4_B 6
#define MOTOR1_PWM 11
#define MOTOR2_PWM 3
#define MOTOR3_PWM 6
#define MOTOR4_PWM 5
#define FORWARD 1//valori funzione di base
#define BACKWARD 2
#define BRAKE 3
#define RIGHT 4//valori per switch
#define LEFT 5
#include <SoftwareSerial.h>//bluetooth
#define BT_TX_PIN A3
#define BT_RX_PIN A4
SoftwareSerial bt = SoftwareSerial(BT_RX_PIN, BT_TX_PIN);
#define TRIG A0
#define ECHO A1
short dir1;//direzione
short dir2;
double b;
void setup(){
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(BT_RX_PIN, INPUT);
pinMode(BT_TX_PIN, OUTPUT);
Serial.begin(9600);
bt.begin(9600);
Serial.println("Set up completed");
dir1=BRAKE;//direzione
dir2=BRAKE;
}
void loop() {
while(bt.available()>0){
switch (bt.read()){
case 65://avanti
case 97://sia 65 che 97 per maiuscola e minuscola
dir1=FORWARD;
break;
case 68://destra
case 100:
dir1=RIGHT;
break;
case 73://indietro
case 105:
dir1=BACKWARD;
break;
case 83://sinistra
case 115:
dir1=LEFT;
break;
case 85:
for(short i=0;i<18;i++){
bt.read();
delay(10);
}
avvio();
dir1=BRAKE;
dir2=BRAKE;
break;
default:
dir1=BRAKE;
break;
}
while(bt.available()<1){
switch (dir1){
case FORWARD://avanti
if(dir1!=dir2 && dist()>26){
motor(1, BRAKE, 0);
motor(4, BRAKE, 0);
delay(100);
motor(1, FORWARD, 255);
motor(4, FORWARD, 255);
dir2=FORWARD;
}
b=dist();
Serial.println(b);
if(b<15 && b>0 && (b<3.75 || b > 3.90) ){
avvio();
dir1=BRAKE;
dir2=BRAKE;
}
break;
case RIGHT://destra
if(dir1!=dir2){
motor(1, BRAKE, 0);
motor(4, BRAKE, 0);
delay(100);
motor(1, FORWARD, 255);
motor(4, BACKWARD, 255);
dir2=RIGHT;
}
break;
case BACKWARD://indietro
if(dir1!=dir2){
motor(1, BRAKE, 0);
motor(4, BRAKE, 0);
delay(100);
motor(1, BACKWARD, 255);
motor(4, BACKWARD, 255);
dir2=BACKWARD;
}
break;
case LEFT://sinistra
if(dir1!=dir2){
motor(1, BRAKE, 0);
motor(4, BRAKE, 0);
delay(100);
motor(1, BACKWARD, 255);
motor(4, FORWARD, 255);
dir2=LEFT;
}
break;
case BRAKE://sinistra
motor(1, BRAKE, 0);
motor(4, BRAKE, 0);
dir2=BRAKE;
break;
}
}
}
}
void avvio(){
motor(1, BRAKE, 0);
motor(4, BRAKE, 0);
delay(50);
motor(1, BACKWARD, 255);
motor(4, FORWARD, 255);
delay(200);
motor(1, BRAKE, 0);
motor(4, BRAKE, 0);
delay(50);
motor(1, FORWARD, 255);
motor(4, BACKWARD, 255);
delay(400);
motor(1, BRAKE, 0);
motor(4, BRAKE, 0);
delay(50);
motor(1, BACKWARD, 255);
motor(4, FORWARD, 255);
delay(200);
motor(1, BRAKE, 0);
motor(4, BRAKE, 0);
}
double dist(){
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
double a=pulseIn(ECHO, HIGH)/29.1/2;
return a;
}
// Initializing
// ------------
// There is no initialization function.
//
// The shiftWrite() has an automatic initializing.
// The PWM outputs are floating during startup,
// that's okay for the Motor Shield, it stays off.
// Using analogWrite() without pinMode() is valid.
//
// ---------------------------------
// motor
//
// Select the motor (1-4), the command,
// and the speed (0-255).
// The commands are: FORWARD, BACKWARD, BRAKE.
//
void motor(int nMotor, int command, int speed)
{
int motorA, motorB;
if (nMotor >= 1 && nMotor <= 4)
{
switch (nMotor)
{
case 1:
motorA = MOTOR1_A;
motorB = MOTOR1_B;
break;
case 2:
motorA = MOTOR2_A;
motorB = MOTOR2_B;
break;
case 3:
motorA = MOTOR3_A;
motorB = MOTOR3_B;
break;
case 4:
motorA = MOTOR4_A;
motorB = MOTOR4_B;
break;
default:
break;
}
switch (command)
{
case FORWARD:
motor_output (motorA, HIGH, speed);
motor_output (motorB, LOW, -1); // -1: no PWM set
break;
case BACKWARD:
motor_output (motorA, LOW, speed);
motor_output (motorB, HIGH, -1); // -1: no PWM set
break;
case BRAKE:
motor_output (motorA, LOW, 0); // 0: output floating.
motor_output (motorB, LOW, -1); // -1: no PWM set
break;
default:
break;
}
}
}
// ---------------------------------
// motor_output
//
// The function motor_ouput uses the motor driver to
// drive normal outputs like lights, relays, solenoids,
// DC motors (but not in reverse).
//
// It is also used as an internal helper function
// for the motor() function.
//
// The high_low variable should be set 'HIGH'
// to drive lights, etc.
// It can be set 'LOW', to switch it off,
// but also a 'speed' of 0 will switch it off.
//
// The 'speed' sets the PWM for 0...255, and is for
// both pins of the motor output.
// For example, if motor 3 side 'A' is used to for a
// dimmed light at 50% (speed is 128), also the
// motor 3 side 'B' output will be dimmed for 50%.
// Set to 0 for completelty off (high impedance).
// Set to 255 for fully on.
// Special settings for the PWM speed:
// Set to -1 for not setting the PWM at all.
//
void motor_output (int output, int high_low, int speed)
{
int motorPWM;
switch (output)
{
case MOTOR1_A:
case MOTOR1_B:
motorPWM = MOTOR1_PWM;
break;
case MOTOR2_A:
case MOTOR2_B:
motorPWM = MOTOR2_PWM;
break;
case MOTOR3_A:
case MOTOR3_B:
motorPWM = MOTOR3_PWM;
break;
case MOTOR4_A:
case MOTOR4_B:
motorPWM = MOTOR4_PWM;
break;
default:
// Use speed as error flag, -3333 = invalid output.
speed = -3333;
break;
}
if (speed != -3333)
{
// Set the direction with the shift register
// on the MotorShield, even if the speed = -1.
// In that case the direction will be set, but
// not the PWM.
shiftWrite(output, high_low);
// set PWM only if it is valid
if (speed >= 0 && speed <= 255)
{
analogWrite(motorPWM, speed);
}
}
}
// ---------------------------------
// shiftWrite
//
// The parameters are just like digitalWrite().
//
// The output is the pin 0...7 (the pin behind
// the shift register).
// The second parameter is HIGH or LOW.
//
// There is no initialization function.
// Initialization is automatically done at the first
// time it is used.
//
void shiftWrite(int output, int high_low)
{
static int latch_copy;
static int shift_register_initialized = false;
// Do the initialization on the fly,
// at the first time it is used.
if (!shift_register_initialized)
{
// Set pins for shift register to output
pinMode(MOTORLATCH, OUTPUT);
pinMode(MOTORENABLE, OUTPUT);
pinMode(MOTORDATA, OUTPUT);
pinMode(MOTORCLK, OUTPUT);
// Set pins for shift register to default value (low);
digitalWrite(MOTORDATA, LOW);
digitalWrite(MOTORLATCH, LOW);
digitalWrite(MOTORCLK, LOW);
// Enable the shift register, set Enable pin Low.
digitalWrite(MOTORENABLE, LOW);
// start with all outputs (of the shift register) low
latch_copy = 0;
shift_register_initialized = true;
}
// The defines HIGH and LOW are 1 and 0.
// So this is valid.
bitWrite(latch_copy, output, high_low);
// Use the default Arduino 'shiftOut()' function to
// shift the bits with the MOTORCLK as clock pulse.
// The 74HC595 shiftregister wants the MSB first.
// After that, generate a latch pulse with MOTORLATCH.
shiftOut(MOTORDATA, MOTORCLK, MSBFIRST, latch_copy);
delayMicroseconds(5); // For safety, not really needed.
digitalWrite(MOTORLATCH, HIGH);
delayMicroseconds(5); // For safety, not really needed.
digitalWrite(MOTORLATCH, LOW);
}