-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudiotour_nonworking.ino
114 lines (93 loc) · 2.31 KB
/
audiotour_nonworking.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
#include <SPI.h>
#include <MFRC522.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
#include <Servo.h>
class Leg {
public:
float angle=0,speed=0;
Servo servo;
void update() {
servo.write(90+angle);
}
} front,back;
// Setup SPI-pins for RFID shield avoiding conflict with the MP3 shield, see https://learn.sparkfun.com/tutorials/mp3-player-shield-hookup
#define SS_PIN 10
#define RST_PIN 5
MFRC522 mfrc522(SS_PIN, RST_PIN);
SFEMP3Shield MP3player;
SdFat sd;
unsigned long lastSeen = 0;
bool playing = false;
bool critter = true;
void setup() {
back.servo.attach(3);
front.servo.attach(4);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
sd.begin(SD_SEL, SPI_HALF_SPEED);
MP3player.begin();
MP3player.setVolume(0x0000,0x0000); // Set volume at maximum
Serial.begin(115200);
}
void loop() {
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
lastSeen = millis();
if (!playing) {
mfrc522.PICC_HaltA();
startAudio();
critter = false;
}
}
if (millis()-lastSeen>5000) {
if (playing) {
stopAudio();
critter = true;
}
}
if (critter) {
updateCritter();
}
// Serial.println(millis()-lastSeen);
//
// Serial.println("start track");
// MP3player.pauseDataStream(); // disable interrupts to avoid collisions on the SPI bus between RFID reader and MP3player
// mfrc522.PICC_HaltA();
//
// MP3player.playTrack(1);
// }
//
// else {
// Serial.println(MP3player.getState()); //currentPosition());
// }
// MP3player.resumeDataStream(); //enable interrupts
}
void startAudio() {
Serial.println("startAudio");
playing = true;
MP3player.stopTrack();
MP3player.playTrack(1);
MP3player.resumeDataStream(); //enable interrupts
}
void stopAudio() {
Serial.println("stopAudio");
MP3player.stopTrack();
playing = false;
}
void updateCritter() {
// Serial.println("not playing");
back.angle = random(-40,40);
back.update();
front.angle = random(-40,40);
front.update();
}
void dumpUID() {
// Dump UID
Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();
}