Cannot print to LCD with Wave file playing

Trying to make a program that plays a bird call from a SD card. then shows you the Name of bird from SD card. it will not work without a delay. it will not work with a bird call only a simple wave playing. Any suggestion. was thinking get Name from sd card first then display after sound. but cannot get it to work. any help would be great. I am not good at programing and just try to get things to work.


/*  Needs to be wav, bits 8, hz 16000, mono
1horn
3
4
5  Saddle back
6 LSK F
7 LSK M
8 Hihi


//Arduino Uno I2C module LCD
//GND - GND
//5V - Vcc
//Analog Pin 4 - SDA
//Analog pin 5 - SCL


 SD card attached to SPI bus as follows:
 ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
 ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
 ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
 ** CS - depends on your SD card shield or module.
 		Pin 2 used here for consistency with other Arduino examples

*/

//LCD liberys

#include <Wire.h>               // this replaces all stuff if no module
#include <LiquidCrystal_I2C.h>  // this replaces all stuff if no module

LiquidCrystal_I2C lcd(0x27, 16, 2);  // this replaces all stuff if no module

//SD card liburays
#include <SD.h>


// wave file playing libery
#include <TMRpcm.h>

TMRpcm tmrpcm;  // create an object for use in this sketch
#include <SPI.h>

const int chipSelect = 2;  // for sd connection

int SW1;  //the bottom pushed value.  asign a button to each one in loop
int SW2;
int SW3;
int SW4;

void setup() {
  //configure pin to input and enable the internal pull-up resistor
  pinMode(5, INPUT_PULLUP);  //Botton  D5 and GRD for botton
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);

  tmrpcm.speakerPin = 3;  //speacker pin  was 10 had to change to 3

  lcd.begin();      // this replaces all stuff if no module
  lcd.backlight();  // this replaces all stuff if no module
  lcd.clear();      // this replaces all stuff if no module
  lcd.setCursor(1, 0);
  lcd.print("Bruce Harrison");
  lcd.setCursor(1, 1);
  lcd.print("*  Bird Game  *");
  delay(500);

  lcd.noDisplay();  // Turn off the display:
  delay(500);
  lcd.display();  // Turn on the display:
  delay(500);
  lcd.clear();  // this replaces all stuff if no module

  ////////// added everything in here

  Serial.begin(9600);  // Open serial communications and wait for port to open:

  while (!Serial)
    ;  // wait for Serial Monitor to connect. Needed for native USB port boards only:

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {  // trying thing
    Serial.println("initialization failed. Things to check:");
    Serial.println("1. is a card inserted?");
    Serial.println("2. is your wiring correct?");
    Serial.println("3. did you change the chipSelect pin to match your shield or module?");
    Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
    while (true)
      ;
  }
  Serial.println("initialization done.");

  /////play horn to see if it works

  if (!SD.begin(chipSelect)) {
    return;  // don't do anything more if not
  }
  tmrpcm.volume(1);
  tmrpcm.play("1.wav");  //the sound file "1" will play each time the arduino powers up, or is reset
  delay(4000);


  /////////// Writing the file on serail monitor
  File dataFile = SD.open("2e.txt");  // open the file, only one can be open at a time, so have to close after
  if (dataFile) {
    while (dataFile.available()) {
      Serial.write(dataFile.read());
    }
  } else {
    Serial.println("error opening datalog.txt");  // if the file isn't open, pop up an error:
  }
  dataFile.close();

  //////////////////////////////////////////////////////////////////////////////////////////////////////

  // area for LCD stuff
  {
    File dataFile = SD.open("2e.txt");
    lcd.setCursor(3, 0);  // this puts everything on same spot
    while (dataFile.available()) {
      char c = dataFile.read();  //this is the file from SD card
      lcd.print(c);              // this prints the data file 1 letter at a time. one after the other
      delay(50);                 // how farst it prints the letter
    }
  }
  dataFile.close();  // closing eng txt file

  {
    File dataFile = SD.open("2m.txt");
    lcd.setCursor(3, 1);  // this puts everything on same spot
    while (dataFile.available()) {
      char c = dataFile.read();  //this is the file from SD card
      lcd.print(c);              // this prints the data file 1 letter at a time. one after the other
      delay(50);                 // how farst it prints the letter
    }
  }

  dataFile.close();  // closing ma txt file
}

void loop() {
}

Hello, do yourself a favour and please read How to get the best out of this forum and post accordingly (including code with code tags and necessary documentation for your ask like your exact circuit and power supply, links to components etc).

➜ can you edit your post with the :memo: that is below your text and add code tags?

Hi Jackson
Have tried to edit it so its a bit better. only started Arduino at 50 and just trying to learn by myself. Taking a while to get it all sorted and there is a lot to take in.
Currently using a Uno for this running of USB . sd Card has wave files and name Files. One wave and 2 txt file for each bird.
The problem i have is if i remove the Delay the text will not display on the LCD. or make the Delay smaller than 4000.
Thanks

the tmrpcm library uses the standard Arduino SD library to output sound asynchronously (it allows code to run after you issue the command) but it does rely heavily on interrupts and access to the SD card.

As they say in the documentation

Known Limitations

This library can be very processor intensive, and code execution during playback will be slower than normal
Processing load can be reduced by using lower quality sounds encoded at a lower sample rate (8khz minimum)
May interfere with other libraries that rely on interrupts. The isPlaying() disable() or noInterrupts()
functions can be used to prevent parallel code execution.

I doubt you can do any SD access at the same time you are playing a sound as the SD card will already be busy.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.