-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuggy1.ino
164 lines (156 loc) · 3.43 KB
/
Buggy1.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
const int tp = 13;
const int ep = 12;
long duration ;
int distance ;
int counter = 0;
unsigned long int st =0 ,ft =0,gST =0,gFT = 0;
int pin=4;
unsigned valueOfSignalGantry ;
////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
// put your setup code here, to run once:
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(tp,OUTPUT);
pinMode(ep,INPUT);
pinMode(A1,INPUT);//l
pinMode(A2,INPUT);//r
pinMode(pin,INPUT);
Serial.begin(9600);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BUGGY MOVEMENT
void forward() {
//digitalWrite(5,HIGH);
analogWrite(5,180);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
//digitalWrite(8,HIGH);
analogWrite(8,180);
}
void backward() {
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
}
void right() {
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
}
void left() {
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
}
void stopf() {
digitalWrite(5, LOW);
digitalWrite (6, LOW);
digitalWrite (7, LOW);
digitalWrite (8, LOW);
}
void clockwise(){
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// GANTRY SIGNAL
void checkGantry(){
// now check for different gantry signals
valueOfSignalGantry=pulseIn(pin,HIGH,3000);
if (valueOfSignalGantry >400 && valueOfSignalGantry<1000){
Serial.println(valueOfSignalGantry);
Serial.println("Gantry 1 ");
stopf();
delay(1000);
forward();
delay(400);
}
// more else if conditions
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// LINE FOLLOWING CODE
void buggy(){
int l,r;
digitalWrite(tp,LOW);
delayMicroseconds(2);
digitalWrite(tp,HIGH);
delayMicroseconds(10);
digitalWrite(tp,LOW);
duration = pulseIn(ep,HIGH,5000);
distance = (duration * 0.034)/2;
// Serial.println(distance);
if (distance <= 15 && distance>0){
stopf();
}
else{
l= digitalRead(A1);
r= digitalRead(A2);
checkGantry();
if(l==1 && r== 0){
right();
}
else if(l==0 && r==1){
left();
}
else if(l ==1 && r== 1){
forward();
}
else if(l == 0 && r== 0){
st = millis();
if(st - 500 > ft){
counter++;
Serial.print("value of counter :");
Serial.println(counter);
ft = millis();
}
if(counter ==1){
forward();
}
else if(counter == 2){
right();
}
else if(counter == 3){
left();
}
//delay kei bad ek bari aur detect kar rahi thi
else if(counter == 4){
// right();
clockwise();
delay(50);
stopf();
delay(10);
}
else if(counter == 6){
stopf();
// exit(0);
delay(5000);
counter=0;
}
}
}
}
char s;
void loop(){
if (Serial.available()>0){
char temp = Serial.read();
if (temp == 'G' || temp == 'S'){
s = temp;
}
// s = Serial.read();
}
if ( s == 'G'){
buggy();
}
else if( s== 'S'){
stopf();
delay(1000);
}
}