#define PIN_BUTTON A1  
#define PIN_TRIG A3
#define PIN_ECHO A2
#define vert A4
#define orange A5
#define rouge A0 




void setup() {
  Serial.begin(115200);
  pinMode(PIN_BUTTON, INPUT_PULLUP);  
  pinMode(PIN_TRIG, OUTPUT);
  pinMode(PIN_ECHO, INPUT);
  pinMode(rouge, OUTPUT);
  pinMode(orange, OUTPUT);
  pinMode(vert, OUTPUT);
}

void loop() {
  while (digitalRead(PIN_BUTTON) == HIGH) {
    delay(10);  
  

  // Start a new measurement:
  digitalWrite(PIN_TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(PIN_TRIG, LOW);


  int duration = pulseIn(PIN_ECHO, HIGH);
  float distance = duration / 58.0;  
  Serial.print("Distance in CM: ");
  Serial.println(distance);
  if(distance<40){
  digitalWrite(rouge, HIGH);
  digitalWrite(orange, LOW);
  digitalWrite(vert, LOW);
}else{
  if(distance>=40 && distance<=120){
  digitalWrite(rouge, LOW);
  digitalWrite(orange, HIGH);
  digitalWrite(vert, LOW);
}else{
  if(distance>120){
  digitalWrite(rouge, LOW);
  digitalWrite(orange, LOW);
  digitalWrite(vert, HIGH);
}
}
}}
  delay(100);  
}

Loading
st-nucleo-l031k6