-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArduinoRobot.ino
223 lines (174 loc) · 4.66 KB
/
ArduinoRobot.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
#include "IRLibAll.h"
#include <IRLibDecodeBase.h> //We need both the coding and
#include <IRLibSendBase.h>
#include <IRLib_P01_NEC.h>
#include <IRLibCombo.h>
#include <IRLibRecv.h>
#include <IRLibRecvPCI.h>
/*
* CODE UNTESTED BUT COMPLETE
*
*/
//L293D Motor Driver
//Motor A
const int motorPin1 = 9; // Pin 14 of L293
const int motorPin2 = 10; // Pin 10 of L293
//Motor B
const int motorPin3 = 6; // Pin 7 of L293
const int motorPin4 = 5; // Pin 2 of L293
int motorSpeed = 180;
char rxChar= 0; // RXcHAR holds the received command.
boolean speedBoost = false;
boolean shield = false;
//#define SONY 2
//Create a receiver object to listen on pin 2
IRrecvPCI myReceiver(2);
// Create a sender object tied to pin 3
IRsend mySender;
//Create a decoder object
IRdecode myDecoder;
//Pins and state for tilt switch
//const int tiltPin = 8;
//int tiltState = 0;
//int prevTiltState = 0;
//Number of times code has looped
//This will run only one time.
void setup(){
Serial.begin(9600); // Open serial port (9600 bauds).
//Delay to allow initialization of Serial connection
delay(3000);
//Sets motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
//Sets tilt switch pin as output
// pinMode(tiltPin, INPUT);
myReceiver.enableIRIn(); // enable receiver
//Serial.write("AT+NAME ");
//Sends message to app stating its ready
Serial.println("r#");
}
void forward(){
//This code will make the bot move forward
checkBoost();
analogWrite(motorPin1, motorSpeed);
analogWrite(motorPin2, 0);
analogWrite(motorPin3, 0);
analogWrite(motorPin4, motorSpeed);
}
void backwards(){
//This code will make the bot move backwards
checkBoost();
analogWrite(motorPin1, 0);
analogWrite(motorPin2, motorSpeed);
analogWrite(motorPin3, motorSpeed);
analogWrite(motorPin4, 0);
}
void left(){
//This code will make the bot move left
checkBoost();
analogWrite(motorPin1, 0);
analogWrite(motorPin2, motorSpeed);
analogWrite(motorPin3, 0);
analogWrite(motorPin4, motorSpeed);
}
void right(){
//This code will make the bot move right
checkBoost();
analogWrite(motorPin1, motorSpeed);
analogWrite(motorPin2, 0);
analogWrite(motorPin3, motorSpeed);
analogWrite(motorPin4, 0);
}
void stopBot(){
//And this code will stop motors
analogWrite(motorPin1, 0);
analogWrite(motorPin2, 0);
analogWrite(motorPin3, 0);
analogWrite(motorPin4, 0);
}
void shoot(){
mySender.send(NEC,0x61a0f00f,0); //Sends signal via IR Blaster
myReceiver.enableIRIn(); // Re-enable receiver
Serial.println("Code Sent");
}
void robotDead(){
stopBot();
Serial.println("k#");
Serial.end();
}
void checkBoost(){
if(speedBoost){
motorSpeed = 255;
}else{
motorSpeed = 180;
}
} //end checkboost
void loop(){
//Read state of tilt switch
//tiltState = digitalRead(tiltPin);
myReceiver.enableIRIn(); //Enable Receiver
//disable IR receiver for sheild buff myReceiver.disableIRIn();
//if(tiltState != prevTiltState && (cycle > 1)){
// robotDead();
//}
//Listen for serial Input
if (Serial.available() >0){ // Check receive buffer.
rxChar = Serial.read(); // Save character received.
Serial.flush();
//Check char and make decision
switch(rxChar){
case 'w':
forward();
delay(10);
break;
case 'a':
left();
delay(10);
break;
case 'd':
right();
delay(10);
break;
case 's':
backwards();
delay(10);
break;
case'e':
shoot();
delay(10);
break;
case 'i':
speedBoost = true;
break;
case 'I':
speedBoost = false;
case 'o':
shield = true;
break;
case 'O':
shield = false;
break;
case 'q':
stopBot();
checkBoost();
}
}//End Serial Listener
//signal received robot shot
if (myReceiver.getResults()) {
//only shoot if robot does not have shield enabled
if(shield == false){
myDecoder.decode(); //Decode it
//Serial.println("Code Received");
//myDecoder.dumpResults(false); //Now print results. Use false for less detail
//`Serial.println(myDecoder.value);
if(myDecoder.value == 1637937167){
Serial.println("h#");
}
myReceiver.enableIRIn(); //Restart receiver
}
}
//All code has run and cycle is restarting
//Serial.println("Cycle#");
}