The code :
int mic1 = A2;
int mic2 = A1;
int motor1= 2;
int motor2= 3;
int motor3= 4;
int motor4= 5;
int baseline = 337;
int amp1=0;
int amp2=0;
int difference=0;
int enA = 9;
int enB = 10;
int start;
int end;
const int echoPin= 7;
const int trigPin= 6;
long duration;
int distance;
void setup(){
Serial.begin(9600);
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
pinMode(motor3, OUTPUT);
pinMode(motor4, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}
void loop(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
start=0;
start= millis();
end = start;
int reading1 = analogRead(mic1);
amp1= abs(reading1-baseline);//determine the peak
int reading2 = analogRead(mic2);
amp2= abs(reading2-baseline);//determine the peak
difference= amp1-amp2;//peak intensity difference
Serial.print("mic1: ");
Serial.print(amp1);
Serial.print(" mic2: ");
Serial.print(amp2);
Serial.print(" difference: ");
Serial.print(difference);
Serial.print(" distance:");
Serial.print(distance);
Serial.print("\n");
if(difference>9 && amp1+amp2>250){//steers right based on the sound
analogWrite(enA,90);
analogWrite(enB,90);
roboright();
}
else if(difference<-9 && amp1+amp2>250){//steers left based on the sound
analogWrite(enA,90);
analogWrite(enB,90);
roboleft();
}// jatin sachdev
else if(difference>=-5 && difference<=5 && amp1+amp2>250 ){// drives towards the source of the sound
// while(start-end<0.5){
analogWrite(enA,180);
analogWrite(enB,180);
roboforward(); //}
/*stop();
drivetosource();*/
}
if(distance<4){// makes the car recognise when it has reached the sound of source
stop();
}
}
/*void drivetosource(){
analogWrite(enA, 100);
analogWrite(enB, 100);
while (amp1<335 || amp2<335){
if(amp1<337 || amp2<337 && difference<5 && difference>-5 && amp1+amp2>300){
roboforward();}
else if(difference>5){
roboright();
}
else if(difference<-5 ){
roboleft();
}
else if(amp1>335 && amp2>335){
stop();
}}
}*/
void roboright(){
digitalWrite(motor1,LOW);
digitalWrite(motor2,HIGH);
digitalWrite(motor3,LOW);
digitalWrite(motor4,HIGH );
}
void roboleft(){
digitalWrite(motor1,HIGH);
digitalWrite(motor2,LOW);
digitalWrite(motor3,HIGH);
digitalWrite(motor4,LOW);
}
void stop(){
digitalWrite(motor1,LOW);
digitalWrite(motor2,LOW);
digitalWrite(motor3,LOW);
digitalWrite(motor4,LOW);
}
void roboforward(){
digitalWrite(motor1,LOW);
digitalWrite(motor2,HIGH);
digitalWrite(motor3,HIGH);
digitalWrite(motor4,LOW);
}
void robobackward(){
digitalWrite(motor1,HIGH);
digitalWrite(motor2,LOW);
digitalWrite(motor3,LOW);
digitalWrite(motor4,HIGH);
}
void locatesource(){
//do nothing
}
p.s the commented out code are the different variations i was trying
I have infact made a prototype tutorial for it on arduino project hub, also, witht the diagram, the details of microphones etc at this link
https://projecthub.arduino.cc/arunav1/sound-tracking-rover-13f9b6
And jremington, i have the microphones pointing towards the front of the rover, and i use a speaker emitting a fixed frequency(440-470 hz)as a sound source in my case , and it tracks to it. I m sure that the motor noise is neglible compared to it, or may be is it?