-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
242 lines (196 loc) · 5.54 KB
/
main.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Add libraries
#include "Arduino.h"
#include <Bounce2.h>
#include "SoftwareSerial.h"
// https://github.com/electronicdrops/RFIDRdm630
#include <RFIDRdm630.h>
//for Piezo Melody
#include <pitches.h>
//DFPlayer Mini
#include "DFRobotDFPlayerMini.h"
// set Data Pin Piezo buzzer
const int buzzer = 9; //buzzer to arduino pin 9
// set Data Pin RDM6300
const int rxPin = 6; // pin that will receive the data
const int txPin = 7; // connection not necessary.
RFIDtag tag; // RFIDtag object
RFIDRdm630 reader = RFIDRdm630(rxPin,txPin); // the reader object.
// set Data Pin DFRobotDFPlayerMini
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
// define Cars Numbers
#define elements 2
long validCardNumber[elements] = {13683409, // Folder 1
13683544 // Folder 2
};
long lastCardNumber = 0;
// set Buttons
const int buttonNextPin = 4;
const int buttonPrevPin = 5;
int buttonNextState = LOW;
int buttonPrevState = LOW;
bool isplaying = false;
// Instantiate a Bounce object
Bounce debouncer1 = Bounce();
// Instantiate another Bounce object
Bounce debouncer2 = Bounce();
void setup(){
// Initializing serial console
Serial.begin(115200);
Serial.println();
Serial.println(F("RFID Arduino Jukebox"));
Serial.println(F("Initializing ..."));
// Initializing Piezo Buzzer
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
Serial.println(F("... Piezo Buzzer done."));
// Initializing Buttons
pinMode(buttonNextPin, INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer1.attach(buttonNextPin);
debouncer1.interval(3); // interval in ms
pinMode(buttonPrevPin, INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer2.attach(buttonPrevPin);
debouncer2.interval(3); // interval in ms
// Instantiate a Bounce object
// Initializing RFID RDM6300
// Initializing DFRobotDFPlayerMini
mySoftwareSerial.begin(9600);
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
noTone(buzzer);
tone(buzzer, 1519,100);
delay(300);
tone(buzzer, 1519,100);
delay(300);
tone(buzzer, 1519,100);
delay(300);
noTone(buzzer);
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true){
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.setTimeOut(500);
myDFPlayer.volume(20);
Serial.print(F("DFPlayer Mini set Volume "));
Serial.println(myDFPlayer.readVolume());
delay(300);
myDFPlayer.play(1);
//myDFPlayer.next();
// play startupsound via buzzer
Serial.println(F("Playing startupsound via Buzzer"));
noTone(buzzer);
tone(buzzer, 1519,100);
delay(300);
tone(buzzer, 1519,100);
delay(300);
noTone(buzzer);
Serial.println(F("Waiting for Interaction."));
}
void loop() {
// is Playing
if (isplaying){
// Update the Bounce instances :
debouncer1.update();
debouncer2.update();
// Get the updated value :
// Button 1
if ( debouncer1.fell() ) {
// Toggle LED state :
buttonNextState = HIGH;
}
if (buttonNextState == HIGH) {
// play next:
Serial.println("Button Next");
tone(buzzer,NOTE_C4,100);
mySoftwareSerial.listen();
myDFPlayer.next();
delay(500);
buttonNextState = LOW;
}
// Button 2
if ( debouncer2.fell() ) {
// Toggle LED state :
buttonPrevState = HIGH;
}
if (buttonPrevState == HIGH) {
// play next:
Serial.println("Button Previous");
tone(buzzer,NOTE_G4,100);
mySoftwareSerial.listen();
myDFPlayer.previous();
delay(500);
buttonPrevState = LOW;
}
}
reader.rfidlisten();
delay(300);
/*
if (mySoftwareSerial.isListening()) {
Serial.println("DFPlayer is listening!");
}else{
Serial.println("DFPlayer is not listening!");
}
if (reader.rfidisListening()) {
Serial.println("RFID is listening!");
}else{
Serial.println("RFID is not listening!");
}
*/
//RDM6300 get Tag
if (reader.isAvailable()){ // tests if a card was read by the module
tag = reader.getTag(); // if true, then receives a tag object
// Compare cardNumber
boolean matching = true;
int j;
for (j = 0; j < elements; j++) {
if(validCardNumber[j] == tag.getCardNumber()){
matching = true;
break;
}else{
matching = false;
}
}
// Check if lastCardNumber
if (lastCardNumber == tag.getCardNumber()){
Serial.println("Card Number is Last Card Number");
}else{
// Action after comparing cardNumber
if(matching) {
Serial.print("Match. Element: ");
Serial.print(j);
Serial.print(" Card Number: ");
Serial.println(tag.getCardNumber());
lastCardNumber = tag.getCardNumber();
// Play Fireball sound
tone(buzzer,NOTE_G4,35);
delay(35);
tone(buzzer,NOTE_G5,35);
delay(35);
tone(buzzer,NOTE_G6,35);
delay(35);
noTone(buzzer);
Serial.println("Listen to DFPlayer");
mySoftwareSerial.listen();
delay(300);
int folder=j+1;
Serial.print("Play Song, Folder ");
Serial.println(folder);
myDFPlayer.loopFolder(folder);
delay(300);
Serial.println("Playing");
isplaying=true;
}else{
Serial.print("No Match. Card Number: ");
Serial.println(tag.getCardNumber());
tone(buzzer, 1519,100);
reader.rfidlisten();
}
//Action done
}
}
//getting Tag done
}