const int pinTrig = D3;
const int pinEcho = D2;
const int switchPin = A1;  
const int rouge = A6;
const int jaune = A4;
const int vert = A5;
const int buzzer = A7;

void setup() {
  Serial.begin(9600);
  pinMode(pinTrig, OUTPUT);
  pinMode(pinEcho, INPUT);
  pinMode(switchPin, INPUT);  
  pinMode(rouge, OUTPUT);
  pinMode(jaune, OUTPUT);
  pinMode(vert, OUTPUT);
  pinMode(buzzer, OUTPUT);
}

void loop() {
  if (digitalRead(switchPin) == HIGH) {
    digitalWrite(pinTrig, HIGH);
    delayMicroseconds(10);
    digitalWrite(pinTrig, LOW);
    int duration = pulseIn(pinEcho, HIGH);
    float distance = duration / 58.0;
    int x=distance*3.3;
    Serial.print("distance in CM: ");
    Serial.println(distance);
    if (distance < 30) {
      digitalWrite(rouge, HIGH);
      digitalWrite(jaune, LOW);
      digitalWrite(vert, LOW);
      digitalWrite(buzzer, HIGH); 
      delay(x);
      digitalWrite(buzzer, LOW); 
      delay(x);
    } else if (distance >= 30 && distance <= 120) {
      digitalWrite(rouge, LOW);
      digitalWrite(jaune, HIGH);
      digitalWrite(vert, LOW);
      digitalWrite(buzzer, HIGH); 
      delay(x);
      digitalWrite(buzzer, LOW); 
      delay(x);
    } else if (distance > 120) {
      digitalWrite(rouge, LOW);
      digitalWrite(jaune, LOW);
      digitalWrite(vert, HIGH);
      noTone(buzzer);
    }
  } else {
    // Si l'interrupteur est en position OFF, éteindre tout
    digitalWrite(rouge, LOW);
    digitalWrite(jaune, LOW);
    digitalWrite(vert, LOW);
    noTone(buzzer);
  }
}
$abcdeabcde151015202530fghijfghij