SD Card confusion

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
]]

Hi @jmcebula10633.

The reason for this is that the onboard SD card slot's SS pin is connected to a dedicated pin 28 on the MKR Zero, not to pin 4.

The SD library is configured to automatically use the appropriate pin by default, so you can initialize the library for use with the MKR Zero's onboard SD slot like this:

SD.begin();

In other cases (e.g., if using an alternative library other than "SD") where you do need to explicitly reference the pin, you can use the SDCARD_SS_PIN macro.

Thanks Per!

In the haze of throwing everything at the wall, I think I've tried this already, but would it need to look like this?

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();
  Serial.begin(115200);
  AudioZero.begin(8800); 
delay(3000);

if (!SD.begin(28))) {
  Serial.println(" failed!");
  while(true);
}
Serial.println(" done.");
}

I've found that if I leave out the if statement, all other parts of the code work, it just doesn't play the sound files. I've got the audio output from the mkr zero in the DAC0/A0 pin from the simple audio player tutorial hooked up to the LM386 amplifier.

thanks again, you're super helpful :slight_smile:

You are calling SD.begin twice.

Here:

and again here:

You should only call it once. I haven't looked at the SD library code to see what happens when you call it twice, but it certainly isn't necessary and from what you say it might be causing a problem.

After removing the redundant SD.begin call, post the updated full sketch code in a reply here and the forum helpers will take a look to see if we can spot a problem.

1 Like

Hello!

I didn't realize I was calling it twice. I removed the first SD.begin(); and left the second one as (28).

When I upload the code it still returns "failed!"

I did some poking around and found the pin out diagrams here on the website. I don't pretend to be smart enough to know what all that means, but at the far left in grey it calls out: 5-CLK, 3-CMD, 2-CD DAT3, and 7-DAT0. Is it one of these pins I need to be looking at? I don't see a Pin 28 referenced.

Thanks again!

Here is my 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;
const int SD_CS = 4;
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();removes redundant SD.begin
  Serial.begin(115200);
  AudioZero.begin(8800); 
delay(3000);

if (!SD.begin(28)) {//Should it be this pin or another one? I'll try others too.
  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

Since it is somewhat internal to the board hardware, it is not clearly documented. If you look at the full pinout, you can learn which pin of the MKR Zero board's ATSAMD21G18 microcontroller is connected to the SS pin on the card slot:

The pinout tells us the low level PA14 pin label, but not which Arduino pin number is mapped to PA14. To learn the Arduino pin number, it is necessary to go to the source code of the MKR Zero's core variant:

This line is an element in the g_APinDescription array. The Arduino pin number is the index of the element in the array, which happens to be 28, as mentioned in the associated comment:

But as I already explained, you don't really need to know this. When using the SD library, it will automagically use the correct pin by default if you omit the pin number argument from the SD.begin call:

  SD.begin();

And when you do need to specify the pin number, you can use the convenient SDCARD_SS_PIN macro instead of the integer number.

Thanks for the explanation, it seems like it should be easy but I'm doing something really wrong. it can't be this hard.

when I use just SD.begin();, nothing happens with SD card. I'm using .wav files with 8 character names, the files are in the root directory. Is DAC0/A0 the right pin to go out to the speaker? I really don't know what I'm doing wrong now.

and as an extra bonus, I think I broke it somehow. The yellow LED doesn't come on anymore and the speaker makes loud angry hissing when I plug it in, and my computer doesn't recognize it anymore. I don't have a spare.

I was waiting for replies, uploading the code and changing the number in the (!SD.begin(X)); place. When I got to 21 my computer stopped recognizing it, and upon unplugging/plugging it back in it hisses angrily. I hope i didn't break it permanently.

What do you expect to happen? If you use this code, do you get a "failed" message?

if (!SD.begin()) {
  Serial.println(" failed!");
  while(true);
}

Always post the current test code, using code tags, so forum members have some idea what you are talking about.

Hi j!

I just tried it witih


if (!SD.begin()) {
  Serial.println(" failed!");
  while(true);
Serial.println(" done.");
}
}

and now the code doesn't do anything, just stops. I'm pretty sure I'm not doing it right. It can't be this hard.

I did get a new Zero and now it works again, just doesn't play the SFX. (I think my PIR sensor is bad too, always giving out a HIGH signal even when I have a box covering it. I also had to rework my {} placement so that it actually does the whole thing. I'm pretty sure the only problem I have now is that I'm not activating the SD card, or that my .wav files aren't the right format maybe?
i have them named as shown in the code. I downloaded them so I don't know if there is a different kind of .wav file that I need or if I need to reformat them.

Here is my current code:

#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();
  Serial.begin(115200);
  AudioZero.begin(9600); 
delay(1000);

//if (!SD.begin()) {
//  Serial.println(" failed!");
//  while(true);
//Serial.println(" done.");
//}
}

void loop() {   // put your main code here, to run repeatedly:
  motionStatus = LOW;
  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 + 8000);//and pirTimerStop values
       Serial.println("Motion Detected");
       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.
    
   Serial.println("Motion Not detected");
   pirState = LOW;
   delay(1000);
  
 }

}

void RandomFile()
{
  RandomSound = random(4); //pick a random number to play
if (RandomSound == 1) 
    {
    File Sound1 = SD.open("Sound 1.wav");
    File Sound2 = SD.open("Sound2.wav");
    Serial.println(" Sound 1 playing!!");
    AudioZero.play(Sound1);//plays Sound.
    AudioZero.end();//wait until the end of the file
    Serial.println(" Sound 2 playing!!");
    AudioZero.play(Sound2);//plays Sound.
    AudioZero.end();//wait until the end of the file
    delay(8000);//wait 2 seconds before starting over
    }
  else if (RandomSound == 2)
        {
        File Sound3 = SD.open("Sound3.wav");
        File Sound4 = SD.open("Sound4.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
        Serial.println(" Sound 4 playing!!");
        AudioZero.play(Sound4);//plays Sound.
        AudioZero.end();//wait until the end of the file
        delay(8000);//wait 2 seconds before starting over
          }
      else if (RandomSound == 3)
            {
            File Sound5 = SD.open("Sound5.wav");
            File Sound6 = SD.open("Sound6.wav");
            Serial.println(" Sound 5 playing!!");
            AudioZero.play(Sound5);//plays Sound.
            AudioZero.end();//wait until the end of the file
            Serial.println(" Sound 6 playing!!");
            AudioZero.play(Sound6);//plays Sound.
            AudioZero.end();//wait until the end of the file
            delay(8000);//wait 2 seconds before starting over
            }
         else if (RandomSound == 4) 
              {
              File Sound7 = SD.open("Sound7.wav");
              File Sound8 = SD.open("Sound8.wav");
              Serial.println(" Sound 7 playing!!");
              AudioZero.play(Sound7);//plays Sound.
              AudioZero.end();//wait until the end of the file
              Serial.println(" Sound 8 playing!!");
              AudioZero.play(Sound8);//plays Sound.
              AudioZero.end();//wait until the end of the file
              delay(8000);//wait 2 seconds before starting over
              }

}
//end of program

with the if (!SD.begin()) statement out, it does the thing without SFX. Not sure what I'm missing here, it really doesn't seem like it should be this hard.

Thanks!!

It sounds like you have a bad board.

With the new board, there is no reason to open two files at once, if they are to be played sequentially. Make sure that the first is closed before you open the second.

File Sound1 = SD.open("Sound 1.wav");
    File Sound2 = SD.open("Sound2.wav");
    Serial.println(" Sound 1 playing!!");
    AudioZero.play(Sound1);//plays Sound.
    AudioZero.end();//wait until the end of the file
    Serial.println(" Sound 2 playing!!");
    AudioZero.play(Sound2);//plays Sound.
    AudioZero.end();//wait until the end of the file

Thanks!

I moved it so that it only does one file at a time:

if (RandomSound == 1) 
    {
    File Sound1 = SD.open("Sound 1.wav");
    Serial.println(" Sound 1 playing!!");
    AudioZero.play(Sound1);//plays Sound.
    AudioZero.end();//wait until the end of the file
    Serial.println(" Sound 2 playing!!");
    File Sound2 = SD.open("Sound2.wav");
    AudioZero.play(Sound2);//plays Sound.
    AudioZero.end();//wait until the end of the file
    delay(8000);//wait 2 seconds before starting over
    }

Both boards I have have done the same thing, that's why I didn't think I had a bad board.

I've just tried to upload the code from this tutorial:

https://docs.arduino.cc/tutorials/generic/simple-audio-player/

which is this code:


#include <SD.h>
#include <SPI.h>
#include <AudioZero.h>

void setup()
{
  // debug output at 115200 baud
  Serial.begin(115200);
  // setup SD-card
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println(" failed!");
    while(true);
  }
  Serial.println(" done.");
  // 44100kHz stereo => 88200 sample rate
  AudioZero.begin(2*44100);
}

void loop()
{
  int count = 0;
  // open wave file from sdcard
  File myFile = SD.open("Sound1.wav");
  if (!myFile) {
    // if the file didn't open, print an error and stop
    Serial.println("error opening test.wav");
    while (true);
  }
  Serial.print("Playing");
  // until the file is not finished
  AudioZero.play(myFile);
  Serial.println("End of file. Thank you for listening!");
  while (true) ;
}

and I get the same "failed!" message (most likely due to the reasons Per set out above). The tutorial claims to be for the MKR Zero, so I'm not sure how it's worked for anyone.

I don't have another SD card to try, but it works fine in my computer when I upload the files to the root directory.

I keep going back to the thought that I'm doing something very wrong, and it's got to be easier than this. Do you have a Zero you can load it on to and try it out yourself?

thanks again.

I did get a new Zero and now it works again,

I interpreted the above to indicate that the new board behaves differently. What do you mean by "works again"?

This is expected to fail if the correct SS pin is 28.

if (!SD.begin(4))

If SD.begin(28) also fails for both boards, then something else is wrong (bad SD card, etc.).

I just tried it out. with these lines of code in,

if (!SD.begin(28)) {
  Serial.println(" failed!");
  while(true);
  Serial.println(" done.");
 }

nothing happens. no "failed!", no "done". Nothing.

With these lines of code:

if (!SD.begin(4)) {
  Serial.println(" failed!");
  while(true);
  Serial.println(" done.");
 }

I get "failed!" as before, and I get the same result as before without the !SD.begin.

I also just tried another SD Card, with the same results as my last comment.

I also tried this:

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();
  Serial.begin(115200);
  AudioZero.begin(9600);
delay(1000);
File Sound1 = SD.open("sound 1.wav");
    Serial.println(" Sound 1 playing!!");
    AudioZero.play(Sound1);//plays Sound.
    AudioZero.end();//wait until the end of the file

//if (!SD.begin(28)) {
//  Serial.println(" failed!");
//  while(true);
//  Serial.println(" done.");
// }
}

putting in a sound during the setup, as sort of an initiatializing sound. The program skipped right over it and went straight into my void loop(). Is there something wrong with my setup code?

So, I tried some experiments:

I added a set of {} after the "while" statement:

if (!SD.begin(28)) {
  Serial.println(" failed!");
  while(true){
  Serial.println(" done.");
 }
}

this made it print "done." forever, over and over.

I changed the if/While statement to an if/else statement, and it worked! it printed "done." once, but still no sounds or otherwise from the void loop program.

Then I added this to my setup {}, to print a line for what it's doing:


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();
  Serial.println("SD Card Startup");
  delay(500);
  Serial.begin(115200);
  Serial.println("Serial Begin");
  delay(500);
  AudioZero.begin(9600);
  Serial.println("AudioZeroBegin");
  delay(500);
  File Sound1 = SD.open("sound 1.wav");
    Serial.println(" Sound 1 playing!!");
    AudioZero.play(Sound1);//plays Sound.
    AudioZero.end();//wait until the end of the file

  if (!SD.begin(28)) {
    Serial.println(" failed!"); }
    else {
    //while(true){
    Serial.println(" done.");
    }
 
}

it skips over ALL of the serial.println commands and the delay commands, I have to be doing something wrong here. The void loop knows to look for input on pirPin, so it's reading that in the setup{}, so I don't know what's going on here.

Here is the full code for reference

#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();
  Serial.println("SD Card Startup");
  delay(500);
  Serial.begin(115200);
  Serial.println("Serial Begin");
  delay(500);
  AudioZero.begin(9600);
  Serial.println("AudioZeroBegin");
  delay(500);
  File Sound1 = SD.open("sound 1.wav");
    Serial.println(" Sound 1 playing!!");
    AudioZero.play(Sound1);//plays Sound.
    AudioZero.end();//wait until the end of the file

  if (!SD.begin(28)) {
    Serial.println(" failed!"); }
    else {
    //while(true){
    Serial.println(" done.");
    }
 
}

void loop() {   // put your main code here, to run repeatedly:
  motionStatus = LOW;
  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 + 8000);//and pirTimerStop values
       Serial.println("Motion Detected");
       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.
    
   Serial.println("Motion Not detected");
   pirState = LOW;
   delay(1000);
  
 }

}

void RandomFile()
{
  RandomSound = random(4); //pick a random number to play
if (RandomSound == 1) 
    {
    File Sound1 = SD.open("sound 1.wav");
    Serial.println(" Sound 1 playing!!");
    AudioZero.play(Sound1);//plays Sound.
    AudioZero.end();//wait until the end of the file
    Serial.println(" Sound 2 playing!!");
    File Sound2 = SD.open("sound2.wav");
    AudioZero.play(Sound2);//plays Sound.
    AudioZero.end();//wait until the end of the file
    delay(8000);//wait 2 seconds before starting over
    }
  else if (RandomSound == 2)
        {
        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
        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(8000);//wait 2 seconds before starting over
          }
      else if (RandomSound == 3)
            {
            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
            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(8000);//wait 2 seconds before starting over
            }
         else if (RandomSound == 4) 
              {
              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
              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(8000);//wait 2 seconds before starting over
              }

}
//end of program

You need to provide some time for the Serial connection to be established before you start printing. Otherwise a few early Serial.print statements will be skipped.

Try this and let us know what is printed.

  // debug output at 115200 baud
  Serial.begin(115200);
delay(3000);
  // setup SD-card
  Serial.print("Initializing SD card...");
 if (!SD.begin(28)) 
     Serial.println(" failed!");
  else
     Serial.println(" SD setup OK.");

 Serial.println("end SD setup");

Hi j!

with the changed suggested to the tutorial program, I still get

initializing sd card... failed!

from the serial monitor.

I put a delay after serial.begin in my code like this:

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();
  Serial.println("SD Card Startup");
  delay(500);
  Serial.begin(115200);
    delay(3000);
  Serial.println("Serial Begin");
  delay(500);
  AudioZero.begin(9600);
  Serial.println("AudioZeroBegin");
  delay(500);
  File Sound1 = SD.open("sound 1.wav");
    Serial.println(" Sound 1 playing!!");
    AudioZero.play(Sound1);//plays Sound.
    AudioZero.end();//wait until the end of the file

  if (!SD.begin(28)) {
    Serial.println(" failed!"); }
    else {
    //while(true){
    Serial.println(" done.");
    }
 
}

and it still skips over all the serial.println commands and goes right into my void loop. Is there something about my setup {} that makes it skip right over those parts? the only part of it that I know works is setting up pin 6 as a digital read.

so some progress.

i changed it to this:

void setup() {
  // put your setup code here, to run once:
//Pin0 is pir, pinmode INPUT
  pinMode(pirPin, INPUT);
  pirState = LOW;
  motionStatus = LOW;
  TimerOn = 0;
  Serial.begin(115200);
    delay(3000);
  Serial.println("Serial Begin");
  delay(500);
  SD.begin();
  Serial.println("SD Card Startup");
  delay(500);
  AudioZero.begin(9600);
  Serial.println("AudioZeroBegin");
  delay(500);
  File Sound1 = SD.open("sound 1.wav");
    Serial.println(" Sound 1 playing!!");
    AudioZero.play(Sound1);//plays Sound.
    AudioZero.end();//wait until the end of the file

  if (!SD.begin(28)) {
    Serial.println(" failed!"); }
    else {
    //while(true){
    Serial.println(" done.");
    }
 
}

putting the serial.begin line first up in the program.

Now it returns this in the serial monitor:

Serial Begin
SD Card Startup
AudioZeroBegin
Sound 1 playing!!
failed!
Motion Not detected.

it then goes into the loop, but no sounds come out.

You have two SD.begin() lines with SD.open() in between them, and you don't bother to check whether the first SD.begin() or the SD.open() returned an error.

You really do need to pay attention.