-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpumpkinbot2014.cpp
221 lines (163 loc) · 3.76 KB
/
pumpkinbot2014.cpp
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
//
// pumpkinbot 2014
//
// hack o'lantern 2014
//
// author: @jefforulez
//
//
// parallax rfid code borrowed from
// https://github.com/ril3y/Make-Projects/blob/master/Arduino_Parallax_RFID_Reader/Arduino_Parallax_RFID_Reader.pde
//
#include "pumpkinbot2014.h"
//
// pin assignments
//
const int EMIC2_RX = 2 ;
const int EMIC2_TX = 3 ;
const int RFID_RX = 6 ;
const int RFID_TX = 7 ;
const int RFID_ENABLE = 8 ;
const int LED_PIN = 13 ;
//
// rfid globals
//
const int CODE_LEN = 10 ; // max length of RFID tag
const int VALIDATE_LENGTH = 200 ; // maximum reads b/w tag read and validate
const char START_BYTE = 0x0A ;
const char STOP_BYTE = 0x0D ;
const char candySourPatchKids[] = "35021E906B" ;
char rfid_tag[ CODE_LEN + 1 ] ;
SoftwareSerial serialRFID( 6, 7) ; // RX, TX
//
// tts globals
//
SoftwareSerial emic2Serial = SoftwareSerial( EMIC2_RX, EMIC2_TX ) ;
Emic2TtsModule emic2TtsModule = Emic2TtsModule( &emic2Serial ) ;
//
// methods
//
void setup()
{
Serial.begin( 9600 ) ;
//
// initialize pins
//
// tts
pinMode( EMIC2_RX, INPUT ) ;
pinMode( EMIC2_TX, OUTPUT ) ;
// rfid
pinMode( RFID_ENABLE, OUTPUT ) ;
pinMode( RFID_RX, INPUT ) ;
pinMode( RFID_TX, OUTPUT ) ;
// leds
pinMode( LED_PIN, OUTPUT ) ;
//
// initialize devices
//
// turn the rfid reader on
digitalWrite( RFID_ENABLE, LOW ) ;
delay( 1000 ) ;
}
void loop()
{
Serial.println( "loop()" ) ;
// blocks while an RFID tag is present
blockWhileRFIDPresent() ;
sayGiveMeCandy() ;
delay( 1000 ) ;
}
//
// TTS
//
int voice_index = 0 ;
void sayGiveMeCandy()
{
Serial.println( "sayGiveMeCandy()" ) ;
emic2Serial.begin( 9600 ) ;
emic2TtsModule.init() ;
emic2TtsModule.setVolume( 40 ) ;
emic2TtsModule.setWordsPerMinute( 250 ) ;
emic2TtsModule.setVoice( (EmicVoice)( voice_index++ % 9 ) ) ;
// emic2TtsModule.setVoice( HugeHarry ) ;
emic2TtsModule.say( F("Yo, witches. Where's my candy") ) ;
}
//
// RFID
//
void blockWhileRFIDPresent()
{
Serial.println( "blockWhileRFIDPresent()" ) ;
int no_rfid_count = 0 ;
// start the serial connections
serialRFID.begin( 2400 ) ;
while ( 42 )
{
// start the loop with a clean input buffer
serialRFID.flush() ;
// give the reader time to generate new data
delay( 400 ) ;
// clear the rfid_tag
for ( int i = 0 ; i < CODE_LEN + 1 ; i++ ) {
rfid_tag[i] = 0x0 ;
}
byte byte_read = 0x0 ;
// throw away garbage bytes and wait for the start_byte
while ( serialRFID.available() > 0 )
{
byte_read = serialRFID.read() ;
if ( byte_read == START_BYTE ) {
break ;
}
else if ( byte_read == STOP_BYTE ) {
Serial.println( "received STOP_BYTE when expecting START_BYTE" ) ;
delay( 200 ) ;
continue ;
}
delay( 200 ) ;
}
// break, if we haven't gotten a start byte by now
if ( byte_read != START_BYTE )
{
Serial.println( "error: expecting START_BYTE" ) ;
// allow for one error to account for noise and timing issues
if ( no_rfid_count++ < 1 ) {
continue ;
}
// turn the LEDs on when the rfid is missing
ledsOn() ;
break ;
}
// turn the LEDs off when the rfid is present
ledsOff() ;
byte bytesread = 0x0 ;
while ( bytesread < CODE_LEN )
{
// Serial.println( "reading RFID tag" ) ;
if ( serialRFID.available() <= 0 ) {
Serial.println( "error: expecting more bytes" ) ;
break ;
}
if ( ( byte_read = serialRFID.read() ) == STOP_BYTE ) {
Serial.println( "error: expecting STOP_BYTE" ) ;
break ;
}
rfid_tag[ bytesread++ ] = byte_read ;
delay( 200 ) ;
}
Serial.println( "tag: " ) ;
Serial.println( rfid_tag ) ;
}
return ;
}
//
// LEDs
//
void ledsOn()
{
digitalWrite( LED_PIN, HIGH ) ;
}
void ledsOff()
{
digitalWrite( LED_PIN, LOW ) ;
}