Arduino - audio playback - ultrasonic sensors

Hi Dear friends .
We have a project with arduino, but it works wrong. We have three Ultrasonic sensors, one CD card and one audioplayer. We need to read the file from CD and send it to the audioplayer everytime at least one of ultrasonic sensors detects movement.
You can see my code below. It doesn't work correct. Please, if it's possible help to find my mistake or suggest me another code.
Thanks a lot for your time.

We’ll be looking forward to hearing from you!

Best regards,

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

TMRpcm tmrpcm;

//define Pins
int trigPin1 = A0;
int echoPin1 = A1;

int trigPin2 = A2;
int echoPin2 = A3;

int trigPin3 = A4;
int echoPin3 = A5;

// defines variables
long duration1;
int distance1;

long duration2;
int distance2;

long duration3;
int distance3;

void setup()
{

// Sets the trigPin as an Output
pinMode(trigPin1, OUTPUT);
pinMode(trigPin2, OUTPUT);
pinMode(trigPin3, OUTPUT);
// Sets the echoPin as an Input
pinMode(echoPin1, INPUT);
pinMode(echoPin2, INPUT);
pinMode(echoPin3, INPUT);
// Starts the serial communication
Serial.begin(9600);

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


void loop()
{
   // Clears the trigPin
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*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance1: ");
Serial.println(distance1);

//2
// Clears the trigPin
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration2 = pulseIn(echoPin2, HIGH);
// Calculating the distance
distance2= duration2*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance2: ");
Serial.println(distance2);

//3
// Clears the trigPin
digitalWrite(trigPin3, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration3 = pulseIn(echoPin3, HIGH);
// Calculating the distance
distance3= duration3*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance3: ");
Serial.println(distance3);

if (distance1 < 25)
 {
   delay(1000);
   tmrpcm.setVolume(6);
   tmrpcm.play("test.wav");
 }

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.