LCD1602超声波测距实例
昨天用DHT11做了个LCD1602的实例!今天移植到超声波测距来了!刚开始学的小白!大神轻点喷,多提宝贵意见共同进步!
LCD接法参考教科书,超声波的接法,我发送接数字8.反馈接数字9.
把误判大于2000cm的输出,直接一闪而过,正常读数保留三秒读数时间。
代码如下:
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int Tfa = 8;
int Ehk = 9;
float juli;
byte armsDown = {
0b00100,
0b01010,
0b00100,
0b00100,
0b01110,
0b10101,
0b00100,
0b01010
};
byte armsUp = {
0b00100,
0b01010,
0b00100,
0b10101,
0b01110,
0b00100,
0b00100,
0b01010
};
void setup(){
Serial.begin(9600);
pinMode(Tfa,OUTPUT);
pinMode(Ehk,INPUT);
lcd.begin(16,2);
lcd.print("lcd already ok");
/* lcd.createChar(1, armsDown);
// create a new character
lcd.createChar(2, armsUp);*/
delay(2000);
lcd.clear();
}
void loop()
{
digitalWrite(Tfa,LOW);
delayMicroseconds(2);
digitalWrite(Tfa,HIGH);
delayMicroseconds(10);
juli = pulseIn(Ehk,HIGH)/58.30;
Serial.print(juli);
Serial.print("cm");
Serial.println();
/*switch(chk){
case DHTLIB_OK:
Serial.println("ok");
break;
//case DHTLIB_ERROR_TIMEOUT:
Serial.println("ERROR_TIMEOUT");
break;
//case DHTLIB_ERROR_CHECKSUM:
Serial.println("CHECKSUM ERROR");
break;
default:
Serial.println("ok ko");
break;
}*/
lcd.setCursor(0,0);
lcd.print(juli);
lcd.print("cm");
//delay(3000);//wating 3s,clear lcd.
if(juli>=2000){
delay(50);}
else{
delay(3000);//wating 3s,clear lcd.
}
//fang zhi wupan
//delay(3000);//wating 3s,clear lcd.
lcd.setCursor(0,0);
lcd.clear();
lcd.setCursor(4, 1);
// draw the little man, arms down:
lcd.write(2);
delay(300);
lcd.setCursor(4, 1);
// draw him arms up:
lcd.write(1);
delay(300);
}
你好,厉害啊 有没有原理图啥的哈哈 有接线图参考吗 木子齐 发表于 2018-9-27 12:59
有接线图参考吗
LCD接法参考教科书,超声波的接法,我发送接数字8.反馈接数字9.
把误判大于2000cm的输出,直接一闪而过,正常读数保留三秒读数时间。 更新一下I2C的1602事例:
#include<LiquidCrystal_I2C.h>
#include <Wire.h>
//LiquidCrystal_I2C Lcd(0x27,16,02);
LiquidCrystal_I2C lcd(0x27,20,4);
//LiquidCrystal lcd(12,11,5,4,3,2);
int Tfa = 8;
int Ehk = 9;
float juli;
byte armsDown = {
0b00100,
0b01010,
0b00100,
0b00100,
0b01110,
0b10101,
0b00100,
0b01010
};
byte armsUp = {
0b00100,
0b01010,
0b00100,
0b10101,
0b01110,
0b00100,
0b00100,
0b01010
};
void setup(){
Serial.begin(9600);
pinMode(Tfa,OUTPUT);
pinMode(Ehk,INPUT);
// lcd.begin(16,2);
lcd.init();
lcd.backlight();
lcd.print("lcd already ok");
/* lcd.createChar(1, armsDown);
// create a new character
lcd.createChar(2, armsUp);*/
delay(2000);
lcd.clear();
}
void loop()
{
digitalWrite(Tfa,LOW);
delayMicroseconds(2);
digitalWrite(Tfa,HIGH);
delayMicroseconds(10);
juli = pulseIn(Ehk,HIGH)/58.30;
Serial.print(juli);
Serial.print("cm");
Serial.println();
/*switch(chk){
case DHTLIB_OK:
Serial.println("ok");
break;
//case DHTLIB_ERROR_TIMEOUT:
Serial.println("ERROR_TIMEOUT");
break;
//case DHTLIB_ERROR_CHECKSUM:
Serial.println("CHECKSUM ERROR");
break;
default:
Serial.println("ok ko");
break;
}*/
lcd.setCursor(0,0);
lcd.print(juli);
lcd.print("cm");
//delay(3000);//wating 3s,clear lcd.
if(juli>=2000){
delay(50);}
else{
delay(3000);//wating 3s,clear lcd.
}
//fang zhi wupan
//delay(3000);//wating 3s,clear lcd.
lcd.setCursor(0,0);
lcd.clear();
lcd.setCursor(4, 1);
// draw the little man, arms down:
lcd.write(2);
delay(300);
lcd.setCursor(4, 1);
// draw him arms up:
lcd.write(1);
delay(300);
}
很奇怪的是,我用1.8.6可以正常烧录,用1.8.5验证一直报错!望高手解答一下 juli = pulseIn(Ehk,HIGH)/58.30;
请问,计算距离的这个程序,为什么是58.3,有点不明白? JasonChing 发表于 2018-11-29 16:09
juli = pulseIn(Ehk,HIGH)/58.30;
请问,计算距离的这个程序,为什么是58.3,有点不明白? ...
声音速度340m/s
然后算出来的,小学数学 奈何col 发表于 2018-12-3 09:07
声音速度340m/s
然后算出来的,小学数学
搞明白了,没注意单位是us
页:
[1]
2