I am having challenges getting this sketch to work. I have modified Danielsz's sketch (thanks SO MUCH!) to include more audio tracks triggered by the HR-SC04 sensor and It works fine. Then I added a relay into the sketch to be turned on at the same time as an audio track. The error message in the Arduino IDE is "expected '}' at the end of input. How can I get this to work? Please let me know if I have done this incorrectly and what I can do to resolve the issue. Thanks!
/*
// include SPI, MP3, Servo and SD libraries
#include <SPI.h> // We will use the hardware SPI pins: CLK (13), MISO (12), MOSI (11)
#include "Adafruit_VS1053.h"
#include <SD.h>
int track1, track2, track3, track4, track5, track6, track7, track8, track9, track10;
const int pingPin = 8;
const int echoPin = 9;
unsigned int duration, inches;
int relay = 7; // relay switch on pin 7
// These are the pins used for the music maker shield
#define SHIELD_RESET -1 // VS1053 reset pin (unused!)
#define SHIELD_CS 7 // VS1053 chip select pin (output)
#define SHIELD_DCS 6 // VS1053 Data/command select pin (output)
// These are common pins between breakout and shield
#define CARDCS 4 // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3 // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer =
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
void setup() {
track1=0;
track2=0;
track3=0;
track4=0;
track5=0;
track6=0;
track7=0;
track8=0;
track9=0;
track10=0;
pinMode(8,OUTPUT);
pinMode(9,INPUT);
Serial.begin(9600);
Serial.println("File From Distance Test");
pinMode(relay, OUTPUT);
if (! musicPlayer.begin()) { // initialise the music player
Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
while (1);
}
Serial.println(F("VS1053 found"));
SD.begin(CARDCS); // initialise the SD card
// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(1,1);
}
void ping() {
digitalWrite(pingPin, LOW); // Ensure pin is low
delayMicroseconds(2);
digitalWrite(pingPin, HIGH); // Start ranging
delayMicroseconds(5); // with 5 microsecond burst
digitalWrite(pingPin, LOW); // End ranging
duration = pulseIn(echoPin, HIGH); // Read echo pulse
inches = duration / 74 / 2; // Convert to inches
Serial.println(inches); // Display result
if ((inches>3) && (inches<5)) {
Serial.println("1");
delay(100);
}
if ((inches>5) && (inches<7)) {
Serial.println("2");
delay(100);
}
if ((inches>7) && (inches<9)) {
Serial.println("3");
delay(100);
}
if ((inches>9) && (inches<11)) {
Serial.println("4");
delay(100);
}
if ((inches>11) && (inches<13)) {
Serial.println("5");
delay(100);
}
if ((inches>13) && (inches<15)) {
Serial.println("6");
delay(100);
}
if ((inches>15) && (inches<17)) {
Serial.println("7");
delay(100);
}
if ((inches>17) && (inches<19)) {
Serial.println("8");
delay(100);
}
if ((inches>19) && (inches<21)) {
Serial.println("9");
delay(100);
}
if ((inches>21) && (inches<23)) {
Serial.println("10");
delay(100) ;}
}
void loop() {
delay(50);
ping();
if ((inches>3) && (inches<5)) {
if (track1==0){
musicPlayer.playFullFile("track001.mp3");
track1=1;
track2=0;
track3=0;
track4=0;
track5=0;
track6=0;
track7=0;
track8=0;
track9=0;
track10=0;
digitalWrite(relay, HIGH); // turn the relay on (HIGH is the voltage level)
delay(10000); // wait for 10 seconds
digitalWrite(relay, LOW); // turn the relay off by making the voltage LOW
}
if ((inches>5) && (inches<7)) {
if (track2==0){
musicPlayer.playFullFile("track002.mp3");
track1=0;
track2=1;
track3=0;
track4=0;
track5=0;
track6=0;
track7=0;
track8=0;
track9=0;
track10=0;
digitalWrite(relay, HIGH); // turn the LED on (HIGH is the voltage level)
delay(10000); // wait for a second
digitalWrite(relay, LOW); // turn the LED off by making the voltage LOW
}
if ((inches>7) && (inches<9)) {
if (track3==0){
musicPlayer.playFullFile("track003.mp3");
track1=0;
track2=0;
track3=1;
track4=0;
track5=0;
track6=0;
track7=0;
track8=0;
track9=0;
track10=0;
digitalWrite(relay, HIGH); // turn the relay on (HIGH is the voltage level)
delay(10000); // wait for 10 seconds
digitalWrite(relay, LOW); // turn the relay off by making the voltage LOW
}
if ((inches>9) && (inches<11)) {
if (track4==0){
musicPlayer.playFullFile("track004.mp3");
track1=0;
track2=0;
track3=0;
track4=1;
track5=0;
track6=0;
track7=0;
track8=0;
track9=0;
track10=0;
digitalWrite(relay, HIGH); // turn the relay on (HIGH is the voltage level)
delay(10000); // wait for 10 seconds
digitalWrite(relay, LOW); // turn the relay off by making the voltage LOW
}
if ((inches>11) && (inches<13)) {
if (track5==0){
musicPlayer.playFullFile("track005.mp3");
track1=0;
track2=0;
track3=0;
track4=0;
track5=1;
track6=0;
track7=0;
track8=0;
track9=0;
track10=0;
digitalWrite(relay, HIGH); // turn the relay on (HIGH is the voltage level)
delay(10000); // wait for 10 seconds
digitalWrite(relay, LOW); // turn the relay off by making the voltage LOW
}
if ((inches>13) && (inches<15)) {
if (track1==6){
musicPlayer.playFullFile("track006.mp3");
track1=0;
track2=0;
track3=0;
track4=0;
track5=0;
track6=1;
track7=0;
track8=0;
track9=0;
track10=0;
digitalWrite(relay, HIGH); // turn the relay on (HIGH is the voltage level)
delay(10000); // wait for 10 seconds
digitalWrite(relay, LOW); // turn the relay off by making the voltage LOW
}
if ((inches>15) && (inches<17)) {
if (track7==0){
musicPlayer.playFullFile("track007.mp3");
track1=0;
track2=0;
track3=0;
track4=0;
track5=0;
track6=0;
track7=1;
track8=0;
track9=0;
track10=0;
digitalWrite(relay, HIGH); // turn the relay on (HIGH is the voltage level)
delay(10000); // wait for 10 seconds
digitalWrite(relay, LOW); // turn the relay off by making the voltage LOW
}
if ((inches>17) && (inches<19)) {
if (track8==0){
musicPlayer.playFullFile("track008.mp3");
track1=0;
track2=0;
track3=0;
track4=0;
track5=0;
track6=0;
track7=0;
track8=1;
track9=0;
track10=0;
digitalWrite(relay, HIGH); // turn the relay on (HIGH is the voltage level)
delay(10000); // wait for 10 seconds
digitalWrite(relay, LOW); // turn the relay off by making the voltage LOW
}
if ((inches>19) && (inches<21)) {
if (track9==0){
musicPlayer.playFullFile("track009.mp3");
track1=0;
track2=0;
track3=0;
track4=0;
track5=0;
track6=0;
track7=0;
track8=0;
track9=1;
track10=0;
digitalWrite(relay, HIGH); // turn the relay on (HIGH is the voltage level)
delay(10000); // wait for 10 seconds
digitalWrite(relay, LOW); // turn the relay off by making the voltage LOW
}
if ((inches>21) && (inches<23)) {
if (track10==0){
musicPlayer.playFullFile("track010.mp3");}
track1=0;
track2=0;
track3=0;
track4=0;
track5=0;
track6=0;
track7=0;
track8=0;
track9=0;
track10=1;
digitalWrite(relay, HIGH); // turn the relay on (HIGH is the voltage level)
delay(10000); // wait for 10 seconds
digitalWrite(relay, LOW); // turn the relay off by making the voltage LOW
}