-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphysicalBlockly_bottom.ino
248 lines (209 loc) · 5.75 KB
/
physicalBlockly_bottom.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//Bottom Arduino Motors, US sensor, reflectance, IR
//Top Arduino Speaker, RFID
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
#include <QTRSensors.h>
QTRSensors qtr;
//Bi-directinal SPI
//13 SCK (serial clock)
//12 MISO (master in slave out)
//11 MOSI (master out slave in)
//10 SS (slave select)
// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines. Use the values below (2, 3) for the shield!
//RFID module
#define PN532_IRQ (2) //pin 3 of the RJ12 17 (2)
#define PN532_RESET (3) // pin 4 of the RJ12 9 (3)
//Reflectance sensor
const uint8_t SensorCount = 4;//use four sensors
uint16_t sensorValues[SensorCount];
//Motors
int Rmotor= 12;
int RmotorPWM= 13;
int Lmotor= 8;
int LmotorPWM= 5;
int send_data[6];
// Or use this line for a breakout or shield with an I2C connection:
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
//initialize the buffer
int bufSize = 4;
char buf [4];
volatile byte pos = 0;
void setup() {
Serial.begin(115200);
pinMode (MISO, OUTPUT);
SPCR |= bit (SPE); //turn on SPI in slave mode
// pinMode(trigPin, OUTPUT);
// pinMode(echoPin, INPUT);
pinMode(LED, OUTPUT);
//turn on the interrupt
SPI.attachInterrupt();
pos = 0;
for (int i = 2; i < 6; i++){ pinMode(i, INPUT);}
for (int i = 10; i < 18; i++) {pinMode(i, OUTPUT);}
// configure the sensors
qtr.setTypeRC();
qtr.setSensorPins((const uint8_t[]){3, 4, 5, 6}, SensorCount);
//qtr.setEmitterPin(2);
delay(500);
Serial.println("Begin calibrations");
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // turn on Arduino's LED to indicate we are in calibration mode
// 2.5 ms RC read timeout (default) * 10 reads per calibrate() call
// = ~25 ms per calibrate() call.
// Call calibrate() 400 times to make calibration take about 10 seconds.
for (uint16_t i = 0; i < 400; i++)
{
qtr.calibrate();
}
digitalWrite(LED_BUILTIN, LOW); // turn off Arduino's LED to indicate we are through with calibration
// print the calibration minimum values measured when emitters were on
//Serial.begin(9600);
for (uint8_t i = 0; i < SensorCount; i++)
{
Serial.print(qtr.calibrationOn.minimum[i]);
Serial.print(' ');
}
Serial.println();
// print the calibration maximum values measured when emitters were on
for (uint8_t i = 0; i < SensorCount; i++)
{
Serial.print(qtr.calibrationOn.maximum[i]);
Serial.print(' ');
}
Serial.println();
Serial.println();
delay(1000);
pinMode(Rmotor,OUTPUT);
pinMode(Lmotor,OUTPUT);
pinMode(RmotorPWM,OUTPUT);
pinMode(LmotorPWM, OUTPUT);
}
//SPI ISR (Interrupt Service Routine)
ISR (SPI_STC_vect){
byte c = SPDR; //get byte from the SPI data register
//detect the beginning of the buffer, do not put it in the buffer
if (c == '\n'){
valid = true;
}
//detect the end character
else if (c == '\r'){
valid = false;
// buf[0] = 0;
// buf[1] = 0;
pos = 0;
// process_it = true;
}
//put data into the buffer
if ((valid == true) && (c != '\n') && (c != '\r')){
if (pos < bufSize ){ ///sizeof buffer
buf [pos] = c;
pos ++;
}
}
}
void read_IR(){
val = digitalRead(IRPin);
SPDR = val;
Serial.println(val);
}
boolean stop_motor = false;
boolean right = false;
boolean left = false;
boolean driveForward = false;
void check_buffer(){
if (buf[0] == 's'){
stop_motor = true;
left = false;
right = false;
driveForward = false;
}
else if (buf[0] == 'l'){
stop_motor = false;
left = true;
right = false;
driveForward = false;
}
else if (buf[0] == 'r'){
stop_motor = false;
left = false;
right = true;
driveForward = false;
}
else if (buf[0] == 'f'){
stop_motor = false;
left = false;
right = false;
driveForward = true;
}
}
void motor_control(){
check_buffer();
while (stop_motor == true && left == false && right == false && driveForward == false){
stopMotors();
}
//turn left
while (stop_motor == false && left == true && right == false && driveForward == false){
left();
delay(200);
drive_forward();
}
//turn righ
while (stop_motor == false && left == false && right == true && driveForward == false){
right();
delay(200);
drive_forward();
}
//go straight
while (stop_motor == false && left == false && right == false && driveForward == true){
drive_forward();
}
}
void loop(){
// read calibrated sensor values and obtain a measure of the line position
// from 0 to 5000 (for a white line, use readLineWhite() instead)
uint16_t position = qtr.readLineBlack(sensorValues);
// print the sensor values as numbers from 0 to 1000, where 0 means maximum
// reflectance and 1000 means minimum reflectance, followed by the line
// position
for (uint8_t i = 0; i < SensorCount; i++)
{
Serial.print(sensorValues[i]);
Serial.print('\t');
}
Serial.println(position);
//use sensor values to determine where line is and move accordingly
if ((sensorValues[1] > 500) || (sensorValues[2]> 500)){
stopMotors();
} else {
motor_control();
}
//clear the buffer when a command is executed
if (process_it){
pos = 0;
process_it = false;
}
}
void left(){
digitalWrite(Rmotor,HIGH);
digitalWrite(Lmotor,HIGH);
analogWrite(RmotorPWM,50);
analogWrite(LmotorPWM, 90);
}
void right(){
digitalWrite(Rmotor,HIGH);
digitalWrite(Lmotor,HIGH);
analogWrite(RmotorPWM,90);
analogWrite(LmotorPWM, 50);
}
void drive_forward(){
digitalWrite(Rmotor,HIGH);
digitalWrite(Lmotor,HIGH);
analogWrite(RmotorPWM,75);
analogWrite(LmotorPWM, 75);
}
void stopMotors(){
digitalWrite(motorpin2, LOW); //stop
digitalWrite(motorpin1, LOW);
}