#define echoPin A2
#define trigPin A1
#define buttonPin A5
#define ledverte A4
#define ledorangé A3
#define ledrouge A0
#define buzzer A6
long duration, distance;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buttonPin, INPUT);
pinMode(ledverte, INPUT);
pinMode(ledorangé, INPUT);
pinMode(ledrouge, INPUT);
pinMode(buzzer, INPUT);
}
void loop() {
if (digitalRead(buttonPin) == LOW ) {
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
int duration = pulseIn(echoPin, HIGH);
float distance = duration / 58.0;
Serial.print("Distance in CM: ");
Serial.println(distance);
if(distance<100){
digitalWrite(ledrouge, HIGH);
digitalWrite(ledorangé, LOW);
digitalWrite(ledverte, LOW);
tone(buzzer, 1000);}
else if(distance>=100 && distance<=300){
digitalWrite(ledrouge, LOW);
digitalWrite(ledorangé, HIGH);
digitalWrite(ledverte, LOW);
tone(buzzer, 300);}
else if(distance>300){
digitalWrite(ledrouge, LOW);
digitalWrite(ledorangé, LOW);
digitalWrite(ledverte, HIGH);
noTone(buzzer);
}
}
delay(1000);
}
Loading
st-nucleo-l031k6
st-nucleo-l031k6