ESP32 Starter Kit
(continued from previous page)
if (dat[2] < 100) {
lcd.setCursor(15, 1);
lcd.print(" ");
}
}
}
void show_humidity() { //Display humidity
if (xht.receive(dat)) { //Check correct return to true
lcd.setCursor(0, 1);
lcd.print("Humidity:");
lcd.setCursor(10, 1);
lcd.print(dat[0]);
Serial.println("2");
if (dat[0] < 100) {
lcd.setCursor(12, 1);
lcd.print(" ");
}
}
}
float checkdistance() { //Acquire 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 show_distance() { //Display the distance
distance = checkdistance();
// if (distance < 2 || distance >= 400) { //Disolay "-1" if exceeding the detection␣
˓→range
// distance = -1;
// }
lcd.setCursor(0, 1);
lcd.print("distance:");
lcd.setCursor(9, 1);
lcd.print(distance);
if (distance < 100 && distance > 10) { //Eliminate the third residual digit when the␣
˓→value drops to two digits
lcd.setCursor(11, 1);
lcd.print(" ");
}
(continues on next page)
8.5. Arduino Project 161