//https://wokwi.com/projects/397402554280879105
#include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C lcd(0x27,16,2); //0x3f, 0x20, 0x27 // set the LCD address to 0x27 for a 16 chars and 2 line display
#define soundPin1 A0 // Sound detector 1 analog pin
#define soundPin2 A1 // Sound detector 2 analog pin
#define soundPin3 A2 // Sound detector 3 analog pin
#define soundPin4 A3 // Sound detector 4 analog pin
const float squareSize = 1;// Size of the square in meters
void setup() {
Serial.begin(9600);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Sound Tracking");
lcd.setCursor(0,1);
lcd.print("Aud Measurement");
delay(3000);
lcd.clear();
}
int sound1=0;
int sound2=0;
int sound3=0;
int sound4=0;
void loop() {
//Notice how each input is read twice with a delay of 10 microseconds between each reading.
sound1 = analogRead(soundPin1);
delay(10);
sound1 = analogRead(soundPin1);
delay(10);
sound2 = analogRead(soundPin2);
delay(10);
sound2 = analogRead(soundPin2);
delay(10);
sound3 = analogRead(soundPin3);
delay(10);
sound3 = analogRead(soundPin3);
delay(10);
sound4 = analogRead(soundPin4);
delay(10);
sound4 = analogRead(soundPin4);
delay(10);
Serial.print("sound1: ");
Serial.println(sound1);
Serial.print("sound2: ");
Serial.println(sound2);
Serial.print("sound3: ");
Serial.println(sound3);
Serial.print("sound4: ");
Serial.println(sound4);
// Convert analog readings to distance
float distance1 = map(sound1, 0, 1023, 0, squareSize);
float distance2 = map(sound2, 0, 1023, 0, squareSize);
float distance3 = map(sound3, 0, 1023, 0, squareSize);
float distance4 = map(sound4, 0, 1023, 0, squareSize);
// Calculate the average x and y positions of the sound source
float avgX = (distance1 + distance2) / 2;
float avgY = (distance3 + distance4) / 2;
// Output the calculated position
Serial.print("Sound source position: (");
Serial.print(avgX, 2);
Serial.print("m, ");
Serial.print(avgY, 2);
Serial.println("m)");
lcd.setCursor(0,0);
lcd.print("avgX:");
lcd.setCursor(6,0);
lcd.print(avgX);
lcd.setCursor(0,1);
lcd.print("avgY:");
lcd.setCursor(6,1);
lcd.print(avgY);
sound1 = 0;
sound2 = 0;
sound3 = 0;
sound4 = 0;
//delay(1000); // Delay for stability
}