Stap 4: De Code
<p>#include <br></p><p>LiquidCrystal lcd(12, 11, 5, 4, 3, 2);</p><p>int pingPin = 7; int inPin = 8; long duration, inches, cm; int indec, cmdec; int inchconv = 147; int cmconv = 59; String s1, s2;</p><p>void setup() { lcd.begin(16, 2); pinMode(pingPin, OUTPUT); pinMode(inPin, INPUT); }</p><p>void loop() { digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(10); digitalWrite(pingPin, LOW);</p><p> duration = pulseIn(inPin, HIGH);</p><p> inches = microsecondsToInches(duration); indec = (duration - inches * inchconv) * 10 / inchconv; cm = microsecondsToCentimeters(duration); cmdec = (duration - cm * cmconv) * 10 / cmconv; s1 = String(inches) + "." + String(indec) + "in" + " "; s2 = String(cm) + "." + String(cmdec) + "cm" + " "; lcd.setCursor(0, 0); lcd.print(s1); lcd.setCursor(0,1); lcd.print(s2);</p><p> delay(900); }</p><p>long microsecondsToInches(long microseconds) { return microseconds / inchconv; }</p><p>long microsecondsToCentimeters(long microseconds) { return microseconds / cmconv; }</p>