Hello,
Beginner Arduino coder here.
I'm trying to create a device that displays temperature, humidity and pressure on an oled using a bme280 sensor and also lights up an LED when there's a spike in any of those measurements. Right now I have it set for when there's a drop in temp, drop in pressure and a rise in humidity.
the basic function of the device is working. I can blow on the sensor and see the humidity LED turn on, put it in the fridge for a few seconds and see the temp LED turn on, put it in an inflated plastic bag and pressurize it and when I release pressure, the pressure LED goes on.
The problem I'm having is that I can leave the device sitting there for a while and nothing will happen, but at random times, any one of the LEDs could start flashing constantly. They would flash for maybe a minute or so and then settle down. There's no consistency with the timing of the flashing or which LED would flash. I've timed it multiple times.
So, I'm beginning to think that there's a flaw in the code that I'm not seeing. So far, I've tried reordering the if statements to see if that would help if the code was getting confused, but that didn't help. I've also tried making the temp humidity and pressure multipliers less to decrease sensitivity but that also didn't help.
I'm wondering if there's something in the code that I'm missing that someone experienced would be able to notice. Or maybe there's a different way to produce the same function that's more consistent. Most of this code was taken from multiple different projects online and then just adapted to my project. I've done some research to see if I could find anyone with a similar problem out there but came up empty.
This all happens both on battery power and USB from the PC
Any advice or ideas would be appreciated.
Thanks!
#include <SSD1306Ascii.h>
#include <SSD1306AsciiAvrI2c.h>
#include <SSD1306AsciiSoftSpi.h>
#include <SSD1306AsciiSpi.h>
#include <SSD1306AsciiWire.h>
#include <SSD1306init.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "Wire.h"
#define LED1 11 // LED when humidity drops
#define LED2 9 // LED when temperature drops
#define LED3 10 // LED when pressure drops
Adafruit_BME280 Sensor;
int temp; // Sensor temperature value
int tempnow;
int tempold;
int pressure; // Sensor pressure value
int pressurenow;
int pressureold;
int hum; //Sensor humidity value
int humnow;
int humdown;
unsigned long previousMillis = 0;
const unsigned long interval= 50; // interval for time between readings
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
const byte sensor;
void setup(){
Sensor.begin(0x76);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.clearDisplay();
display.setTextSize(3);
display.setTextColor(WHITE);
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(A0, INPUT);
delay (2000);
digitalWrite(LED1, LOW); // turns LED1 off
digitalWrite(LED2, LOW); // turns LED2 off
digitalWrite(LED3, LOW); // turns LED3 off
}
void loop() {
display.clearDisplay();
display.setCursor(0,0);
display.print(1.8 * Sensor.readTemperature() + 32,0); display.println("F");
display.setCursor(70,0);
display.print(Sensor.readHumidity(),0); display.println("%");
display.setCursor(0,40);
display.print(Sensor.readPressure() / 100.0F,0); display.println("hPa");
display.display();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval){
temp = Sensor.readTemperature();
pressure = Sensor.readPressure();
hum = Sensor.readHumidity();
pressure = (pressure *0.0100);
hum = (hum *0.1000);
tempold = tempnow; // compares last temperature measurement with the new one
tempnow = temp; // this maps tempnow to the values of the temperature sensor
if (tempold > tempnow) { //if temperature drops, turn on LED2
digitalWrite(LED2, HIGH);}
else{ //if temperature is unchanged turn off LED2
digitalWrite(LED2, LOW);}
pressureold = pressurenow; // compares last pressure measurement with the new one
pressurenow = pressure; // this maps pressurenow to the values of the pressure sensor
if (pressureold > pressurenow) { // if pressure changes turn on LED3
digitalWrite(LED3, HIGH);}
else { // if pressure unchanged, keep LED3 off
digitalWrite(LED3, LOW);}
humdown = humnow; // compares last humidity measurement with the new one
humnow = hum; // this maps humnow to the values of the humidity sensor
if (humdown < humnow) { //if humidity changes turn on LED1
digitalWrite(LED1, HIGH);}
else { //if humidity is unchanged, keep LED1 off
digitalWrite(LED1, LOW);}
previousMillis = currentMillis;
}
}