i am running my code if anyone is up front or to the left or right using ultrasonic sensors * 2 and 1 ir sensor and when i play the respective code i am hearing crackling noise but not the sound wich is intended to b heard. But wen i tried playing the same (just for audio) it worked but nw it is nt working wen i am using tat in the code. i am using sd card module to play the music from.pls help me out
#include <SoftwareSerial.h>
#include <SD.h>
#include <TMRpcm.h> //"to play WAV files"
#include <SPI.h> //"for serial peripheral interface"
TMRpcm tmrpcm;
const int chipselect = 9;
const int trig_left = 2;
const int echo_left = 3;
const int trig_right = 5;
const int echo_right = 6; //"10 to 13 pins are used for spi protocls"
const int ir = 13; // "miso to 12 mosi to 11 sck to 13 cs to 10 no changes must be made"
long duration_left;
long duration_right;
int distance_left;
int safe_left;
int distance_right;
int safe_right;
int ir_input;
void setup()
{
tmrpcm.speakerPin = 9; //"for making pin number 9 as speaker"
pinMode(trig_left, OUTPUT);
pinMode(echo_left, INPUT);
pinMode(trig_right, OUTPUT);
pinMode(echo_right, INPUT);
pinMode(ir, INPUT);
pinMode(7, OUTPUT);
digitalWrite(7,HIGH);
Serial.begin(9600);
if (!SD.begin(chipselect)) // "SD.begin returns if sd card readable or not"
{
Serial.println("SD fail");
return;
}
tmrpcm.setVolume(6); //"it is used to set the volume till 7 in speaker"
}
void loop()
{
digitalWrite(trig_left, LOW);
delayMicroseconds(2);
digitalWrite(trig_left, HIGH);
delayMicroseconds(10);
digitalWrite(trig_left, LOW);
duration_left = pulseIn(echo_left, HIGH);
distance_left = duration_left*0.034/2;
safe_left = distance_left;
if (safe_left<= 5)
{
tmrpcm.play("sl.wav");
delay(5000);
Serial.println("left unsafe");
}
delay(100);
digitalWrite(trig_right, LOW);
delayMicroseconds(2);
digitalWrite(trig_right, HIGH);
delayMicroseconds(10);
digitalWrite(trig_right, LOW);
duration_right = pulseIn(echo_right, HIGH);
distance_right = duration_right*0.034/2;
safe_right = distance_right;
if (safe_right<= 5)
{
tmrpcm.play("sr.wav");
delay(5000);
Serial.println("right unsafe");
}
delay(100);
ir_input = digitalRead(ir);
if (ir_input== HIGH)
{
tmrpcm.play("sf.play");
delay(5000);
}
delay(1000);
}