-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.ide
232 lines (187 loc) · 5.03 KB
/
bot.ide
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// C Code for arduino working as a slave in master slave operation
#include<Servo.h>
#include<String.h>
#include<SoftwareSerial.h>
SoftwareSerial mySerial(0,1);
//starting from our input source
String left1[2]={"name1","name2"};
String left2[5]={"name3","name4","name5","name6","name7"};
String right1[3]={"name8","name9",""};
String right2[3]={"A","B","C"};
//variables initialization---------------------------------------
String message;
String source,destination;
char character;
const int trigger1 = 11; //Trigger pin of 1st Sesnor
const int echo1 = 12; //Echo pin of 1st Sesnor
const int trigger2 = 2; //Trigger pin of 2nd Sesnor
const int echo2 = 4;//Echo pin of 2nd Sesnor
const int trigger = 7; //Trigger pin of 2nd Sesnor
const int echo = 8;//Echo pin of 2nd Sesnor
const int lefthigh=10;
const int leftlow=6;
const int righthigh=5;
const int rightlow=3;
int check=0;
long time_taken;
int midist,dist,distL,distR,distF;
int src,dest,flag=0,flag2=0;
//setup-------------------------------------------------------------
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
mySerial.begin(38400);
pinMode(lefthigh,OUTPUT);
pinMode(righthigh,OUTPUT);
pinMode(leftlow,OUTPUT);
pinMode(rightlow,OUTPUT);
pinMode(trigger1, OUTPUT);
pinMode(echo1, INPUT);
pinMode(trigger2, OUTPUT);
pinMode(echo2, INPUT);
}
//side distance calculation-----------------------------------------------------------
int calculate_distance(int trigger, int echo)
{
digitalWrite(trigger, LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
//delayMicroseconds(10);
//digitalWrite(trigger, LOW);
time_taken = pulseIn(echo, HIGH);
dist= time_taken*0.034/2;
if (dist>100)
dist = 100;
return dist;
}
//source and destination index finding---------------------------------
void index_search(){
int i=0;
int flag=0,flag2=0,src,dest;
for(i=0;i<10;i++) //checking array 1 for src and dest
{
if(source.equals(left1[i]))
{
src=i ;
flag=1;
}
if(destination.equals(left1[i]))
{
dest=i ;
flag2=1;
}
}
if(flag==0 || flag2==0){
for(i=0;i<10;i++) //checking array 2 for src and dest
{
if(flag==0){
if(source.equals(left2[i]))
{
src=i;
flag=2;
}
}
if (flag2==0){
if(destination.equals(left1[i]))
{
dest=i; ;
flag2=2;
}
}
}
}
if(flag==0 || flag2==0){
for(i=0;i<10;i++) //checking array 3 for src and dest
{
if(flag==0){
if(source.equals(right1[i]))
{
src=i;
flag=3;
}
}
if (flag2==0){
if(destination.equals(left1[i]))
{
dest=i ;
flag2=3;
}
}}
}
if(flag==0 || flag2==0){
for(i=0;i<10;i++) //checking array 4 for src and dest
{
if(flag==0){
if(source.equals(right2[i]))
{
src=i ;
flag=4;
}
}
if (flag2==0){
if(destination.equals(left1[i]))
{
dest=i;
flag2=4;
}}
}
}
}
//move forward---------------------------------------------------
void forward()
{
analogWrite(lefthigh,255);
analogWrite(leftlow,0);
analogWrite(righthigh,100);
analogWrite(rightlow,0);
}
//halt---------------------------------------------------------------
void halt(){
analogWrite(lefthigh,0);
analogWrite(leftlow,0);
analogWrite(righthigh,0);
analogWrite(rightlow,0);
}
//thodasaright--------------------------------------------------------
void thodasideright(){
analogWrite(lefthigh,225);
analogWrite(leftlow,0);
analogWrite(righthigh,120);
analogWrite(rightlow,0);
}
//thodasaleft-----------------------------------------------------------------
void thodasideleft(){
analogWrite(lefthigh,255);
analogWrite(leftlow,0);
analogWrite(righthigh,40);
analogWrite(rightlow,0);
}
// loop-----------------------------------------------------------------------
void loop() {
//source and destination input from phone or manually-------------------------
source="";
destination="";
// roll_pitch(check);
// check++;
// roll_pitch(check);
// check++;
// Serial.print(destination);
// destination = roll_pitch();
// Serial.println(destination);
index_search();
distL = calculate_distance(11,12);
distR = calculate_distance(2,4);
distF = calculate_distance(7,8);
// Halt if our sensor sence and obstacle
if(distF<50)
halt();
midist=(distL+distR)/2;
// Otherwise move forward
if(midist-10<distL<midist+10)
forward();
Serial.println(distF);
if(distL<midist-10)
thodasideright();
if(distR<midist-10)
thodasideleft();
}