Skip to content

Commit b0aea4c

Browse files
authored
Create maze_solver_robot_v2.ino
1 parent 3d5b1e4 commit b0aea4c

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <Wire.h>
2+
3+
#define trigPin 15
4+
#define echoPin 14
5+
#define MAX_DISTANCE 400
6+
7+
long distance = 0;
8+
long duration = 0;
9+
10+
int hcsr(){
11+
long dis, dur;
12+
digitalWrite(trigPin, LOW);
13+
delayMicroseconds(2);
14+
digitalWrite(trigPin, HIGH);
15+
delayMicroseconds(10);
16+
digitalWrite(trigPin, LOW);
17+
dur = pulseIn(echoPin, HIGH);
18+
dis = (dur/2) / 29.1;
19+
return dis;
20+
}
21+
22+
void setup() {
23+
24+
}
25+
26+
void loop() {
27+
delay(50);
28+
distance = hcsr();
29+
Forward();
30+
if(distance<5){
31+
Stop();
32+
delay(1000);
33+
Right();
34+
delay(1000);
35+
distance = hcsr();
36+
if(distance < 5){
37+
Stop();
38+
delay(1000);
39+
Left();
40+
delay(500);
41+
// If the distance is less than 5, wait, turn right; if the distance is less than 5 again, move in the opposite direction
42+
}
43+
}
44+
}
45+
46+
void dc(int dcNumber, int speed, int direction){
47+
Wire.beginTransmission(0x22);
48+
Wire.write(0x26);
49+
Wire.write(dcNumber);
50+
Wire.write(speed);
51+
Wire.write(direction);
52+
int cs = dcNumber ^ speed ^ direction;
53+
Wire.write(cs);
54+
Wire.endTransmission();
55+
}
56+
57+
void Forward(){ //if the distance is higher than 5, go straight
58+
dc(1,255,1);
59+
dc(2,255,1);
60+
}
61+
62+
void Left(){ //turn left
63+
dc(1,0,1);
64+
dc(2,255,1);
65+
}
66+
67+
void Right(){ //turn right
68+
dc(1,255,1);
69+
dc(2,0,1);
70+
}
71+
72+
void Stop(){
73+
dc(1,0,1);
74+
dc(2,0,1);
75+
}

0 commit comments

Comments
 (0)