-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreamer.ino
110 lines (90 loc) · 2.78 KB
/
Screamer.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
#include <I2S.h>
/*
This reads a wave file from an SD card and plays it using the I2S interface to
a MAX08357 I2S Amp Breakout board.
Circuit:
* Arduino/Genuino Zero, MKRZero or MKR1000 board
* SD breakout or shield connected
* MAX08357:
* GND connected GND
* VIN connected 5V
* LRC connected to pin 0 (Zero) or pin 3 (MKR1000, MKRZero)
* BCLK connected to pin 1 (Zero) or pin 2 (MKR1000, MKRZero)
* DIN connected to pin 9 (Zero) or pin A6 (MKR1000, MKRZero)
created 15 November 2016
by Sandeep Mistry
*/
#include <SD.h>
#include <ArduinoSound.h>
// filename of wave file to play
const char filename[] = "MUSIC.WAV";
unsigned long last_played = 0;
bool cleared = true;
// variable representing the Wave File
SDWaveFile waveFile;
void setup() {
// Open serial communications and wait for port to open:
//Serial.begin(9600);
//while (!Serial) {
// ; // wait for serial port to connect. Needed for native USB port only
//}
pinMode(11, OUTPUT);
digitalWrite(11, LOW);
pinMode(12, OUTPUT);
digitalWrite(12, HIGH);
// setup the SD card, depending on your shield of breakout board
// you may need to pass a pin number in begin for SS
//Serial.println("Initializing SD card...");
if (!SD.begin()) {
//Serial.println("initialization failed!");
return;
}
//Serial.println("initialization done.");
// create a SDWaveFile
waveFile = SDWaveFile(filename);
// check if the WaveFile is valid
if (!waveFile) {
//Serial.println("wave file is invalid!");
while (1); // do nothing
}
// print out some info. about the wave file
//Serial.print("Bits per sample = ");
//Serial.println(waveFile.bitsPerSample());
long channels = waveFile.channels();
//Serial.print("Channels = ");
//Serial.println(channels);
long sampleRate = waveFile.sampleRate();
//Serial.print("Sample rate = ");
//Serial.print(sampleRate);
//Serial.println(" Hz");
long duration = waveFile.duration();
//Serial.print("Duration = ");
//Serial.print(duration);
//Serial.println(" seconds");
// adjust the playback volume
AudioOutI2S.volume(100);
// check if the I2S output can play the wave file
if (!AudioOutI2S.canPlay(waveFile)) {
//Serial.println("unable to play wave file using I2S!");
while (1); // do nothing
}
// start playback
//Serial.println("starting playback");
AudioOutI2S.play(waveFile);
}
void loop() {
// check if playback is still going on
if (!AudioOutI2S.isPlaying()) {
// playback has stopped
// Prints the distance on the Serial Monitor
//Serial.print("Light: ");
//Serial.println(analogRead(A1));
if(analogRead(A1) < 500 && cleared) {
AudioOutI2S.play(waveFile);
last_played = millis();
cleared = false;
} else if (analogRead(A1) > 500){
cleared = true;
}
}
}