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