-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
66 lines (58 loc) · 1.09 KB
/
main.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
#include<Servo.h>
Servo srv;
Servo srv1;
Servo control_servo;
const int distance_threshold = 100;
int motorSpeed;
int distance;
int left;
int right;
//Retorna a distância em centímetros ao objeto
int measure(){
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
int d=pulseIn(12,HIGH);
d=d/29/2;
return d;
}
void setup()
{
//inicializar pins do sensor
pinMode(13, OUTPUT);
pinMode(12, INPUT);
//inicializar pins dos servos
srv.attach(7);
srv1.attach(6);
control_servo.attach(5);
}
void loop()
{
control_servo.write(90);
distance = measure();
if(distance<=distance_threshold)
{
srv.write(93);
srv1.write(93);
control_servo.write(160);
delay(3000);
left = measure();
control_servo.write(20);
delay(3000);
right = measure();
if(right>=left){
srv.write(120);
}else{
srv1.write(60);
}
delay(150);
}else{
motorSpeed = map(distance, distance_threshold, 334, 93,150 );
srv.write(motorSpeed);
srv1.write(180-motorSpeed);
}
delay(3000);
delay(100);
}