-
Notifications
You must be signed in to change notification settings - Fork 0
/
IR_Manual.ino
228 lines (196 loc) · 4.55 KB
/
IR_Manual.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
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
/*
IR controlled car with an option for automatic
movement based on the distance from the fron of
the car, with the preferance of moving forward
of towards the left.
*/
#include "Ultrasonic.h"
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
// set pins
#define IN1_PIN 2
#define IN2_PIN 3
#define IN3_PIN 4
#define IN4_PIN 5
#define Buzzer 11
#define IR 10
#define R 7
#define G 8
#define B 9
Ultrasonic ultrasonic (13,12); // trg echo
LiquidCrystal_I2C lcd(0x27,16,2); //could be 3F
int dist,t1=0,t2=0;
bool flag = true, start = true;
IRrecv irrecv(IR);
decode_results results;
unsigned long key_value = 0;
void setup (){
lcd.init();
lcd.backlight();
Serial.begin(9600);
//set up pins
pinMode(IN1_PIN,OUTPUT);
pinMode(IN2_PIN,OUTPUT);
pinMode(IN3_PIN,OUTPUT);
pinMode(IN4_PIN,OUTPUT);
pinMode(Buzzer,OUTPUT);
pinMode(R,OUTPUT);
pinMode(G,OUTPUT);
pinMode(B,OUTPUT);
irrecv.enableIRIn();
Serial.println("Robot initialize");
lcd.setCursor(0,0);
lcd.print("Robot initialize");
delay(1500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("distance: CM");
}
void buzz(){
for (int j = 0; j < 2; j++){
digitalWrite(Buzzer, HIGH);
delay (300);
digitalWrite(Buzzer, LOW);
delay (300);
}
}
// -----------------------------------------------------------
// driving function that also change the color of a lightbulb
// based on which direction it is going.
void forward(){
digitalWrite(IN1_PIN, LOW);
digitalWrite(IN2_PIN, HIGH);
digitalWrite(IN3_PIN, LOW);
digitalWrite(IN4_PIN, HIGH);
analogWrite(R,0);
analogWrite(G,255);
analogWrite(B,0);
delay(50);
}
void backward(){
digitalWrite(IN1_PIN, HIGH);
digitalWrite(IN2_PIN, LOW);
digitalWrite(IN3_PIN, HIGH);
digitalWrite(IN4_PIN, LOW);
analogWrite(R,255);
analogWrite(G,0);
analogWrite(B,0);
buzz();
delay(50);
}
void left(){
digitalWrite(IN1_PIN, LOW);
digitalWrite(IN2_PIN, LOW);
digitalWrite(IN3_PIN, HIGH);
digitalWrite(IN4_PIN, LOW);
analogWrite(R,255);
analogWrite(G,255);
analogWrite(B,255);
delay(50);
}
void right() {
digitalWrite(IN1_PIN, HIGH);
digitalWrite(IN2_PIN, LOW);
digitalWrite(IN3_PIN, LOW);
digitalWrite(IN4_PIN, LOW);
analogWrite(R,0);
analogWrite(G,0);
analogWrite(B,255);
delay(50);
}
void STOP(){
digitalWrite(IN1_PIN, LOW);
digitalWrite(IN2_PIN, LOW);
digitalWrite(IN3_PIN, LOW);
digitalWrite(IN4_PIN, LOW);
digitalWrite(R,0);
digitalWrite(G,0);
digitalWrite(B,0);
delay(50);
}
//---------------------------------------------------------
// automatic driving function
void Auto(int dist)
{
if(dist >=25)
{
forward();
}
else
{
STOP();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Obstacle ahead");
lcd.setCursor(0,1);
lcd.print("changing route");
backward();
delay(100);
left();
delay(500);
flag = false;
}
}
//----------------------------------------------------------
void loop (){
// the timing is to prevent incrorrect clicks from IR
// and to be able to fully read the distance on the lcd monitor.
if (start || (t2-t1 >= 5))
{
dist = ultrasonic.read(CM);
t1 = millis()/100;
if(!flag)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("distance: CM");
}
else
{
lcd.setCursor(9,0);
lcd.print(" ");
}
lcd.setCursor(9,0);
lcd.print(dist);
start = false;
}
else
{
t2 = millis()/100;
}
if (irrecv.decode(&results))
{
// to prevent incorrectly decoded clicks from IR
if(!(results.value == 0xFFFFFFFF || results.value >= 0xFFFFFF || results.value == 0xFF))
key_value = results.value;
// write what was decoded as well as what is currenctly saved
// as a way to make sure the information is correct
Serial.print(results.value , HEX);
Serial.print(" key: ");
Serial.println( key_value , HEX);
irrecv.resume();
}
// choose operation based on IR key.
switch (key_value)
{
case 0xFF18E7: //forward
forward();
break;
case 0xFF5AA5: //right
right();
break;
case 0xFF4AB5: //backward
backward();
break;
case 0xFF10EF: //left
left();
break;
case 0xFF38C7: //stop (ok)
STOP();
break;
case 0xFF6897: //auto (*)
Auto(dist);
break;
}
}