|
Post by 邱老师 on Jan 3, 2021 10:35:13 GMT
将测得的距离显示在LCD屏上 视频链接网盘:https://pan.baidu.com/s/1c-NondY2RMzd_1RYh7ts7Q 提取码:je0r 接线:液晶接线不变, 超声波:trig接D7 Echo接D6 别忘了接电源线,这里用了面包板,主要为了方便演示。 #include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int TrigPin = 7;
const int EchoPin = 6;
float cm;
void setup() {
lcd. begin(16, 2);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT) ;
}
void loop()
{
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite (TrigPin, LOW);
int timer=-500;
cm=pulseIn(EchoPin, HIGH)/58.0;
cm=(int (cm*100.0))/100.0;
delay(500);
if(millis()>timer +500)
{
lcd. print ("Distancce:");
lcd. setCursor(6,1);
lcd. print (cm);
lcd. print ("cm ");
lcd. home();
timer=millis()+500;
}
}
|
|
程澄
New Member
ура
Posts: 29
|
Post by 程澄 on Jan 17, 2021 9:39:07 GMT
|
|