IF Statement Execution is Stopping Temperature Readout

So I have a BMP280 and UNO running an OLED temperature gauge. I added an IF statement to turn an LED on when temp is above 25c. This works well except that the display freezes on 25c for the duration of the time it is 25c or above 25c. I want it to continue registering to the oled what the temperature is at all times , any idea how I can acheive this?

the code:

#include <Adafruit_GFX.h>      //Libraries for the OLED and BMP280
#include <Adafruit_SSD1306.h>
#include <Adafruit_BMP280.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display)
Adafruit_BMP280 bmp;


void setup() {

  pinMode (2, OUTPUT);
  

  bmp.begin(0X76);                                //Start the bmp                  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display
  display.clearDisplay();
  display.display();
  display.setTextColor(WHITE);
  display.setCursor (22,3);
  display.setTextSize(1); 
  display.print("URBAN ASSAULT");     //Show the name, you can remove it or replace it
  display.setCursor(18,12);
  display.setTextSize(1);          
  display.println("AUTOMATION AND "); 
  display.setCursor(1,22);
  display.setTextSize(1);          
  display.println("LIGHTING 403-597-XXXX "); 
  display.display();
  delay(6000);
}

void loop() {



    display.clearDisplay();
    float T = bmp.readTemperature();           //Read temperature in C
    
                                              //If you don't know it just modify it until you get the altitude of your place
    display.drawCircle( 123,12,2,WHITE);         
    
     

 
if (T >= 25.0) {
digitalWrite(2, HIGH); // if the temperature reaches 30ºC or above, close the circuit
}
else {
  digitalWrite(2, LOW); // otherwise, open (break) the circuit*/

    display.setCursor (29,0);              //Oled display, just playing with text size and cursor to get the display you want
    display.setTextColor(WHITE);
    display.setTextSize(1); 
    display.print("TEMPERATURE");

     display.setCursor (0,5);              //Oled display, just playing with text size and cursor to get the display you want
    display.setTextColor(WHITE);
    display.setTextSize(1); 
    display.print("=====================");
    
    display.setCursor(18,12);
    display.setTextSize(3);
    display.print(T,1);
    display.setCursor(103,12);
    display.setTextSize(3);
    display.print("C");

   
    
display.display();
   

}} `Preformatted text`

It looks like the screen update code is in the ELSE portion of the IF that won't execute when the temp >25C. I suggest you move the } to this new location:

if (T >= 25.0) {
digitalWrite(2, HIGH); // if the temperature reaches 25ºC or above, close the circuit
}
else {
  digitalWrite(2, LOW); // otherwise, open (break) the circuit*/
}

display.setCursor (29,0);    
 display.setTextColor(WHITE);

2 Likes

You sir, are the man. Yes that made it work right , thank you so much for sharing your knowledge :+1: :metal: :metal:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.