-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsillycode
More file actions
88 lines (74 loc) · 2.02 KB
/
sillycode
File metadata and controls
88 lines (74 loc) · 2.02 KB
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
#include <SPI.h>
#include <MFRC522.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
// --- Pin setup ---
#define RST_PIN 9
#define SS_PIN 10
#define DF_RX 5 // Arduino receives from DFPlayer TX
#define DF_TX 6 // Arduino transmits to DFPlayer RX
#define VOLUME_POT A0 // optional volume control
// --- Track numbers ---
#define TRACK_CAT 1
#define TRACK_STAL 2
#define TRACK_DEFAULT 3
// --- Modules ---
MFRC522 rfid(SS_PIN, RST_PIN);
SoftwareSerial dfSerial(DF_RX, DF_TX);
DFRobotDFPlayerMini player;
// --- Variables ---
bool isPlaying = false;
bool jukeboxEnabled = true; // toggle this to false to disable RFID scanning
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
dfSerial.begin(9600);
if (!player.begin(dfSerial)) {
Serial.println("⚠️ DFPlayer not found!");
while (true);
}
player.volume(20);
Serial.println("🎵 Jukebox Ready — scan a disc!");
}
void loop() {
if (!jukeboxEnabled) return; // skip all if disabled
// --- RFID detection ---
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
if (isPlaying) {
player.stop();
isPlaying = false;
Serial.println("🛑 Disc removed.");
}
return;
}
// --- Handle new tag ---
if (!isPlaying) {
unsigned long tagID = 0;
for (byte i = 0; i < rfid.uid.size; i++) {
tagID = (tagID << 8) | rfid.uid.uidByte[i];
}
Serial.print("💽 Detected Tag: ");
Serial.println(tagID);
if (tagID == 4150795908) {
player.play(TRACK_CAT);
Serial.println("▶️ Playing: Cat");
delay(185000);
}
else if (tagID == 4156658564) {
player.play(TRACK_STAL);
Serial.println("▶️ Playing: Stal");
delay(151000);
}
else {
player.play(TRACK_DEFAULT);
Serial.println("▶️ Playing: Default Track");
delay(96000);
}
isPlaying = true;
}
// --- Optional volume pot ---
int potValue = analogRead(VOLUME_POT);
int volume = map(potValue, 0, 1023, 0, 30);
player.volume(volume*3);
}