Presence or Absence of a File on SD Card

if (digitalRead(22)==LOW)
    {
      tmrpcm.play("/1/001.wav");
    }

With above code and SD Card module as a simplest wave player
containing a folder named 1 and a file 001 I am having a very small issue and want to sort it.

If by mistake I forget to copy paste the wave file named 001.wav
and press Pin 22, the Arduino system get hanged or simply starts creating a noise on speakers

Like it is saying that "You dint upload the file and commanding me I am angry with you"
and I have to reset the system immediately

or

if it finds the file in the right place as per command , the player plays the file smoothly

So here,
no need to explain rest of the codes as it is available all the way everywhere on net

All I want is a code should be added like :-

If the WAVE is present
play the file

Else
do nothing or display empty file on serial monitor

Please guide me in designing that tiny script

Thanks a lot !!!! :blush:

K sir le me go thru that :blush:

Sir actually not able to understand that where and how this SD.exists will be applied

Still getting same issue

Not getting a correct script

Have a look at the SD library Files example

Yeah Looking there only but not a proper sequence i got

Can you please get me the URL where actually you are pointing ??

This one only

Is this one

It is an example available from the IDE

image

Oh ok ok le me go thru

Its partially working but not the exact rule with the code I am working right now

with that thing I deigned this one

if (SD.exists("001")) 
  {
    Serial.println("example.txt exists.");
    tmrpcm.play("001.wav");
  } else {
    Serial.println("example.txt doesn't exist.");
    tmrpcm.play("");
  }

But this one totally blocks the existing function of Arduino in beginning

Please correct it Sir

if (SD.exists("001"))   //test if a file named 001 exists
  {
    Serial.println("example.txt exists.");  //print a message about a different file that you did not test for
    tmrpcm.play("001.wav");  //then play a completely different file, which may not exist

Your code makes no sense
Note my comments added to your code snippet

Please post your complete sketch

OK actually its a garbage of more than 15000 lines

I ll make a simple clean script then post it

Ya Sir Please have a look here as it is now a very important part

#include <stdio.h>
#include <Keypad.h>
#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
TMRpcm tmrpcm;
#define SD_ChipSelectPin 53
const byte IN22 = 22;
const byte IN23 = 23;
const byte IN24 = 24;
const byte IN25 = 25;
boolean readValue22 ;
boolean readValue23 ;
boolean readValue24 ;
boolean readValue25 ;
void setup() 
{
  Serial.begin(9600);
  Serial.println("Begin..");
  tmrpcm.speakerPin = (46);
  if (!SD.begin(SD_ChipSelectPin))
  {
    Serial.println("SD fail");
    return;
  }
  pinMode (IN22, INPUT_PULLUP);
  pinMode (IN23, INPUT_PULLUP);
  pinMode (IN24, INPUT_PULLUP);
  pinMode (IN25, INPUT_PULLUP);
}
void loop() 
{
  if (digitalRead(22) == LOW)
    {
      tmrpcm.play("/6/001.wav");                           
      tmrpcm.setVolume(5);
      tmrpcm.loop(1);
      delay(10);
      while( digitalRead(22) == LOW);
    }
    if (digitalRead(37) == LOW)
    {
      tmrpcm.play("/5/001.wav");                           
      tmrpcm.setVolume(5);
      tmrpcm.loop(1);
      delay(10);
      while( digitalRead(37) == LOW);
    }
}

In this above section :-

I made 2 folders on SD Card named 5 and 6 in which folder 6 has some files but folder 5 has nothing and its completely empty

Now when I press Pin 22 the defined file runs

But doing same action with Pin 37

System gets hanged or distorted and rude sound starts almost

Just tested !!!

Why doesn't your code use the exists() function before trying to play the files ?

When I use to apply that one

The Player plays nothing and do nothing

just like

ALIVE BUT IN COMA !!!

Sir I tried all the way and failed too the same way there

So Kindly help me in solving the issue Please !!!!

Post a sketch with your attempt to use the exists() function

if (digitalRead(22) == LOW)
    {
      if(SD.exists("001"))
      {
      tmrpcm.play("/6/001.wav");
      SD.exists("/6/001");                             
      tmrpcm.setVolume(5);
      tmrpcm.loop(1);
      delay(10);
      while( digitalRead(22) == LOW);
      }
    }

This is just like

A Blind man throwing arrows and spear in a dark forest to kill the man eaters

No idea nothing just playing games with code !!!

if (digitalRead(22) == LOW)
    {
      if(SD.exists("001"))  //check for a file named 001
      {
      tmrpcm.play("/6/001.wav");  //play a completely different file
      SD.exists("/6/001");  //ignore whether or not the file you just played exists or not

Things are not getting any better, are they ?
See my comments

  • check whether the file you want to play exists
  • if so the play that file