-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuggy2.ino
195 lines (177 loc) · 3.9 KB
/
Buggy2.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
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
long previousMillisU = millis();
long intervalU = 500;
#include<NewPing.h>
#include <NewPing.h>
#define trigPin 13
#define echoPin 12
#define MAX_DISTANCE 200
NewPing Sonar(trigPin,echoPin, MAX_DISTANCE);
int ir1 = A0;
int ir2 = A1;
int pin = 4;
int pin5 = 5;
int pin6 = 6;
int pin7 = 7;
int pin8 = 8;
int flag = 0;
int count = 0;
bool flg=true;
long st = millis(), endt;
unsigned value;
int l, r;
void setup() {
// put your setup code here, to run once:
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
pinMode(pin7, OUTPUT);
pinMode(pin8, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(flag==0)
{
if (Serial.available()>0)
{
char s=Serial.read();
switch (s)
{
case 'x':
{
flag=1;
}
}
}
}
if (flag==1)
{
if (digitalRead(pin)>0)
{
value=pulseIn(pin,HIGH);
Serial.print("gantry value = ");
Serial.println(value);
if(value >2000 && value <3000)
{
Serial.println("Gantry 1 Crossed");
stopp();
delay(500);
}
else if(value >500 && value <1000)
{
Serial.println("Gantry 2 Crossed");
stopp();
delay(500);
}
else if(value >3000 && value <4000)
{
Serial.println("Gantry 3 Crossed");
stopp();
delay(500);
}
}
unsigned long currentMillisU = millis();
if(currentMillisU - previousMillisU > intervalU)
{
previousMillisU = currentMillisU;
detectObstacle();
}
l = digitalRead(ir1);
r = digitalRead(ir2);
if (l == HIGH && r == HIGH)
{
forward();
}
else if (l == LOW && r == HIGH)
{
left();
}
else if (l == HIGH && r == LOW)
{
right();
}
else if (l == LOW && r==LOW)
{
forward();
endt= millis();
if(endt - st > 800)
{
Serial.print("st = ");
Serial.println(st);
Serial.print("endt = ");
Serial.println(endt);
Serial.print("Difference ");
Serial.println(endt-st);
count++;
st=millis();
}
if(count == 1)
{
Serial.print("count = ");
Serial.print(count);
forward();
}
if(count == 2)
{
Serial.print("count = ");
Serial.print(count);
right();
delay(20);
}
if(count == 4)
{
Serial.print("count = ");
Serial.print(count);
right();
delay(30);
}
if(count >= 6)
{
stopp();
delay(100000);
Serial.print("Stop");
}
}
}
}
void forward()
{
digitalWrite(pin5, HIGH);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, HIGH);
}
void left()
{
digitalWrite(pin5, HIGH);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
}
void right()
{
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, HIGH);
}
void stopp()
{
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
}
void detectObstacle()
{
delay(50);
unsigned int distanceCm;
distanceCm = Sonar.ping_cm();
pinMode(echoPin,OUTPUT);
digitalWrite(echoPin,LOW);
pinMode(echoPin,INPUT);
if((distanceCm<15) && (distanceCm>0))
{
stopp();
delay(1000);
}
}