Displaying on an OLED screen

I have a weird problem with my Arduino I've been trying to fix for weeks. I'm making a small automated greenhouse for a school project. I want to have a simple display that shows the temperature and humidity, and I want to be able to decide at which values the greenhouse tries to keep them. I've reduced my code to the most barebones state, removing most librairies but one of my screen states still wont diplay. The night I coded it worked but the next day the HUMI state wouldn't display at all. Any ideas why? (I think the TEMP and HUMI states are switched in this code, but I still have the same problem)


//Librairies
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <Arduino.h>

//Screen parameters
#define i2c_Address 0x3D
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1

//Strings for printing
String str1;
String str2;

//Button parameter
const int BUTTON_PIN = 4;
const int PRESS_TIME = 400;
bool LENGTH;
int lastState = LOW;
int currentState;
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;

//Settings values
int SetTemp;
int SetHumi;

//Refresh rate
unsigned long RpreviousMillis = 0;
const long Rinterval = 50;

//Different screen states
typedef enum {
  TEMP_e,
  HUMI_e,
} EtatAffichage_t;
EtatAffichage_t etatAffichage = TEMP_e;

bool SetMode = 0;

//I2C object constructor
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  //Initialize display
  display.begin(i2c_Address, true);
  display.display();
  delay(2000);
  display.clearDisplay();
}

void loop() {
  /*---------------------------------------------------------------------------------------*/
  //Button and interface logic

  //Button short and long press handling
  currentState = Debounce();

  if (lastState == HIGH && currentState == LOW) {
    pressedTime = millis();
  } else if (lastState == LOW && currentState == HIGH) {
    releasedTime = millis();
    long pressDuration = releasedTime - pressedTime;
    if (pressDuration < PRESS_TIME) {
      //Disable set mode if already entered
      if (SetMode == 1) {
        SetMode = 0;
      } else if (etatAffichage == TEMP_e) {
        etatAffichage = HUMI_e;
      } else {
        etatAffichage = TEMP_e;
      }
    }

    //Set mode handling
    if (pressDuration > PRESS_TIME) {
      if (SetMode == 0) {
        SetMode = 1;
      } else {
        SetMode = 0;
      }
    }
  }

  lastState = currentState;


  //Set mode
  if (SetMode == 1) {
    if (etatAffichage == TEMP_e) {
      SetTemp = 15;
    } else if (etatAffichage == HUMI_e) {
      SetHumi = 40;
    }
  }


  /*---------------------------------------------------------------------------------------*/
  //Screen and time handling

  //Set mode indicator
  if (SetMode == 1) {
    display.drawRect(10, 46, 38, 18, SH110X_WHITE);
  }

  //Temp and humi displaying

  int Temp = 12;
  int Humi = 21;

  switch (etatAffichage) {
    case HUMI_e:
      String LiveTempStr = "Live Temp: " + String(Temp) + "C";
      String SetTempStr = "Set Temp:  " + String(SetTemp) + "C";
      Affichage(LiveTempStr, SetTempStr, 0, 0, 2);
      break;

    case TEMP_e:
      String LiveHumiStr = "Live Humi: " + String(Humi) + "p";
      String SetHumiStr = "Set Humi:  " + String(SetHumi) + "p";
      Affichage(LiveHumiStr, SetHumiStr, 0, 0, 2);
      break;
  }

  //Time displaying
  String TimeStr = "12:13";
  Affichage(TimeStr, "0", 68, 49, 2);

  //Display update
  display.display();

  //Refresh rate
  unsigned long RcurrentMillis = millis();
  if (RcurrentMillis - RpreviousMillis >= Rinterval) {
    display.clearDisplay();
    RpreviousMillis = RcurrentMillis;
  }
}


//Text display function
void Affichage(String str1, String str2, int x, int y, int size) {
  display.setTextSize(size);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(x, y);
  display.println(str1);
  if (str2 != "0") {
    display.println(str2);
  }
}

//Button debounce function
bool Debounce() {
  static unsigned long lastStableT = 0;
  static bool stableState = HIGH;
  bool currentState = digitalRead(BUTTON_PIN);

  if (stableState == currentState) {
    lastStableT = millis();
  } else if ((millis() - lastStableT) >= 10) {
    stableState = currentState;
  }
  return stableState;
}


Too much code for a simple project. Maybe look at some sketches others have done. Lot's here in the Project Hub.

Enclose the statements inside each case with brackets to limit the scope of the local variables. When you declare a local variable with an initializer, the compiler sees that variable as existing until the end of the switch/case, and never lets you enter any subsequent case because the initializer would be bypassed for those cases.

1 Like

Thank you I was getting desesperate, it worked!