-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobo.ino
More file actions
171 lines (157 loc) · 3.78 KB
/
Robo.ino
File metadata and controls
171 lines (157 loc) · 3.78 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
#include <SoftwareSerial.h>
// #include <Servo.h>
#include "bluetooth.h"
#include "motor.h"
#include "ultrassom.h"
// #include "obj_servo.h"
Ultrassom *ult = new Ultrassom();
Motor *motor = new Motor();
Bluetooth *blue = new Bluetooth();
// ObjServo *servo = new ObjServo();
int Angulo = 90;
int SERVO_PIN = 4;
void setup(){
Serial.begin(9600);
blue->configuraBluetooth();
motor->setVelocidade(180);
pinMode(SERVO_PIN, OUTPUT);
}
void loop(){
// if(Intervalo20()){
String msg = blue->Ler();
// Serial.println(msg);
if(msg.length() > 1)
lerMsg(msg);
delay(20);
// delay(5000);
// Desviar();
// motor->irParar();
}
void servo(int angulo) {
if (Intervalo20()) {
digitalWrite(SERVO_PIN, HIGH);
delayMicroseconds(700+2100/180*angulo);
digitalWrite(SERVO_PIN, LOW);
}
}
boolean Intervalo500() {
static long time = millis();
if ((int)(millis() - time) >= 500) {
time = millis();
return true;
}
return false;
}
boolean Intervalo20() {
static long time = millis();
if ((int)(millis() - time) >= 20) {
time = millis();
return true;
}
return false;
}
// void chamarFuncao(int cod, int arg1 = 0, int arg2 = 0);
void chamarFuncao(int cod, int arg1){
switch (cod) {
case 1: motor->setVelocidade(arg1); break;
case 2: motor->setVelocidadeMotor1(arg1); break;
case 3: motor->setVelocidadeMotor2(arg1); break;
case 4: motor->irFrente(); break;
case 5: motor->irDireita(); break;
case 6: motor->irDireitaForte(); break;
case 7: motor->irEsquerda(); break;
case 8: motor->irEsquerdaForte(); break;
case 9: motor->irRe(); break;
case 10: motor->irParar(); break;
case 11: ult->setT_pin(arg1); break;
case 12: ult->setE_pin(arg1); break;
case 13: ult->setM_dist(arg1); break;
case 14: retornarDados(arg1, ult->getT_pin()); break;
case 15: retornarDados(arg1, ult->getE_pin()); break;
case 16: retornarDados(arg1, ult->getM_dist()); break;
case 17: retornarDados(arg1, (int)ult->lerDistancia()); break;
case 18: delay(arg1); break;
case 19: retornarDados(arg1, Encruzilhada()); break;
// case 20: servo->setPin(arg1); break;
// case 21: servo->getPin(); break;
case 22: Angulo = arg1; break;
case 23: retornarDados(arg1, EncruzilhadaInvertida()); break;
case 24: retornarDados(arg1, Desviar()); break;
case 666: retornarDados(arg1, 666); break;//funcão ok
}
}
int Encruzilhada(){
motor->irFrente();
delay(1000);
motor->irDireitaForte();
delay(1000);
motor->irFrente();
return 1;
}
int EncruzilhadaInvertida(){
// motor->irFrente();
// delay(1000);
motor->irEsquerdaForte();
delay(1100);
motor->irFrente();
return 1;
}
int Desviar(){
motor->irDireitaForte();
delay(1100);
motor->irFrente();
delay(3000);
motor->irEsquerdaForte();
delay(1500);
motor->irFrente();
delay(4000);
motor->irEsquerdaForte();
delay(1100);
motor->irFrente();
delay(2300);
motor->irDireitaForte();
delay(500);
motor->irFrente();
motor->irParar();
return 1;
}
void retornarDados(int id, int valor){
char Id[5], Val[5], msg[1024];
sprintf(Id, "%d", id);
sprintf(Val, "%d", valor);
strcpy(msg, Id);
strcat(msg, "@");
strcat(msg, Val);
strcat(msg, "#");
// Serial.println(msg);
// Serial.println("########################3");
blue->Enviar(msg);
}
void lerMsg(String msg){
String SB, TB;
int iSB, iTB;
Serial.println(msg);
int i;
switch (msg.charAt(0)) {
case '1':
break;
case '2':
break;
case '3':
for(i = 2; i < msg.length(); i++){
if(msg.charAt(i) == '#')
break;
if(msg.charAt(i) == '@')
for(int k = i+1; k < msg.length(); k++){
if(msg.charAt(k) == '#')
break;
TB += msg.charAt(k);
}
SB += msg.charAt(i);
}
iSB = SB.toInt();
iTB = TB.toInt();
chamarFuncao(iSB, iTB);
break;
}
}