arduino / Arduino

Arduino IDE 1.x
https://www.arduino.cc/en/software
Other
14.18k stars 7.01k forks source link

Xodage 2d2 #11983

Closed Momoo2100 closed 2 months ago

Momoo2100 commented 2 months ago

int echoPin = 7; int triggerPin = 8;

int distance = 0; long readUltrasonicDistance(int triggerPin, int echoPin);

void setup() { Serial.begin(9600); pinMode(triggerPin, OUTPUT); digitalWrite(triggerPin, LOW); delayMicroseconds(2); digitalWrite(triggerPin, HIGH); delayMicroseconds(10); digitalWrite(triggerPin, LOW); pinMode(echoPin, INPUT);

pinMode(11, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); }

void loop() { distance = 0.01723 * readUltrasonicDistance(triggerPin, echoPin); Serial.println(distance);

if (distance >= 200) { digitalWrite(11, HIGH); digitalWrite(12, LOW); digitalWrite(13, LOW); } else { if (distance < 200 && distance >= 80) { digitalWrite(11, LOW); digitalWrite(12, HIGH); digitalWrite(13, LOW); } else { digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, HIGH); } }

delay(10); // Retarde un peu pour améliorer la simulation }

// Définition de la fonction readUltrasonicDistance long readUltrasonicDistance(int triggerPin, int echoPin) { // Envoie une impulsion de 10 microsecondes pour déclencher le capteur à ultrasons digitalWrite(triggerPin, LOW); delayMicroseconds(2); digitalWrite(triggerPin, HIGH); delayMicroseconds(10); digitalWrite(triggerPin, LOW);

// Lis la durée de l'impulsion renvoyée par le capteur long duration = pulseIn(echoPin, HIGH);

// Calcule la distance en cm long distance = duration * 0.034 / 2;

return distance; }