|
本帖最后由 wwwusr 于 2018-3-11 21:56 编辑
IIC地址那里,估计得改一下才能适用你的硬件
用到了一个超声波传感器SR04的库,在后面
[mw_shl_code=c,true]/*
SR04+IIC1602
by:Techie
QQ:13531382
*/
#include <Wire.h>
#include "SR04.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3D, 16, 2);
#define TRIG_PIN 2
#define ECHO_PIN 3
SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);
long a;
void setup()
{
lcd.init();
lcd.backlight();
Serial.begin(9600);
lcd.print("Distance is:");
lcd.setCursor(9, 1);
lcd.print("cm");
}
void loop()
{
for (int i = 18; i > 0; i--)
{
a = sr04.Distance();
delay(5);
Serial.print(a);
Serial.println("cm");
lcd.setCursor(6, 1);
lcd.print(a);
}
for (int j = 6; j > 0; j--)
{
lcd.setCursor(6, 1);
lcd.print(" ");
}
}[/mw_shl_code]
|
|