Programme_arduino_robot
4.62 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
#include <SoftwareSerial.h>
SoftwareSerial soft2(8,9);
int pwma = 10;
int pwmb = 11;
int ain1 = 7;
int ain2 = 6;
int bin1 = 5;
int bin2 = 4;
int stby = 3;
int TRIG = A1;
int ECHO = A0;
long cm;
long lect_echo;
int envoi=0;
void setup() {
Serial.begin(9600);
soft2.begin(38400);
pinMode(TRIG,OUTPUT);
digitalWrite(TRIG, LOW);
pinMode(ECHO,INPUT);
}
void loop() {
String msg;
char inByte='a';
int i = 0;
int nb_if = 0;
char id_sauv;
int test_if;
int nb_boucles[6];
char id_boucles[6];
int boucle_rec = 0;
int capteur;
//boucle dans une boucle gérée
/* soft2.listen();
while(soft2.available()>0 && inByte !='\n'){
inByte=soft2.read();
msg+=inByte;
delay(10);
i++;
envoi=1;
}*/
if(envoi==0){
Serial.println("etape 0");
delay(20000);
msg += (char)0x10;
msg += (char)0x61;
msg += (char)0x0F;
msg += (char)0x20;
msg += (char)0x10;
msg += (char)0x70;
msg += (char)0x10;
msg += (char)0x61;
msg += (char)0x0F;
msg += (char)0x40;
msg += (char)0x04;
msg += (char)0x30;
msg += (char)0x50;
msg += (char)0x70;
i = 14;
envoi=1;
Serial.println("etape 1");
}
//++a01z++a++z test if
//++B 1Q+z test while (executé 32 fois)
//++B 1B +Q1Q+z test while dans un while
if(envoi==1){
Serial.println("étape 2");
for(int k=0;k<i;k++){
if((msg.charAt(k) & 0xF0) ==0x10){
Serial.print("tout droit\n\r");
digitalWrite(stby,HIGH);
analogWrite(pwma,100);
analogWrite(pwmb,100);
digitalWrite(ain1,HIGH);
digitalWrite(bin1,HIGH);
digitalWrite(ain2,LOW);
digitalWrite(bin2,LOW);
delay(1000);
}
else if((msg.charAt(k) & 0xF0) ==0x20){
Serial.print("à gauche\n\r");
digitalWrite(stby,HIGH);
analogWrite(pwma,100);
analogWrite(pwmb,100);
digitalWrite(ain1,HIGH);
digitalWrite(bin1,LOW);
digitalWrite(ain2,LOW);
digitalWrite(bin2,HIGH);
delay(900);
}
else if((msg.charAt(k) & 0xF0) ==0x30){
Serial.print("à droite\n\r");
digitalWrite(stby,HIGH);
analogWrite(pwma,100);
analogWrite(pwmb,100);
digitalWrite(ain1,LOW);
digitalWrite(bin1,HIGH);
digitalWrite(ain2,HIGH);
digitalWrite(bin2,LOW);
delay(900);
}
else if((msg.charAt(k) & 0xF0) ==0x40){
Serial.print("début while\n\r");
k++;
nb_boucles[boucle_rec] = (int)msg.charAt(k);
id_boucles[boucle_rec] = msg.charAt(k-1);
boucle_rec++;
}
else if((msg.charAt(k) & 0xF0) ==0x50){
nb_boucles[boucle_rec-1]--;
if(nb_boucles[boucle_rec-1]!=0){
k--;
while(msg.charAt(k)!= id_boucles[boucle_rec-1]){
k--;
}
k++;
}
else{
boucle_rec--;
Serial.print("fin while\n\r");
}
}
else if((msg.charAt(k) & 0xF0) ==0x60){
Serial.print("if\n\r");
digitalWrite(TRIG, HIGH);
delayMicroseconds(50);
digitalWrite(TRIG, LOW);
lect_echo = pulseIn(ECHO, HIGH);
cm = lect_echo / 58;
Serial.println(cm);
delay(10);
if((msg.charAt(k) & 0x01) == 0x01){
if(msg.charAt(++k) > cm){
nb_if++;
test_if=nb_if;
id_sauv = msg.charAt(k+1);
while(((msg.charAt(k)) & 0xF0) != 0x70 && k<i){
k++;
}
while(test_if!=0){
if(((msg.charAt(k) & 0xF0) == 0x60)){
k++;
test_if--;
}
else k++;
}
}
}
else if((msg.charAt(k) & 0x01) == 0x00){
if(msg.charAt(++k) < cm){
nb_if++;
test_if=nb_if;
id_sauv = msg.charAt(k+1);
while(((msg.charAt(k)) & 0xF0) != 0x70 && k<i){
k++;
}
while(test_if!=0){
if(((msg.charAt(k) & 0xF0) == 0x60)){
k++;
test_if--;
}
else k++;
}
}
}
}
else if((msg.charAt(k) & 0xF0) ==0x70){
Serial.print("fin parcours\n\r");
envoi=2;
digitalWrite(stby,LOW);
analogWrite(pwma,0);
analogWrite(pwmb,0);
digitalWrite(ain1,LOW);
digitalWrite(bin1,LOW);
digitalWrite(ain2,LOW);
digitalWrite(bin2,LOW);
k=i;
}
}
}
}
/*- Tout droit: 0001
- Virage à gauche: 0010
- Virage à droite: 0011
- Début while: 0100
- Fin while: 0101
- If: 0110
- Fin parcours: 0111*/