Stap 3: Test de sensor
Ik wil testen als de sensor werkt, dus ik google het en vind alles wat kan helpen. Google is onze vriend en deze verbinding kan een van de verwijzing:
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}
In de Arduino IDE, Selecteer Gereedschappen > seriële Monitor. Blijkt de afstand als slaagt.