Why do I have to open Serial Monitor to display non nan values ?

Why do I have to open Serial Monitor to display non nan values retrieved from DHT22, :confused:
what am I doing wrong in the code below ?

#include <DHT.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
// OLED display TWI address
#define OLED_ADDR   0x3C
// reset pin not used on 4-pin OLED module
Adafruit_SSD1306 display(-1);  // -1 = no reset pin
// 128 x 64 pixel display
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
#define DHTPIN D3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {

  delay(10);
  dht.begin();
  pinMode (DHTPIN, OUTPUT);
  /// initialize and clear display
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  //  display.drawRect(1, 1, display.width()-1, display.height()-1, WHITE);   // draws the outer rectangular boundary on the screen
  display.display();
  // display a pixel in each corner of the screen
  display.drawPixel(0, 0, WHITE);
  display.drawPixel(127, 0, WHITE);
  display.drawPixel(0, 63, WHITE);
  display.drawPixel(127, 63, WHITE);
  // display a line of text
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(20, 18);
  display.print("Welcome");
  display.setCursor(25, 40);
  display.print("San");
  
  // update display with all of the above graphics
  display.display();
}
void loop() {
  // put your main code here, to run repeatedly:
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 1);
  display.print("Showing Temperature");
  // display Temperature
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(1, 18);
  display.print("T : ");
  display.print(t);
  display.setTextSize(1);
  display.print((char)247);
  display.setTextSize(2);
  display.print("C");
  display.setCursor(1, 40);
  display.print("H : ");
  display.print(h);
  display.print(" %");
  // update display with all of the above graphics
  display.display();
}

After uploading code it does will display nan but when I open serial monitor it works fine displaying actual values from DHT22 :o

it looks like the loop function is not getting executed at all, if I dont hit the serial monitor icon.

Hi.

I have no idea what 'nan' is.
I do not see any serial communication in your sketch.
But opening the serial monitor, causes a reset.
Perhaps your screen needs some extra time to reset after power on.
So try to have a larger delay() in line 18, and if that doesn't help, put in some extra delays in other lines, just to test.

Also posted at:

If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. When you post links please always use the chain links icon on the toolbar to make them clickable.

MAS3:
Hi.

I have no idea what 'nan' is.
I do not see any serial communication in your sketch.
But opening the serial monitor, causes a reset.
Perhaps your screen needs some extra time to reset after power on.
So try to have a larger delay() in line 18, and if that doesn't help, put in some extra delays in other lines, just to test.

thanks for reply, I tried your suggestion, but it didnt help. if i do not have serial monitor open loop function doesnt work. if I have serial monitor open the OLED updates, if i close serial monitor window the text on doest updates from the loop function.
why is loop function dependent on serial monitor window being open ?

MAS3:
Hi.

I have no idea what ‘nan’ is.
I do not see any serial communication in your sketch.
But opening the serial monitor, causes a reset.
Perhaps your screen needs some extra time to reset after power on.
So try to have a larger delay() in line 18, and if that doesn’t help, put in some extra delays in other lines, just to test.

NAN is normally “No A Number” it is normally used to denote division by Zero.

It is difficult to provide any help to the other person since they did not indicate which Arduino board they are using.

I would suggest that they post the question on the adafruit forums. they may have some better insight into the workings or not workings of the adafruit devices.

Are you using pin 0 or 1 by any chance ?