/*
 * Simple Wokwi Microphone Demo
 */
/*
void setup() {
  Serial.begin(115200);
  pinMode(13, OUTPUT);
}

void loop() {
  int sample = analogRead(A0);
  Serial.println(sample);
  digitalWrite(13, abs(sample - 512) > 50);
}
*/

// Define the analog input pin for the microphone
const int MIC_PIN = A0;
#define ECHO_PIN1 2
#define TRIG_PIN1 3
#define ECHO_PIN2 9
#define TRIG_PIN2 8
// Define the number of samples to average
const int NUM_SAMPLES = 32;

void setup() {
  // Initialize the serial communication for debugging
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(TRIG_PIN1, OUTPUT);
  pinMode(ECHO_PIN1, INPUT);
  pinMode(TRIG_PIN2, OUTPUT);
  pinMode(ECHO_PIN2, INPUT);
}


float readDistanceCM() {
  digitalWrite(TRIG_PIN1, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN1, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN1, LOW);
  int duration = pulseIn(ECHO_PIN1, HIGH);
  return duration * 0.034 / 2;
}

float readDistanceCM1() {
  digitalWrite(TRIG_PIN2, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN2, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN2, LOW);
  int duration = pulseIn(ECHO_PIN2, HIGH);
  return duration * 0.034 / 2;
}


void loop() {
  // Read and average the voltage values from the microphone
  int micValue = 0;
  for (int i = 0; i < NUM_SAMPLES; i++) {
    micValue += analogRead(MIC_PIN);
    delayMicroseconds(100);
  }
  micValue /= NUM_SAMPLES;

  // Convert the voltage value to an amplitude value
  float ampValue = micValue * 0.0049 / sqrt(2.0);  // Assumes 5V reference voltage and AC coupling
  ampValue *= 1000.0;  // Convert to millivolts
  float freqValue = micValue * 0.0049;  // Assumes 5V reference voltage
  freqValue = freqValue * 1000.0 / 2.0;
  // Debugging: Print the amplitude value to the serial monitor
  /*Serial.print("Amplitude: ");
  Serial.print(ampValue);
  Serial.println(" mV");

  Serial.print("Frequency: ");
  Serial.print(freqValue);
  Serial.println(" Hz");*/
  //ADJUST THRESHOLD HERE
  
  digitalWrite(13, abs(ampValue) > 1900.00);
  if(ampValue >1900.00 & freqValue>1350.00)
  {
    Serial.println("emergency vehicle detected");
    Serial.println("Priority to lane 2");
    digitalWrite(8, HIGH);
    digitalWrite(7, LOW);
    delay(10000);
    //digitalWrite(8, LOW);
    //digitalWrite(7, HIGH);
  }


  float distance = readDistanceCM();

  Serial.print("lane1: ");
  Serial.println(readDistanceCM());

  Serial.print("lane2: ");
  Serial.println(readDistanceCM1());

  if((readDistanceCM()>readDistanceCM1())&(abs(ampValue) < 1900.00))
  {
    
    Serial.println("Priority to lane 1");
    digitalWrite(8, LOW);
    digitalWrite(7, HIGH);
    delay(10000);
    //digitalWrite(8, HIGH);
    //digitalWrite(7, LOW);
  }

  if(readDistanceCM()<readDistanceCM1())
  {
    
    Serial.println("Priority to lane 2");
    digitalWrite(8, HIGH);
    digitalWrite(7, LOW);
    delay(10000);
    //digitalWrite(8, LOW);
    //digitalWrite(7, HIGH);
  }

  // Wait a short period before reading the microphone again
  delay(500);
}



// Define the analog input pin for the microphone