ESP32 Starter Kit
(continued from previous page)
void loop() {
// put your main code here, to run repeatedly:
if (xht.receive(dat)) { //Check correct return to true
temperature = dat[2];
if (temperature > 75) { // Determine whether value is greater than 75
digitalWrite(green_led, LOW);
digitalWrite(red_led, HIGH);
digitalWrite(blue_led, LOW);
digitalWrite(yellow_led,LOW);
}
if (temperature < 75 && temperature > 50) { //Determine whether value is between 50␣
˓→and 75
digitalWrite(green_led, LOW);
digitalWrite(red_led, LOW);
digitalWrite(blue_led, LOW);
digitalWrite(yellow_led,HIGH);
}
if (temperature < 50 && temperature > 25) { //Determine whether value is between 25␣
˓→and 50
digitalWrite(green_led, HIGH);
digitalWrite(red_led, LOW);
digitalWrite(blue_led, LOW);
digitalWrite(yellow_led,LOW);
}
if (temperature < 25) { //Determine whether value is smaller than 25
digitalWrite(green_led, LOW);
digitalWrite(red_led, LOW);
digitalWrite(blue_led, HIGH);
digitalWrite(yellow_led,LOW);
}
}
delay(1500); //Delay 1500ms
}
7. Code Explanation
xht11 xht(Pin); Set the instance named xht and add the pins
unsigned char dat[4] = { 0, 0, 0, 0 }; dat[0] is the integer part of the humidity value. dat[1] is the decimal part of the
humidity value. dat[2] is the integer part of the temperature value, and dat[3] is the decimal part of the temperature
value
&& (value < 100 && value > 75) means that, it is true only both expressions satisfying the condition, or else it is false.
132 Chapter 8. Arduino Tutorial