|
#include <Servo.h>
Servo myServo;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
char "°"=1;
const int TrigPin = 2; //发出超声波
const int EchoPin = 3; //收到反射回来的超声波
float cm; //因为测得的距离是浮点型的
int pos;
void setup() {
myServo.attach(9);
lcd.init();
lcd.backlight();
Serial.begin(9600); //设置波特率
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop() {
for (pos = 15; pos < 165; pos += 1) {
myServo.write(pos);
delay(50);
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2); // delayMicroseconds在更小的时间内延时准确
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW); //通过这里控制超声波的发射
cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
cm = (int(cm * 100.0)) / 100.0; //保留两位小数
switch (pos) {
case 15:
Serial.print("15°");
Serial.print(cm);
Serial.print("cm");
Serial.println();
lcd.setCursor(0, 0);
lcd.print("15°");
lcd.setCursor(6, 0);
lcd.print(cm);
lcd.setCursor(14, 0);
lcd.print("Cm");
break;
case 45:
Serial.print("45°");
Serial.print(cm);
Serial.print("cm");
Serial.println();
lcd.setCursor(0, 0);
lcd.print("45°");
lcd.setCursor(6, 0);
lcd.print(cm);
lcd.setCursor(14, 0);
lcd.print("Cm");
break;
case 90:
Serial.print("90°");
Serial.print(cm);
Serial.print("cm");
Serial.println();
lcd.setCursor(0, 0);
lcd.print("90°");
lcd.setCursor(6, 0);
lcd.print(cm);
lcd.setCursor(14, 0);
lcd.print("Cm");
break;
case 120 :
Serial.print("120°");
Serial.print(cm);
Serial.print("cm");
Serial.println();
lcd.setCursor(0, 0);
lcd.print("120°");
lcd.setCursor(6, 0);
lcd.print(cm);
lcd.setCursor(14, 0);
lcd.print("Cm");
break;
case 165 :
Serial.print("165° ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
lcd.setCursor(0, 0);
lcd.print("165°");
lcd.setCursor(6, 0);
lcd.print(cm);
lcd.setCursor(14, 0);
lcd.print("Cm");
break;
}
}
for (pos = 165; pos > 15; pos -= 1) {
myServo.write(pos);
delay(50);
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2); // delayMicroseconds在更小的时间内延时准确
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW); //通过这里控制超声波的发射
cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
cm = (int(cm * 100.0)) / 100.0; //保留两位小数
switch (pos) {
case 15:
Serial.print("15° ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
lcd.setCursor(0, 0);
lcd.print("15°");
lcd.setCursor(6, 0);
lcd.print(cm);
lcd.setCursor(14, 0);
lcd.print("Cm");
break;
case 45:
Serial.print("45° ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
lcd.setCursor(0, 0);
lcd.print("45°");
lcd.setCursor(6, 0);
lcd.print(cm);
lcd.setCursor(14, 0);
lcd.print("Cm");
break;
case 90:
Serial.print("90° ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
lcd.setCursor(0, 0);
lcd.print("90°");
lcd.setCursor(6, 0);
lcd.print(cm);
lcd.setCursor(14, 0);
lcd.print("Cm");
break;
case 120 :
Serial.print("120° ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
lcd.setCursor(0, 0);
lcd.print("120°");
lcd.setCursor(6, 0);
lcd.print(cm);
lcd.setCursor(14, 0);
lcd.print("Cm");
break;
case 165 :
Serial.print("165° ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
lcd.setCursor(0, 0);
lcd.print("165°");
lcd.setCursor(6, 0);
lcd.print(cm);
lcd.setCursor(14, 0);
lcd.print("Cm");
break;
}
}
}
LCD显示“ ° ”乱码求解
|
|