I'm writing a code where I want the display oled to show me the temperature of the hand, so when the hand is less than 10 cm, the display will show the temperature, otherwise it will say "far away". The problem is that when it is less than 10 cm it shows the temperature, but then it shows other symbols until it shows the new temperature. The time showing the temperature and the symbols it's the same and it is the time I set in the delay. Here below y leave the code.
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0)
{
Serial.println("Out of range");
}else
{if(distance<10){
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 32);
display.setTextSize(2);
temp=mlx.readObjectTempC();
display.println(temp);
display.display();
delay(2000);
} else
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 32);
display.println("far away");
display.display();
delay(2000);
}
{
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}