Sorry for another beginner question...

...especially one about millis(). Ive combed through all the tutorials i could find but im just still utterly lost as to why this program isnt working.

Im trying to display sensor data to an oled screen using the mlx90614 and u8g2 libraries, respectively. Ive tried using two separate timers to check if the time elapsed between the current and previous sensor/screen timers has passed the specified sensor/screen intervals, and either clear the buffer then populate it with data, or send the buffer accordingly. The numbers appear on the screen as they should, however all four readings are in the thousands and dont change regardless of what is in front of the sensor, so im not sure if the sensor or screen isnt refreshing correctly, or both.

Here is my current code. I put both on separate timers just for the sake of clarity in figuring out what I was doing:

#include <Wire.h>
#include <Adafruit_MLX90614.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

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

const long screenInterval = 1000;           // interval at which to buffer (milliseconds)
unsigned long sensorInterval = 500;
unsigned long screenPreviousMillis = 0;
unsigned long sensorPreviousMillis = 0;


void setup() {
  mlx.begin();
  u8g2.begin();
  u8g2.enableUTF8Print();
}

void loop() {
  unsigned long screenCurrentMillis = millis();
  unsigned long sensorCurrentMillis = millis();
  if (sensorCurrentMillis - sensorPreviousMillis >= sensorInterval) {
    sensorPreviousMillis = sensorCurrentMillis;
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_5x7_tf);
    u8g2.setCursor(0, 15);
    u8g2.print("Ambient = "); u8g2.print(mlx.readAmbientTempC());
    u8g2.setCursor(0, 25);
    u8g2.print("*C\tObject = "); u8g2.print(mlx.readObjectTempC()); u8g2.println("*C");
    u8g2.setCursor(0, 35);
    u8g2.print("Ambient = "); u8g2.print(mlx.readAmbientTempF());
    u8g2.setCursor(0, 45);
    u8g2.print("*F\tObject = "); u8g2.print(mlx.readObjectTempF()); u8g2.println("*F");
    u8g2.println();
  }
  if (screenCurrentMillis - screenPreviousMillis >= screenInterval / 4) {
    screenPreviousMillis = screenCurrentMillis;
    u8g2.sendBuffer();
  }
}

Always a good plan if you use non-off-the-shelf libraries (i.e. included with the IDE) that you attach copies of the libraries.

No problem. Here is the u8g2 library reference manual:

Here is a link to the u8g2 source code (so you can view it in full, as opposed to an attachment): GitHub - olikraus/u8g2: U8glib library for monochrome displays, version 2

and here is the adafruit mlx90614 IR sensor library I am using: GitHub - adafruit/Adafruit-MLX90614-Library: Arduino library for the MLX90614 sensors in the Adafruit shop

Unfortunately, the IR sensor library has no reference documentation though, so Ive been trying to go off of the IR readout to serial example program, which I have attached. Thanks!

mlxtest.ino (1.26 KB)

It's unclear as to whether you are using the buffer properly, too much documentation to go through. Your code, wrt the timing loops, is fine, except I don't understand why you set screen interval at 1000 and then test 1000/4. Why not just set the interval at 250? The compiler will likely optimize it that way anyhow.

What I did read in the u8g2 documentation is that the sendBuffer method appears to deal with a buffer being written to by draw commands (which you don't have) and as this is being done twice as fast as you u8g2.print statements, it may be causing unknown issues. Have you tried getting rid of the u8g2.clearBuffer() and u8g2.sendBuffer() commands and see what happens?

Honestly a large part of why its written how it is for the timing is because i used the u8g2 library with another sensor program, and had the buffer set how it is around the timing with millis.

When I adjusted the timing and made the screen buffer twice as slow as the sensor timing (instead of twice as fast), I got the same results.

When I removed the buffer altogether (and by association, the millis timing as well) the screen was just blank.