LEDs flash at random times

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;
}

}

Weclome to the board @citrusslice..

Just took a quick peek..
First line in your loop is clearDisplay(), ouchie..

The display only needs to be updated when values displayed change..
Might be possible that your sensor returns a bad value also..
I see your interval at 50ms, that's also very short for temp changes, should be more like 1000, which is once per second..

nice job on the posting..

~q

I do not see a pin declared for the BME280. (ah... I2c... sorry)

simmed here..

played with the display some..
removed unused includes as well..

good luck.. ~q

Hello @qubits-us @citrusslice - I added debug random values to qubits' code to make the LEDs react. Humidity (middle LED) is not reacting.

I added these lines in loop:

    // ADDED FOR DEBUG
    hum = random (50, 70);
    pressure = random (100, 150);
    temp = random (30, 40);

The simulation is here.

One last thing... Analog A0 is referenced once... but not used. Will it be connected to another sensor?

1 Like

please, what's here??

~q

A0 actually isn't used. I probably had it in there from when I combined my projects together and forgot to remove it. My bad. It has been removed now. Tried q's sim, all of that makes sense, although I'm still getting some random flashes, this time happens to be on the pressure LED

added this debug. middle LED is pressure. But yes, doesn't seem to react when uploaded

Just added some Serial prints to pressure led..
should print out the value, thinking maybe getting a zero??

what does it print??

~q

Okay. The LED should light on a change in pressure. maybe a multiplication or division made it a constant zero (stagnant pressure does not light the LED).

Make your sim values change so the LEDs light.

Prints out -305 every time

@citrusslice If you are using the hardware, I think the BME 280 only updates every two seconds (similar to the DHT).

Hysteresis.
Hysteresis @ Wikipedia

mode some changes..
all values are now floats per the libs h file..
enhanced the debug print a bit..
I did try the random in the loop all leds responded, still in there commented out..
Give it another go..

~q

all LEDs respond in debug. When changed to float all LEDs are going crazy. Float seems to be too sensitive to a change in reading

curious,

what do you get when you load this sketch??
bme280test ino
change address so it picks up your sensor..
~q

All looks normal here. readings on serial monitor are consistant and respond to temp, humidity, altitude and pressure changes.

I think this was one of the example sketches I tried when I first tested the bme280

Lol,

just noticed, when the display is updated the sensor is read again..
that probably can't happen..

~q

Here's a simple example of adding hysteresis or "deadband" (difference between on and off points, 2 degrees here):

If (temp <= 20)
{
    heatON();
}
else if (temp >= 22)
{
  heatOFF();
}