ESP32 Starter Kit
(continued from previous page)
int TrigPin = 13; //Connect Trig pin to io13
float checkdistance() { //Acquire the distance
// preserve a short low level to ensure a clear high pulse:
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
//Trigger the sensor by a high pulse of 10um or longer
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
// Read the signal from the sensor: a high level pulse
//Duration is detected from the point sending "ping" command to the time receiving␣
˓→echo signal (unit: um).
float distance = pulseIn(EchoPin, HIGH) / 58.00; //Convert into distance
delay(10);
return distance;
}
void setup() {
Serial.begin(9600);//Set the baud rate to 9600
pinMode(TrigPin, OUTPUT);//Set Trig pin to output
pinMode(EchoPin, INPUT); //Set Echo pin to input
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("Keyestudio");
}
void loop() {
distance = checkdistance();
if (distance < 2 || distance >= 400) { //Display "-1" if exceeding the detection␣
˓→range
distance = -1;
}
if(distance < 100 && distance > 10){ //Eliminate the shadow of the third␣
˓→digit when the value drops to two digits
lcd.setCursor(11,1);
lcd.print(" ");
}
if(distance < 10){ //Eliminate two-digit shadows when the value drops to␣
˓→one digit
lcd.setCursor(10,1);
lcd.print(" ");
}
lcd.setCursor(0,1);
lcd.print("distance:");
lcd.setCursor(9,1);
lcd.print(distance);
delay(200);
}
8.5. Arduino Project 139