mixly ARDUINO图形编程LCD1602显示的超声波测距程序
Mixly米思齐 •
代码:
//Arduino超声波测距程序
#include <LiquidCrystal_SoftI2C.h>
#include <SoftI2CMaster.h>
LiquidCrystal_SoftI2C mylcd(0x27,16,2,A5,A4);
volatile int distance;
float checkdistance_2_4() {
digitalWrite(2, LOW);
delayMicroseconds(2);
digitalWrite(2, HIGH);
delayMicroseconds(10);
digitalWrite(2, LOW);
float distance = pulseIn(4, HIGH) / 58.00;
delay(10);
return distance;
}
void setup(){
distance = 0;
pinMode(2, OUTPUT);
pinMode(4, INPUT);
mylcd.init();
mylcd.backlight();
}
void loop(){
distance = checkdistance_2_4();
mylcd.clear();
mylcd.setCursor(0, 0);
mylcd.print("distance");
mylcd.setCursor(0, 1);
mylcd.print(String(String(distance, DEC)) + String("cm"));
delay(200);