-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrickrollbox.ino
67 lines (57 loc) · 1.6 KB
/
rickrollbox.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
#include <SPI.h>
#include <SD.h>
#include <TMRpcm.h>
#define ButtonPin 2
#define DiscoPin 3
#define SpeakerPin 9
TMRpcm tmrpcm;
File root;
void setup() {
pinMode(ButtonPin, INPUT_PULLUP);
pinMode(DiscoPin, OUTPUT);
// Use Serial for debugging.
Serial.begin(9600);
Serial.println("Initialising SD.");
if (!SD.begin(4)) {
Serial.println("SD failed.");
while(true); // Stay here if the SD fails to initialise.
}
Serial.println("SD success.");
tmrpcm.speakerPin = 9;
root = SD.open("/");
tmrpcm.setVolume(6);
tmrpcm.quality(0);
}
void loop() {
if (!tmrpcm.isPlaying() ) {
Serial.println("No music");
// no file being played.
if (digitalRead(ButtonPin) == LOW) {
Serial.println("Wait for button.");
while(debounce());
Serial.println("Playing");
tmrpcm.play("rickroll.wav");
digitalWrite(DiscoPin, HIGH);
}
} else {
if (digitalRead(ButtonPin) == LOW) {
while(debounce());
// If the button is pressed again, stop playing music.
tmrpcm.stopPlayback();
// Write both pins low to turn of the lights and stop the speaker from making a terrible whining noise.
digitalWrite(SpeakerPin, LOW);
digitalWrite(DiscoPin, LOW);
return;
}
}
}
bool debounce() {
// Give time for a button to be pressed and released by the user- limiting the rate at which the function is executed.
byte count = 0;
for (byte i = 0; i < 5; i++) {
if (digitalRead(ButtonPin) == LOW) count++;
delay(10);
}
if (count > 2) return 1;
return 0;
}