...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();
}
}