Étape 3: Test du capteur
Je veux tester si le capteur fonctionne, donc j’ai google il et trouver quoi que ce soit peut aider. Google est notre ami et ce lien peut s’agir de la référence :
http://arthursrobotorial.blogspot.HK/2012_12_01_archive.html
#define trigPin 5#define echoPin 6void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT);}int CheckDistance(){ long duration, distance; digitalWrite(trigPin, LOW); // Added this line delayMicroseconds(2); // Added this line digitalWrite(trigPin, HIGH);// delayMicroseconds(1000); - Removed this line delayMicroseconds(10); // Added this line digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; return distance;}void loop() { int testDistance = CheckDistance(); /// get object distance using ping /// if object is more than 50 cm away it is out of range if (testDistance >= 50 || testDistance <= 0) /// if object is more than 50 cm away it is out of range { Serial.println("Out of range"); } else /// object is closer than 50cm, print distance { Serial.print(testDistance); Serial.println(" cm"); delay(500); ///wait half a sec before next ping}
Dans l’IDE Arduino, sélectionnez Outils > Serial Monitor. Il montre la distance si réussit.