Dumb beginner question...

Sorry, I know the issue im having probably lies within one of the most basic beginner solutions, using mills (), but Ive exhausted all possible resources ive found (and have used 3-4 different, but comparable methods), and I still cant get this program working.

At this point, the error Im getting is an 'invalid use of non static member function, u8g2.sendBuffer()', which from what Ive gathered is a pretty general error regarding user/library defined functions and syntax, but Ive tried to use the library reference manual's suggested setup, and a modifed setup using millis(), and havent had any luck with either.

So, what Im trying to do is use a temperature/humidity sensor to display to an OLED screen. The screen library setup is here, and the reference manual itself can be found in the tabs on the right: setup_tutorial · olikraus/u8g2 Wiki · GitHub

The most successful code either used both delays or eliminated the screen's delay, but made the screen blink intermittently (both confusing the sensor delay) which is here:

// DHT Temperature & Humidity Sensor
// Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries
// Released under an MIT license.

// Depends on the following Arduino libraries:
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#define DHTPIN            2         // Pin which is connected to the DHT sensor.

// Uncomment the type of sensor in use:
#define DHTTYPE           DHT11     // DHT 11 
//#define DHTTYPE           DHT22     // DHT 22 (AM2302)
//#define DHTTYPE           DHT21     // DHT 21 (AM2301)

// See guide for details on sensor wiring and usage:
//   https://learn.adafruit.com/dht/overview

U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

void setup() {
  u8g2.begin();
  u8g2.enableUTF8Print();
  dht.begin();
  u8g2.println("DHTxx Unified Sensor Example");
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Temperature");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println(" *C");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println(" *C");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println(" *C");  
  u8g2.println("------------------------------------");
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Humidity");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println("%");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println("%");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println("%");  
  u8g2.println("------------------------------------");
  // Set delay between sensor readings based on sensor details.
  delayMS = sensor.min_delay / 1000;
}

void loop() {
  // Delay between measurements.
  delay(delayMS);
  u8g2.clearBuffer();
  u8g2.clearDisplay();
  u8g2.home();
  u8g2.setFont(u8g2_font_u8glib_4_hr);
  u8g2.setCursor(0,15);
    {// Get temperature event and print its value.
    sensors_event_t event;  
    dht.temperature().getEvent(&event);
    if (isnan(event.temperature)) {
      u8g2.println("Error reading temperature!");
    }
    else {
      u8g2.print("Temperature: ");
      u8g2.print(event.temperature);
      u8g2.println(" *C");
    }
    // Get humidity event and print its value.
    dht.humidity().getEvent(&event);
    if (isnan(event.relative_humidity)) {
      u8g2.println("Error reading humidity!");
    }
    else {
      u8g2.print("Humidity: ");
      u8g2.print(event.relative_humidity);
      u8g2.println("%");
    }  
  } 
  u8g2.sendBuffer();      // transfer internal memory to the display
}

But when I try to use the millis() check, i get an 'invalid use of non-static member function' on the line 'u8g2.sendBuffer':

// DHT Temperature & Humidity Sensor
// Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries
// Released under an MIT license.

// Depends on the following Arduino libraries:
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library


#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#define DHTPIN            2         // Pin which is connected to the DHT sensor.

// Uncomment the type of sensor in use:
#define DHTTYPE           DHT11     // DHT 11 
//#define DHTTYPE           DHT22     // DHT 22 (AM2302)
//#define DHTTYPE           DHT21     // DHT 21 (AM2301)

// See guide for details on sensor wiring and usage:
//   https://learn.adafruit.com/dht/overview

U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
DHT_Unified dht(DHTPIN, DHTTYPE);

unsigned long delayMS;
// constants won't change:
const long interval = 1000;           // interval at which to buffer (milliseconds)
bool Buffer=false;
unsigned long currentMillis = millis();
unsigned long previousMillis = millis();

void setup() {
  u8g2.begin();
  dht.begin();
  u8g2.enableUTF8Print();
  u8g2.println("DHTxx Unified Sensor Example");
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Temperature");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println(" *C");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println(" *C");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println(" *C");  
  u8g2.println("------------------------------------");
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Humidity");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println("%");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println("%");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println("%");  
  u8g2.println("------------------------------------");
    // Set delay between sensor readings based on sensor details.
 delayMS = sensor.min_delay / 1000;
}

void loop(){
  u8g2.clearBuffer();
     u8g2.home();
     u8g2.setFont(u8g2_font_u8glib_4_hr);
     u8g2.setCursor(0,15);
     if (currentMillis - previousMillis >= interval){
        // save the last time
        previousMillis = currentMillis;
        delay(delayMS);
        // Get temperature event and print its value.
        sensors_event_t event;  
        dht.temperature().getEvent(&event);
          if(isnan(event.temperature)) {
          u8g2.println("Error reading temperature!");
          }
          else {
          u8g2.print("Temperature: ");
          u8g2.print(event.temperature);
          u8g2.println(" *C");
          }
          // Get humidity event and print its value.
          dht.humidity().getEvent(&event);
          if (isnan(event.relative_humidity)) {
          u8g2.println("Error reading humidity!");
          }
          else {
          u8g2.print("Humidity: ");
          u8g2.print(event.relative_humidity);
          u8g2.println("%");
          }
     }
   u8g2.sendBuffer;
}

Ive also tried using the interval to digitalRead and digitalWrite an associated boolean value, and tried using that to tell the sendBuffer function to execute if the boolean value equals one of the values (true or false) but this has also not worked

Ah, apparently I did -_- Now it compiles and uploads, but the screen is still blank.

Think it might be something in the timing? I'm not sure how mills is Vs delay as far as how much time makes up the remainder of the clock during the delay, but since the interval is 1000, but the delay is also 1000, would they need to be complimentary somehow (as is 60 seconds in a minute) or is it just a continuous count either way?

Well I suppose that may be a dumb question as well, since I used unsigned long for both...

Please post your updated code. It might be just that change or you might have changed more.

Sorry, here it is. Thanks

// DHT Temperature & Humidity Sensor
// Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries
// Released under an MIT license.

// Depends on the following Arduino libraries:
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library


#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#define DHTPIN            2         // Pin which is connected to the DHT sensor.

// Uncomment the type of sensor in use:
#define DHTTYPE           DHT11     // DHT 11 
//#define DHTTYPE           DHT22     // DHT 22 (AM2302)
//#define DHTTYPE           DHT21     // DHT 21 (AM2301)

// See guide for details on sensor wiring and usage:
//   https://learn.adafruit.com/dht/overview

U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
DHT_Unified dht(DHTPIN, DHTTYPE);

unsigned long delayMS;
// constants won't change:
const long interval = 1000;           // interval at which to buffer (milliseconds)
bool Buffer=false;
unsigned long currentMillis = millis();
unsigned long previousMillis = millis();

void setup() {
  u8g2.begin();
  dht.begin();
  u8g2.enableUTF8Print();
  u8g2.println("DHTxx Unified Sensor Example");
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Temperature");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println(" *C");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println(" *C");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println(" *C");  
  u8g2.println("------------------------------------");
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Humidity");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println("%");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println("%");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println("%");  
  u8g2.println("------------------------------------");
    // Set delay between sensor readings based on sensor details.
 delayMS = sensor.min_delay / 1000;
}

void loop(){
     u8g2.clearBuffer();
     u8g2.home();
     u8g2.setFont(u8g2_font_u8glib_4_hr);
     u8g2.setCursor(0,15);
     if (currentMillis - previousMillis >= interval){
        // save the last time
        previousMillis = currentMillis;
        delay(delayMS);
        // Get temperature event and print its value.
        sensors_event_t event;  
        dht.temperature().getEvent(&event);
          if(isnan(event.temperature)) {
          u8g2.println("Error reading temperature!");
          }
          else {
          u8g2.print("Temperature: ");
          u8g2.print(event.temperature);
          u8g2.println(" *C");
          }
          // Get humidity event and print its value.
          dht.humidity().getEvent(&event);
          if (isnan(event.relative_humidity)) {
          u8g2.println("Error reading humidity!");
          }
          else {
          u8g2.print("Humidity: ");
          u8g2.print(event.relative_humidity);
          u8g2.println("%");
          }
     }
   u8g2.sendBuffer();
}

Hi

You have a couple of problems:-

Firstly you never update the currentMillis variable with the result of millis() other than when you define both currentMillis and previousMillis at the start of the program so it is most unlikely that the test

if (currentMillis - previousMillis >= interval)

will ever evaluate to true.

Change this line to

if (millis() >= previousMillis + interval)

Also

Change this line

previousMillis = currentMillis;

to this

previousMillis = millis();

Which will update your previousMillis variable correctly (you could get rid of the currentMillis variable entierly).

Secondly every time you go round loop() you are executing a clearBuffer() and and a sendBuffer() to the display:-

void loop(){
     u8g2.clearBuffer();
     u8g2.home();
     u8g2.setFont(u8g2_font_u8glib_4_hr);
     u8g2.setCursor(0,15);
     if (currentMillis - previousMillis >= interval){
      //
      // Display code removed for clarity
      //
     }
   u8g2.sendBuffer();
}

So what is happening is that you are going round the loop() function clearing the buffer then sending the buffer to the display. Every second you send some information to the display but the very next time through loop() you clear that information from the buffer and send the buffer to the display. Hence you only see a blank display. Move the code inside the millis() if block and everything should display correctly.

Ian

Thanks a lot! Well, seems Ive had some success, in that now it displays correctly, but it goes between reading the temperature correctly and saying "error reading humidity", or displaying an error for both. Heres the updated code:

// DHT Temperature & Humidity Sensor
// Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries
// Released under an MIT license.

// Depends on the following Arduino libraries:
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library


#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#define DHTPIN            2         // Pin which is connected to the DHT sensor.

// Uncomment the type of sensor in use:
#define DHTTYPE           DHT11     // DHT 11 
//#define DHTTYPE           DHT22     // DHT 22 (AM2302)
//#define DHTTYPE           DHT21     // DHT 21 (AM2301)

// See guide for details on sensor wiring and usage:
//   https://learn.adafruit.com/dht/overview

U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
DHT_Unified dht(DHTPIN, DHTTYPE);

unsigned long delayMS;
// constants won't change:
const long interval = 1000;           // interval at which to buffer (milliseconds)
unsigned long currentMillis = millis();
unsigned long previousMillis = millis();

void setup() {
  u8g2.begin();
  dht.begin();
  u8g2.enableUTF8Print();
  u8g2.println("DHTxx Unified Sensor Example");
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Temperature");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println(" *C");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println(" *C");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println(" *C");  
  u8g2.println("------------------------------------");
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Humidity");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println("%");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println("%");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println("%");  
  u8g2.println("------------------------------------");
    // Set delay between sensor readings based on sensor details.
 delayMS = sensor.min_delay / 1000;
}

void loop(){
     u8g2.home();
     u8g2.setFont(u8g2_font_u8glib_4_hr);
     u8g2.setCursor(0,15);
     if (millis() >= previousMillis + interval){
        u8g2.clearBuffer();
        // save the last time
        previousMillis = millis();
        delay(delayMS);
        // Get temperature event and print its value.
        sensors_event_t event;  
        dht.temperature().getEvent(&event);
          if(isnan(event.temperature)) {
          u8g2.println("Error reading temperature!");
          }
          else {
          u8g2.print("Temperature: ");
          u8g2.print(event.temperature);
          u8g2.println(" *C");
          }
          // Get humidity event and print its value.
          dht.humidity().getEvent(&event);
          if (isnan(event.relative_humidity)) {
          u8g2.println("Error reading humidity!");
          }
          else {
          u8g2.print("Humidity: ");
          u8g2.print(event.relative_humidity);
          u8g2.println("%");
          }
   }
   u8g2.sendBuffer();
}

Referring to Reply #8 ...

I would have simply added the line

currentMillis = millis();

as the first thing in loop()

Using currentMillis is useful if there are several time tests in one iteration of loop() because all the tests can use the same value.

...R

Change this line to

NO! Do NOT do that.

Subtraction involving unsigned types is guaranteed to work. Addition is NOT. The original code structure was right.

PaulS:
NO! Do NOT do that.

Well spotted. I had missed that.

...R

Oh, alright. I'll take another look at everything here shortly and see if I can't figure out something more comparable to the subtraction comparator, and I'll post the updated code in a bit. Thanks!

CatDadJynx:
Oh, alright. I'll take another look at everything here shortly and see if I can't figure out something more comparable to the subtraction comparator, and I'll post the updated code in a bit. Thanks!

The subtraction is correct in the code in Reply #7.

All that is missing is the line (as the first thing in loop() )

currentMillis = millis();

...R

Okay, here's the updated code:

// DHT Temperature & Humidity Sensor
// Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries
// Released under an MIT license.

// Depends on the following Arduino libraries:
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library


#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#define DHTPIN            2         // Pin which is connected to the DHT sensor.

// Uncomment the type of sensor in use:
#define DHTTYPE           DHT11     // DHT 11 
//#define DHTTYPE           DHT22     // DHT 22 (AM2302)
//#define DHTTYPE           DHT21     // DHT 21 (AM2301)

// See guide for details on sensor wiring and usage:
//   https://learn.adafruit.com/dht/overview

U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
DHT_Unified dht(DHTPIN, DHTTYPE);

unsigned long delayMS;
// constants won't change:
const long interval = 1000;           // interval at which to buffer (milliseconds)
unsigned long currentMillis = millis();
unsigned long previousMillis = millis();

void setup() {
  u8g2.begin();
  dht.begin();
  u8g2.enableUTF8Print();
  u8g2.println("DHTxx Unified Sensor Example");
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Temperature");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println(" *C");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println(" *C");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println(" *C");  
  u8g2.println("------------------------------------");
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Humidity");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println("%");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println("%");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println("%");  
  u8g2.println("------------------------------------");
    // Set delay between sensor readings based on sensor details.
 delayMS = sensor.min_delay / 1000;
}

void loop(){
     u8g2.home();
     u8g2.setFont(u8g2_font_u8glib_4_hr);
     u8g2.setCursor(0,15);
     if (previousMillis >= currentMillis - interval){
        u8g2.clearBuffer();
        // save the last time
        currentMillis = previousMillis;
        delay(delayMS);
        // Get temperature event and print its value.
        sensors_event_t event;  
        dht.temperature().getEvent(&event);
          if(isnan(event.temperature)) {
          u8g2.println("Error reading temperature!");
          }
          else {
          u8g2.print("Temperature: ");
          u8g2.print(event.temperature);
          u8g2.println(" *C");
          }
          // Get humidity event and print its value.
          dht.humidity().getEvent(&event);
          if (isnan(event.relative_humidity)) {
          u8g2.println("Error reading humidity!");
          }
          else {
          u8g2.print("Humidity: ");
          u8g2.print(event.relative_humidity);
          u8g2.println("%");
          }
   }
   u8g2.sendBuffer();
}

But now, there is nothing on the display at all. Sorry, wasnt sure how to set things up correctly, but i know whatever changes I made are probably incorrect.

Robin2:
The subtraction is correct in the code in Reply #7.

All that is missing is the line (as the first thing in loop() )

currentMillis = millis();

...R

Sorry, somehow overlooked this reply. Trying now.

Hmm... closer. Now the readout alternates between reading the temperature and (error reading humidity) and an error for both. Also, there were noise pixels at the bottom (which i attributed to a junky screen) but now they alternate between a wider and smaller array of them as well. Heres the current code:

// DHT Temperature & Humidity Sensor
// Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries
// Released under an MIT license.

// Depends on the following Arduino libraries:
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library


#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#define DHTPIN            2         // Pin which is connected to the DHT sensor.

// Uncomment the type of sensor in use:
#define DHTTYPE           DHT11     // DHT 11 
//#define DHTTYPE           DHT22     // DHT 22 (AM2302)
//#define DHTTYPE           DHT21     // DHT 21 (AM2301)

// See guide for details on sensor wiring and usage:
//   https://learn.adafruit.com/dht/overview

U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
DHT_Unified dht(DHTPIN, DHTTYPE);

unsigned long delayMS;
// constants won't change:
const long interval = 1000;           // interval at which to buffer (milliseconds)
bool Buffer=false;
unsigned long currentMillis = millis();
unsigned long previousMillis = millis();

void setup() {
  u8g2.begin();
  dht.begin();
  u8g2.enableUTF8Print();
  u8g2.println("DHTxx Unified Sensor Example");
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Temperature");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println(" *C");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println(" *C");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println(" *C");  
  u8g2.println("------------------------------------");
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Humidity");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println("%");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println("%");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println("%");  
  u8g2.println("------------------------------------");
    // Set delay between sensor readings based on sensor details.
 delayMS = sensor.min_delay / 1000;
}

void loop(){
     currentMillis = millis();
     u8g2.clearBuffer();
     u8g2.home();
     u8g2.setFont(u8g2_font_u8glib_4_hr);
     u8g2.setCursor(0,15);
     if (currentMillis - previousMillis >= interval){
        // save the last time
        previousMillis = currentMillis;
        delay(delayMS);
        // Get temperature event and print its value.
        sensors_event_t event;  
        dht.temperature().getEvent(&event);
          if(isnan(event.temperature)) {
          u8g2.println("Error reading temperature!");
          }
          else {
          u8g2.print("Temperature: ");
          u8g2.print(event.temperature);
          u8g2.println(" *C");
          }
          // Get humidity event and print its value.
          dht.humidity().getEvent(&event);
          if (isnan(event.relative_humidity)) {
          u8g2.println("Error reading humidity!");
          }
          else {
          u8g2.print("Humidity: ");
          u8g2.print(event.relative_humidity);
          u8g2.println("%");
          }
     }
   u8g2.sendBuffer();
}

currentMillis is supposed to be the current time.
1)
So you set it to millis() in the beginning of loop(); like when you look at the clock to see what the time is.
2)
And you subtract the previous time that you took some action when it was time (previousMillis)

Just start with an extremely simple example using the Serial port. Below a stripped version of your code. It also contains some debug print statements so you can see what the values for currentMillis and previousMillis are.

unsigned long delayMS;
// constants won't change:
const long interval = 1000;           // interval at which to buffer (milliseconds)
unsigned long currentMillis = millis();
unsigned long previousMillis = millis();

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  Serial.print("currentMillis = "); Serial.println(currentMillis);
  Serial.print("previousMillis = "); Serial.println(previousMillis);
  delay(200);
  if (previousMillis >= currentMillis - interval)
  {
    // save the last time
    currentMillis = previousMillis;
    Serial.println("Hallo");
  }
}

Run it and look at the serial monitor output; the values will always be 0.

Next add the below line at the beginning of loop()

 // get the time
  currentMillis = millis();

and change the line where you remember the last time

       currentMillis = previousMillis;

to

    previousMillis = currentMillis;

Now look what happens in serial monitor. currentMillis will now update, previousMillis will not change. The code will however not print "Hallo" because previousMillis is always 0; so your condition is wrong. Change the if statement to

 if (currentMillis - previousMillis >= interval)

And see what happens.

currentMillis = 0
previousMillis = 0
currentMillis = 199
previousMillis = 0
currentMillis = 400
previousMillis = 0
currentMillis = 601
previousMillis = 0
currentMillis = 801
previousMillis = 0
currentMillis = 1001
previousMillis = 0
Hallo
currentMillis = 1202
previousMillis = 1001
currentMillis = 1402
previousMillis = 1001
currentMillis = 1603
previousMillis = 1001
currentMillis = 1804
previousMillis = 1001
currentMillis = 2004
previousMillis = 1001
Hallo

A lot better :wink: It now prints "Hallo" every second. You can comment the delay (it was just added so the serial monitor was not flooded with info) and the two serial print lines.

I hope this helps you understand what to do.

To add

previousMillis is a stupid name; I know it's used all the time in examples but it's still a stupid name. It reflects something, e.g. the last time that you read the sensor or the last time that that you updated the display. Better names would be (respectively) lastSensorReadMillis or lastDisplayUpdateMillis. Personally I always us the word time in there instead of millis so lastSensorReadTime or lastDisplayUpdateTime.

Now let's say that you want to read the sensor every 500ms (e.g. for logging) and update the display every 1000ms.

const unsigned long displayUpdateInterval = 1000;
const unsigned long sensorReadInterval = 500;

// timing related
unsigned long currentTime;
unsigned long lastSensorReadTime;
unsigned long lastDisplayUpdateTime;


void setup()
{
  Serial.begin(115200);
}

void loop()
{
  // get the current time
  currentTime = millis();

  // compare current time with last display update time
  if (currentTime - lastDisplayUpdateTime >= displayUpdateInterval)
  {
    // save the last display update time
    lastDisplayUpdateTime = currentTime;
    Serial.print(currentTime); Serial.println(" >> It's time to update the display");
  }
  
  // compare current time with last sensor read time
  if (currentTime - lastSensorReadTime >= sensorReadInterval)
  {
    // save the last sensor read time
    lastSensorReadTime = currentTime;
    Serial.print(currentTime); Serial.println(" >> It's time to read the sensor");
  }
}

Oh, awesome, that's super helpful, thanks a lot! Will change some things and update shortly