Based on the code that Dr. Wang provided (saw this code first on Adafruit and attributed it to goatboy when I asked for help), and with the help of a friend, I am trying to create a project where a the sound is on by default, and then goes off when someone triggers a PIR sensor. This will be part of a sculpture containing a porcelain bird, and I want to have a speaker playing birdsong that shuts off when someone comes close. I want it to seem as if the bird was frightened, and stopped singing, and then I want it to start up again if the person either moves far enough away, or holds still long enough to let the sensor reset.
Some other issues: To make this seem more realistic, I want to have a set of maybe 20 wav files, including some with no sound at all (as pauses), and I want them to play in random order. I also want to make sure no sounds are cut off, so they will need to play to the end, even when a signal to stop has been received from the PIR, and for this reason they will also be very short, so the system can respond quickly.
To be as specific as possible, here are the things I want the code to do:
1-Play wav files continuously unless the PIR is triggered
2-Play the wave files randomly (they could be named simply as sequential numbers, if that would help)
3-Play out the current wav file once the PIR signal is received, and then stop.
4-Allow a setting in the code to be adjusted to control how long the bird waits before singing again (ideally, this could be set as a random length of time, within a certain range)
Thanks in advance to anyone who is willing to help!
#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"
int pirPin = 8; // Reading from digital pin 8 PIR sensor!
int pirPinValue = 0; //variable to hold the digital value
AF_Wave card;
File f;
Wavefile wave;
void setup() {
// set up serial port
Serial.begin(9600);
pinMode(pirPin, INPUT);
// set up waveshield pins
pinMode(8, INPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
// open memory card...........This part is necessary to set up and
read SDcard!
if (!card.init_card()) {
putstring_nl("Card init. failed!"); return;
}
if (!card.open_partition()) {
putstring_nl("No partition!"); return;
}
if (!card.open_filesys()) {
putstring_nl("Couldn't open filesys"); return;
}
if (!card.open_rootdir()) {
putstring_nl("Couldn't open dir"); return;
}
putstring_nl("Files found:");
ls();
}
void ls() {
char name[13];
int ret;
card.reset_dir();
putstring_nl("Files found:");
while (1) {
ret = card.get_next_name_in_dir(name);
if (!ret) {
card.reset_dir();
return;
}
Serial.println(name);
}
}
void loop(){
pirPinValue = digitalRead(pirPin); // read the digital input on pin 8;
Serial.println(pirPinValue, DEC); // print input of digital in 8;
delay(10); // delay 10 ms before next reading;
while (LOW==digitalRead(pirPin)) {;}
playcomplete("HOODED~1.WAV");
delay(1500);
while (LOW==digitalRead(pirPin)) {;}
playcomplete("BLUEBIRD.WAV");
delay(1500);
while (LOW==digitalRead(pirPin)) {;}
playcomplete("BOBWHITE.WAV");
delay(1500);
while (LOW==digitalRead(pirPin)) {;}
playcomplete("CARDINAL.WAV");
delay(1500);
while (LOW==digitalRead(pirPin)) {;}
playcomplete("GOLDFINCH.WAV");
delay(1500);
while (LOW==digitalRead(pirPin)) {;}
playcomplete("INDIGOBUNTING.WAV");
delay(1500);
while (LOW==digitalRead(pirPin)) {;}
playcomplete("OWL.WAV");
delay(1500);
while (LOW==digitalRead(pirPin)) {;}
playcomplete("REDWINGED.WAV");
delay(1500);
}
void playcomplete(char *name){
playfile(name);
while (wave.isplaying);
card.close_file(f);
}
void playfile(char *name) {
// stop any file already playing
if (wave.isplaying) {
wave.stop();
card.close_file(f);
}
f = card.open_file(name);
if (f && wave.create(f)) {
wave.play();
}
}