Help triggering sound

Hello! Completely new to Arduino; working on building a cornhole-type game that keeps score and triggers a sound when the bean bag passes through the hole.

I've got IR Break Beam sensors set up and have managed to code the scoring system.

Having trouble triggering a file to play from an SD card though. I know that the set-up and speaker are working, as I've been able to get a sound to play using some simple sample code from the web. But when I try to integrate that into my existing code, I come up short.

Appreciate any insights from this group - I'm sure it answer will jump out at someone here!

Thanks in advance.

#include <SPI.h>
#include <SD.h>
#include "TMRpcm.h" // includes the library for music playback
#include <LiquidCrystal.h> // includes the library code for LCD

#define SENSORPIN1 6 // sets SENSORPIN1 as pin 6
#define SENSORPIN2 5 // sets SENSORPIN2 as pin 5
#define SENSORPIN3 8 // sets SENSORPIN3 as pin 8
#define SD_ChipSelectPin 10 // sets the SD card as pin 10

TMRpcm music; //Lib object is named "music"
LiquidCrystal lcd(0, 1, 2, 3, 4, 7); // initialize the library with the numbers of the interface pins

int sensorState1 = 0; // variable for reading IR sensor 1
int sensorState2 = 0; // variable for reading IR sensor 2
int HomeScore; // variable for Home Score
int AwayScore; // variable for Away Score

void setup() {

pinMode(SENSORPIN1, INPUT); // initiate SENSORPIN1 as an input
digitalWrite(SENSORPIN1, HIGH); // turn the pullup for SENSORPIN1 on

pinMode(SENSORPIN2, INPUT); // initiate SENSORPIN2 as an input
digitalWrite(SENSORPIN2, HIGH); // turn the pullup for SENSORPIN2 on

lcd.begin(16, 2); // set up the LCDs number of columns and rows:
setupScreen(); // Print a message to the LCD.

music.speakerPin = 9; // set speaker output as pin 9
}

void setupScreen() // what appears on the LCD screen at start
{
lcd.clear(); // clear LCD screen
lcd.setCursor(0,0); // set cursor position for LCD screen
lcd.print("FRANCE v GERMANY"); // what to display at start

HomeScore = 0; // value for HomeScore variable
AwayScore = 0; // value for AwayScore variable
}

void loop(){

sensorState1 = digitalRead(SENSORPIN1); // tells arduino to get the value of sensorState1 by reading SENSORPIN1

if (sensorState1 == LOW) { // if the value is LOW (broken) then...
HomeScore++; // advance Home Score by 1 pt
delay(500); // delay to prevent multiple scoring
music.play("test.wav"); // play file from SD card
}

sensorState2 = digitalRead(SENSORPIN2); // tells arduino to get the value of sensorState2 by reading SENSORPIN2

if (sensorState2 == LOW) {
HomeScore++;
delay(500);
}

{
lcd.setCursor(2, 1); // set the cursor position for the Home Score
lcd.print(HomeScore); // print the HomeScore variable
lcd.setCursor(12, 1); // set the cursor position for the Away Score
lcd.print(AwayScore); // print the AwayScore

}
}

What does that mean?

Please remember to use code tags when posting code

Come up short means I can't figure it out.

Sorry to not follow convention...what do you mean by using "code tags"? Happy to oblige once I know what it means :slight_smile: :grinning:

What is "it"? What actually happens that shouldn't, or doesn't happen that should? We're asking for a detailed description of the problem...

For an explanation of code tags, read the introductory threads at the top of the forum.

This is a very old, obsolete and non-portable version of:

pinMode(SENSORPIN1, INPUT_PULLUP); // initiate SENSORPIN1 as an input

Is the score incrementing?

Oh, didn't realize. Can you suggest an alternative?

When the IR sensor is broken, I want it to advance the score by 1 pt (and display on the LCD) as well play the sound "test.wav" from the SD card.

With the code I have, I can get the scoring to work and posted to the LCD. But the sound does not play.

I though the command "music.play" would trigger the sound but it does not.

I think I might be missing something in the setup?

Please add the code tags so people will be motivated to look.

Sorry, I don't know what is meant by "code tags". Can you give me an example?

Why can you not just go read the instructions? It's all in the threads I mentioned. It's like this:

#include <SPI.h>
#include <SD.h>
#include "TMRpcm.h" // includes the library for music playback
#include <LiquidCrystal.h> // includes the library code for LCD

#define SENSORPIN1 6 // sets SENSORPIN1 as pin 6
#define SENSORPIN2 5 // sets SENSORPIN2 as pin 5
#define SENSORPIN3 8 // sets SENSORPIN3 as pin 8
#define SD_ChipSelectPin 10 // sets the SD card as pin 10

TMRpcm music; //Lib object is named "music"
LiquidCrystal lcd(0, 1, 2, 3, 4, 7); // initialize the library with the numbers of the interface pins

int sensorState1 = 0; // variable for reading IR sensor 1
int sensorState2 = 0; // variable for reading IR sensor 2
int HomeScore; // variable for Home Score
int AwayScore; // variable for Away Score

void setup() {

  pinMode(SENSORPIN1, INPUT); // initiate SENSORPIN1 as an input
  digitalWrite(SENSORPIN1, HIGH); // turn the pullup for SENSORPIN1 on

  pinMode(SENSORPIN2, INPUT); // initiate SENSORPIN2 as an input
  digitalWrite(SENSORPIN2, HIGH); // turn the pullup for SENSORPIN2 on

  lcd.begin(16, 2); // set up the LCDs number of columns and rows:
  setupScreen(); // Print a message to the LCD.

  music.speakerPin = 9; // set speaker output as pin 9
}

void setupScreen() // what appears on the LCD screen at start
{
  lcd.clear(); // clear LCD screen
  lcd.setCursor(0, 0); // set cursor position for LCD screen
  lcd.print("FRANCE v GERMANY"); // what to display at start

  HomeScore = 0; // value for HomeScore variable
  AwayScore = 0; // value for AwayScore variable
}

void loop() {

  sensorState1 = digitalRead(SENSORPIN1); // tells arduino to get the value of sensorState1 by reading SENSORPIN1

  if (sensorState1 == LOW) { // if the value is LOW (broken) then...
    HomeScore++; // advance Home Score by 1 pt
    delay(500); // delay to prevent multiple scoring
    music.play("test.wav"); // play file from SD card
  }

  sensorState2 = digitalRead(SENSORPIN2); // tells arduino to get the value of sensorState2 by reading SENSORPIN2

  if (sensorState2 == LOW) {
    HomeScore++;
    delay(500);
  }

  {
    lcd.setCursor(2, 1); // set the cursor position for the Home Score
    lcd.print(HomeScore); // print the HomeScore variable
    lcd.setCursor(12, 1); // set the cursor position for the Away Score
    lcd.print(AwayScore); // print the AwayScore

  }
}

Notice how much easier it is to read, with auto formatting.

I think the visiting team might be angry that all their points go to the home team. :slight_smile:

Can you post the sketch that did play sound?

1 Like

Just found it but you beat me to it! Thanks.

Yes, while I'm drafting it up the Home Team gets all the points :smiley:

I noticed that in setup() you failed to do an SD.begin().

1 Like
#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm tmrpcm;

void setup(){
tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}

tmrpcm.setVolume(5);
tmrpcm.play("test.wav");
}

void loop(){  }

No, I didn't! This initializes the SD card?

What does the SD library documentation say?

@ToddL1962 That did it!!! Thank you so much. Really appreciate your insight.

1 Like