-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
145 lines (129 loc) · 2.35 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
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
#include <Wire.h>
#include <Adafruit_VL53L0X.h>
Adafruit_VL53L0X sensor = Adafruit_VL53L0X();
int Count = 0;
int distance;
//Motor Pins
int in1 = 6;
int in2 = 9;
int in3 = 11;
int in4 = 10;
//IR Pins
int IRSensorL = 2;
int IRSensorR = 3;
int IRL;
int IRR;
void Stop()
{
analogWrite(in1, 0);
analogWrite(in2, 0);
analogWrite(in3, 0);
analogWrite(in4, 0);
Serial.println("Stop");
}
void North()
{
int speed = 70;
analogWrite(in1, speed);
analogWrite(in2, 0);
analogWrite(in3, speed-7);
analogWrite(in4, 0);
Serial.println("North");
}
void East()
{
int speed = 50;
analogWrite(in1, 0);
analogWrite(in2, speed);
analogWrite(in3, speed-5);
analogWrite(in4, 0);
Serial.println("East");
}
void South()
{
int speed = 70;
analogWrite(in1, 0);
analogWrite(in2, speed);
analogWrite(in3, 0);
analogWrite(in4, speed-7);
Serial.println("South");
}
void West()
{
int speed = 50;
analogWrite(in1, speed);
analogWrite(in2, 0);
analogWrite(in3, 0);
analogWrite(in4, speed-5);
Serial.println("West");
}
void handleInterrupt()
{
IRL = digitalRead(IRSensorL);
IRR = digitalRead(IRSensorR);
while (IRL==0 || IRR==0)
{
IRL = digitalRead(IRSensorL);
IRR = digitalRead(IRSensorR);
if (IRL==1 && IRR==1)
{
break;
}
else if (IRL==0 && IRR==1)
{
Count = 1;
South();
West();
}
else if (IRL==1 && IRR==0)
{
Count = 0;
South();
East();
}
else if (IRL==0 && IRR==0)
{
South();
West();
}
}
}
void setup()
{
Serial.begin(9600);
Wire.begin();
if (!sensor.begin())
{
Serial.println("Failed to initialize VL53L0X sensor!");
while (1);
}
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(IRSensorL, INPUT);
pinMode(IRSensorR, INPUT);
attachInterrupt(digitalPinToInterrupt(IRSensorL), handleInterrupt, CHANGE);
attachInterrupt(digitalPinToInterrupt(IRSensorR), handleInterrupt, CHANGE);
}
void loop()
{
VL53L0X_RangingMeasurementData_t measure
sensor.rangingTest(&measure, false);
if (measure.RangeStatus != 4) // check for a valid measurement
{
distance = measure.RangeMilliMeter;
if (distance<=1300)
{
North();
}
else if (Count == 1)
{
West();
}
else if (Count == 0)
{
East();
}
}
}