Arduino学习笔记——车辆报警显示设计
用超声波检测距离,如果距离车越近,蜂鸣器响的越快,并在led1602显示。
#include \"SR04.h\"#define TRIG_PIN 2#define ECHO_PIN 3#include <Wire.h>#include <LiquidCrystal_I2C.h>LiquidCrystal_I2C lcd(0x27,16,2);SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);long a;void setup() {pinMode(9,OUTPUT);Serial.begin(9600);delay(1000);lcd.init(); //初始化LCDlcd.backlight(); //打开背光}void loop() {a=sr04.Distance();Serial.print(a);Serial.println(\"cm\");delay(10);if(a<=30){tone(9,1200);delay(3*a);noTone(9);delay(3*a);}else{noTone(9);}lcd.setCursor(0,0);lcd.print(\"JULI:\");lcd.setCursor(0,1);lcd.print(a*0.01);lcd.setCursor(5,1);lcd.print(\"M\");}