i have a micro sd card module in which i have saved two audio named 0001.wav and 0002.wav. a ultrasonic detects a object in range and blinks the leds. the code for detection and blinking led works fine but when i added the code for audio, it does not work. please help me out with it.
[ #include <SD.h> // need to include the SD library
#define SD_ChipSelectPin 4
#include <TMRpcm.h> // also need to include this library...
#include <SPI.h>
TMRpcm tmrpcm; // create an object for use in this sketc
int step = 0;
int led1 = 7;
int led2 = 8;
int led3 = 2;
const int echoPin1 = 6;
const int trigPin1 = 5;
long duration1;
int distance1;
void setup() {
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
tmrpcm.speakerPin = 9;
myservo1.attach(3);
Serial.begin(9600);
}
void loop()
{
static unsigned lastMilli = 0;
static unsigned lastLED = 2;
static const byte LEDs[] = { led1, led2, led3 };
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration1 = pulseIn(echoPin1, HIGH);
// Calculating the distance
distance1=(duration1/2)/29.1;
// Prints the distance on the Serial Monitor
Serial.print("Distance1: ");
Serial.println(distance1);
delay(100);
switch(step) {
case 0: do_step_0(); break;
case 1: do_step_1(); break;
case 2: do_step_2(); break;
case 3: do_step_3(); break;
case 4: do_step_4(); break;
}
}
void do_step_0() {
if ((distance1>7) && (distance1<=20))
{
step = 1;
}
else
{
myservo1.write(0);
}
}
void do_step_1() {
myservo1.write(150);
step = 2;
}
void do_step_2() {
if ((distance1>7) && (distance1<=20))
{
tmrpcm.play("0001.wav");
digitalWrite(led1,HIGH);
delay(1000);
digitalWrite(led1,LOW);
step = 3;
}
else {
step = 0;
}
}
void do_step_3() {
if ((distance1>7) && (distance1<=20))
{
tmrpcm.play("0002.wav");
digitalWrite(led2,HIGH);
delay(1000);
digitalWrite(led2,LOW);
step = 4;
}
else {
step = 0;
}
}
void do_step_4() {
if ((distance1>7) && (distance1<=20))
{
tmrpcm.play("0001.wav");
digitalWrite(led3,HIGH);
delay(1000);
digitalWrite(led3,LOW);
step = 2;
}
else {
step = 0;
}
}
]