1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include <LiquidCrystal.h>
#define SR04_TRIG 9 #define SR04_ECHO 8
void setup(void){ lcd.begin(16, 2); pinMode(SR04_TRIG, OUTPUT); pinMode(SR04_ECHO, INPUT); }
void loop(void){ digitalWrite(SR04_TRIG, LOW); delayMicroseconds(2); digitalWrite(SR04_TRIG, HIGH); delayMicroseconds(10); digitalWrite(SR04_TRIG, LOW);
float temp = pulseIn(SR04_ECHO, HIGH); float distance_cm = (temp * 17) / 1000; lcd.setCursor(0, 1); lcd.print("DIS:"); lcd.print(distance_cm); lcd.print("cm"); lcd.print(" "); }
|