-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJunkybox_Wheel_code.ino
196 lines (182 loc) · 4.63 KB
/
Junkybox_Wheel_code.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
196
// Licenced by : Pineapplem3 Inc. | Compiled and Programmed by : NIRMAL RAM
int trigger = 3;
int echo = 2;
int RMP = 10, RMN = 11, LMP = 7, LMN = 6;
void setup() {
pinMode(trigger, OUTPUT); // triggger is output
pinMode(echo, INPUT); // echo is input
pinMode(RMP, OUTPUT);
pinMode(RMN, OUTPUT);
pinMode(LMN, OUTPUT);
pinMode(LMP, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
Serial.begin(9600);
}
void left() {
Serial.println("moving left for 5 second" );
Serial.println("bluetooth working");
digitalWrite(RMP, HIGH);
digitalWrite(RMN, LOW);
digitalWrite(LMP, LOW);
digitalWrite(LMN, HIGH);
}
void right() {
Serial.println("moving Right for 5 second" );
Serial.println("bluetooth working");
digitalWrite(RMP, LOW);
digitalWrite(RMN, HIGH);
digitalWrite(LMP, HIGH);
digitalWrite(LMN, LOW);
}
void backward() {
Serial.println("moving Backwards for 5 second" );
//RIGHT MOTION
digitalWrite(RMP, LOW);
digitalWrite(RMN, HIGH);
digitalWrite(LMP, LOW);
digitalWrite(LMN, HIGH);
}
void forward()
{
Serial.println("moving forward for 5 second" );
Serial.println("bluetooth working");
//LEFT MOTION
digitalWrite(RMP, HIGH);
digitalWrite(RMN, LOW);
digitalWrite(LMP, HIGH);
digitalWrite(LMN, LOW);
}
void stopme()
{
Serial.println("stop command" );
digitalWrite(RMP, LOW);
digitalWrite(RMN, LOW);
digitalWrite(LMP, LOW);
digitalWrite(LMN, LOW);
}
void loop()
{
digitalWrite(trigger, LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(5); // will send waves of 2 ms low wave & 5 ms high wave
long x = pulseIn(echo, HIGH);
int y = x / (29 * 2);
Serial.print(" cm ");
Serial.println(y);
delay(1000);
if (y < 60) {
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
stopme();
}
if (Serial.available())
{
char c = Serial.read();
if (c == 'l' || c == 'L')
{
digitalWrite(trigger, LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(5); // will send waves of 2 ms low wave & 5 ms high wave
long x = pulseIn(echo, HIGH);
int y = x / (29 * 2);
Serial.print(" cm ");
Serial.println(y);
delay(1000);
if (y < 60) {
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
stopme();
delay(500);
backward();
delay(2000);
}
else {
left();
digitalWrite(5, LOW);
digitalWrite(4, LOW);
}
}
else if (c == 'r' || c == 'R')
{
digitalWrite(trigger, LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(5); // will send waves of 2 ms low wave & 5 ms high wave
long x = pulseIn(echo, HIGH);
int y = x / (29 * 2);
Serial.print(" cm ");
Serial.println(y);
delay(1000);
if (y < 60) {
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
stopme();
delay(500);
backward();
delay(2000);
}
else {
right();
digitalWrite(5, LOW);
digitalWrite(4, LOW);
}
}
else if (c == 'F' || c == 'f')
{
digitalWrite(trigger, LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(5); // will send waves of 2 ms low wave & 5 ms high wave
long x = pulseIn(echo, HIGH);
int y = x / (29 * 2);
Serial.print(" cm ");
Serial.println(y);
delay(1000);
if (y < 60) {
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
stopme();
delay(500);
backward();
delay(2000);
}
else {
forward();
digitalWrite(5, LOW);
digitalWrite(4, LOW);
}
}
else if (c == 'b' || c == 'B')
{
digitalWrite(trigger, LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(5); // will send waves of 2 ms low wave & 5 ms high wave
long x = pulseIn(echo, HIGH);
int y = x / (29 * 2);
Serial.print(" cm ");
Serial.println(y);
delay(1000);
if (y < 60) {
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
stopme();
delay(500);
backward();
delay(2000);
}
else {
backward();
digitalWrite(5, LOW);
digitalWrite(4, LOW);
}
}
else if (c == 'S' || c == 's')
{
stopme();
}
}
}