Hi guys! (not sure if hardware is the right place for this)
I'm trying to get this code to work. It's meant to have the pir sensor trigger, play two audio files then a bunch of audio files at random for a minute.
All the timing parts are good to go, works fine. It's the actual playing of the files that isn't happening. I think because I'm not sure how to use this section of code:
void setup() {
// put your setup code here, to run once:
//Pin0 is pir, pinmode INPUT
pinMode(pirPin, INPUT);
pirState = LOW;
motionStatus = LOW;
TimerOn = 0;
SD.begin(4);
Serial.begin(115200);
AudioZero.begin(8800);
delay(3000);
if (!SD.begin(4)) {
Serial.println(" failed!");
while(true);
}
Serial.println(" done.");
}
]]
I always get "failed!" when I have the if(!SD.begin(4)); part in the code. using // to remove it, the code works just fine, just doesn't play any sounds. I'm sure me being an idiot isn't helping.
Thanks!
Here's the full code:
[[
//#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#include <AudioZero.h>
int pirPin = 6;//location of the pir input, pin 6 Tx
int motionStatus; //Current pir reading
int pirState;//monitors what the pir is seeing
int RandomSound;
int RandomFX;
int TimerOn;
unsigned long pirTimer;
unsigned long pirTimerStop;
//void RandomFile();
void setup() {
// put your setup code here, to run once:
//Pin0 is pir, pinmode INPUT
pinMode(pirPin, INPUT);
pirState = LOW;
motionStatus = LOW;
TimerOn = 0;
SD.begin(4);
Serial.begin(115200);
AudioZero.begin(8800);
delay(3000);
if (!SD.begin(4)) {
Serial.println(" failed!");
while(true);
}
Serial.println(" done.");
}
void loop() { // put your main code here, to run repeatedly:
motionStatus = digitalRead(pirPin);
if ((motionStatus == HIGH) && (TimerOn == 0)){ //checks for motion on the input AND the timer to be off
if (pirState == LOW){
pirState = HIGH;
TimerOn = 1; //locks pirState and TimerOn to high for the duration of the program
pirTimer = millis();//set pirTimer
pirTimerStop = (pirTimer + 60000);//and pirTimerStop values
Serial.println("Motion Detected");
//open file from sd card
Serial.println("WelcomeRobot1");
File WelcomeRobot1 = SD.open("WR1.wav");
File WelcomeRobot1Spanish = SD.open("WR1S.wav");
AudioZero.play(WelcomeRobot1);//"Hello, Im a robot."
//AudioZero.end();//wait until the end of the file
delay(1000);
Serial.println("WelcomeRobot1Spanish");
AudioZero.play(WelcomeRobot1Spanish);//Hello, Im a robot, in spanish."
//AudioZero.end();//wait until the end of the file
delay(1000); //two second delay after the welcome message before going to robot sounds
while(TimerOn == 1) { //put timing loop here
pirTimer = millis();//read the current time.
if (pirTimer >= pirTimerStop) { //check to see if pirTimer value equals pirTimerStop
TimerOn = 0;
Serial.println("Time's up, no motion detected");
pirState = LOW; //turn off timer, reset main loop to play from the top if there is still motion.
}
else {
Serial.println("Playing Sounds now");
RandomFile(); //calls the function RandomFile to play a random sound effect.
}
}
}
else { //no motion, turns cycle off.
delay(5000);
Serial.println("Motion Not detected");
pirState = LOW;
}
}
}
void RandomFile()
{
RandomSound = random(10); //pick a random number to play
if (RandomSound == 1)
{
File Sound1 = SD.open("Sound1.wav");
Serial.println(" Sound 1 playing!!");
AudioZero.play(Sound1);//plays Sound.
AudioZero.end();//wait until the end of the file
delay(2000);//wait 2 seconds before starting over
}
else if (RandomSound == 2)
{
File Sound2 = SD.open("Sound2.wav");
Serial.println(" Sound 2 playing!!");
AudioZero.play(Sound2);//plays Sound.
AudioZero.end();//wait until the end of the file
delay(2000);//wait 2 seconds before starting over
}
else if (RandomSound == 3)
{
File Sound3 = SD.open("Sound3.wav");
Serial.println(" Sound 3 playing!!");
AudioZero.play(Sound3);//plays Sound.
AudioZero.end();//wait until the end of the file
delay(2000);//wait 2 seconds before starting over
}
else if (RandomSound == 4)
{
File Sound4 = SD.open("Sound4.wav");
Serial.println(" Sound 4 playing!!");
AudioZero.play(Sound4);//plays Sound.
AudioZero.end();//wait until the end of the file
delay(2000);//wait 2 seconds before starting over
}
else if (RandomSound == 5)
{
File Sound5 = SD.open("Sound5.wav");
Serial.println(" Sound 5 playing!!");
AudioZero.play(Sound5);//plays Sound.
AudioZero.end();//wait until the end of the file
delay(2000);//wait 2 seconds before starting over
}
else if (RandomSound == 6)
{
File Sound6 = SD.open("Sound6.wav");
Serial.println(" Sound 6 playing!!");
AudioZero.play(Sound6);//plays Sound.
AudioZero.end();//wait until the end of the file
delay(2000);//wait 2 seconds before starting over
}
else if (RandomSound == 7)
{
File Sound7 = SD.open("Sound7.wav");
Serial.println(" Sound 7 playing!!");
AudioZero.play(Sound7);//plays Sound.
AudioZero.end();//wait until the end of the file
delay(2000);//wait 2 seconds before starting over
}
else if (RandomSound == 8)
{
File Sound8 = SD.open("Sound8.wav");
Serial.println(" Sound 8 playing!!");
AudioZero.play(Sound8);//plays Sound.
AudioZero.end();//wait until the end of the file
delay(2000);//wait 2 seconds before starting over
}
else if (RandomSound == 9)
{
File Sound9 = SD.open("Sound9.wav");
Serial.println(" Sound 9 playing!!");
AudioZero.play(Sound9);//plays Sound.
AudioZero.end();//wait until the end of the file
delay(2000);//wait 2 seconds before starting over
}
else if (RandomSound == 10);
{
File Sound10 = SD.open("Sound10.wav");
Serial.println(" Sound 10 playing!!");
AudioZero.play(Sound10);//plays Sound.
AudioZero.end();//wait until the end of the file
delay(2000);//wait 2 seconds before starting over
}
//delay (2000);
}
//end of program
]]
