#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int tx1Pin=18;
int rx1Pin=19;
int inputPin=8; // 定义超声波信号接收接口
int outputPin=9; // 定义超声波信号触发接口
void setup()
{
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.print("Hello,world!");
pinMode(inputPin, INPUT);
pinMode(outputPin, OUTPUT);
lcd.begin(16,2); //定义lcd列数及行数
lcd.clear();//清屏
lcd.setCursor(0,0);//光标位置置于0,0
pinMode(tx1Pin,OUTPUT);
pinMode(rx1Pin,INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(outputPin, LOW); // 使发出发出超声波信号接口低电平2μs
delayMicroseconds(2);
digitalWrite(outputPin, HIGH); // 使发出发出超声波信号接口高电平10μs,这里是至少10μs
delayMicroseconds(10);
digitalWrite(outputPin, LOW); // 保持发出超声波信号接口低电平
int distance = pulseIn(inputPin, HIGH); // 读出脉冲时间
distance= distance/58; // 将脉冲时间转化为距离(单位:厘米)
delay(5);
lcd.clear();//清屏
lcd.setCursor(0,0);//光标从这里开始
lcd.print("distance is");
lcd.setCursor(0,1);//换行后显示距离
lcd.print(distance);
Serial.printl(distance);
delay(1000);
}
通过编译了,LCD显示,测距离都正常,可不能传输数据,哪位高手能告诉我,为什么不能输出测距离的数据?
|