|
|
本帖最后由 code-AR 于 2013-7-30 22:31 编辑
在论坛学习了这么久了,正好手中有个超声波模块和1602液晶屏。就想把这两个整合起来,让超声波模块获取距离信息,在1602上面。先上图:
完成效果1
完成效果2
面包板图
源代码:
#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;
}
}
希望各位朋友多多提意见,O(∩_∩)O谢谢 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|