Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adafruit_SH110X.h does not work with Adafruit_BME280 #25

Open
BioFabLab opened this issue Nov 2, 2024 · 0 comments
Open

Adafruit_SH110X.h does not work with Adafruit_BME280 #25

BioFabLab opened this issue Nov 2, 2024 · 0 comments

Comments

@BioFabLab
Copy link

  • Arduino board: Arduino uno

  • Arduino IDE version (found in Arduino -> About Arduino menu): IDE 2.3.3

  • List the steps to reproduce the problem below (if possible attach a sketch or
    copy the sketch code in too):
    Adafruit_SH110X.h does not work with Adafruit_BME280 and has isues with serial.print

I'm changing disply driver from Adafruit_SSD1306.h to Adafruit_SH110X.h

My code (which is still a wrok in progress) workes just fine with Adafruit_SSD1306.h but changing to Adafruit_SH110X.h I run into multiple problems.

In Adafruit_BME280 there is this sensor conection cheek.

bool status;
status = bme.begin();
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring or sencor I2C addres!"); //If this text to long Adafruit_SH110X.h dies
while (1);
}

If the Serial.println text is short the splash screen from Adafruit_SH110X.h flickers and the program continues but If the Serial.println text is to long the program gets stuck on the splach screen even thoug it's not printing that line.

I found a hint for one of the problems in a forum: you must not use serial.print within I2C configurations,
because I2c and serial both use interrupts and so get in conflict.
https://forum.arduino.cc/t/uno-with-bme280-and-oled-1106-loop-stops-when-using-bme-readpressure/1035419/5

When trying to read the values from the BME my program chrashes and I get a glitchy screen.

c_vac = (bme.readPressure() / 1000.0F); //If I use this Adafruit_SH110X.h dies
temp = bme.readTemperature(); //If I use this Adafruit_SH110X.h dies

Here's my code:
Sorry for the mess work in progress

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_GFX.h>
//#include <Adafruit_SSD1306.h> // OLED Driver
#include <Adafruit_SH110X.h>
#include <EEPROM.h> // EEPROM is a memory whose values are kept when the board is powered off.
#define TIMEOUT 3000 // Set timer for Return to main screen
#define UPDATE_SERIEL 3000 // Set timer for seriel update

/* Uncomment the initialize the I2C address , uncomment only one, If you get a totally blank screen try the other*/
//#define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
//#define i2c_Address 0x3d //initialize with the I2C addr 0x3D Typically Adafruit OLED's

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int temp = 0; // Temp

int irset = 0; // IR xx%
int vacset = 0; // Vacuum
int fanset = 0; // Fan temp set
int on_time = 0; // Time

int c_vac = 0; // curent Vacuum
int c_fanset = 0; // curent Fan on Temp
int c_on_time = 0; // curent Time

int hh=0, mm=0, ss=0;

int newinput = 0;
int oldinput = 0;
int state = 0;
uint32_t timeout; // Return to main screen
uint32_t update_seriel;

int on_hh=0, on_mm=0; // On Timer

Adafruit_BME280 bme; // I2C

void setup() {

pinMode(0, INPUT_PULLUP); // Analg A0/D0 TEMP MCP9700

pinMode(3, OUTPUT); // D2/GPIO4 IR PWM
pinMode(5, OUTPUT); // D3/GPIO5 VAC PWM
pinMode(6, OUTPUT); // D6/GPI21 FAN PWM

pinMode(4, INPUT_PULLUP); // + Button
pinMode(8, INPUT_PULLUP); // Select Button
pinMode(12, INPUT_PULLUP); // - Button

Serial.begin(9600);
Serial.println(F("Connecting"));

delay(250); // wait for the OLED to power up
display.setRotation(2); // Rotate disp 180
display.begin(0x3c, true); // Address 0x3C default
display.setContrast (0); // dim display
display.display();
delay(500);
display.clearDisplay(); // Clear the buffer.

display.setTextSize(2);
display.setTextColor(SH110X_WHITE);

bool status;
status = bme.begin();
if (!status) {
// Serial.println("Could not find a valid BME280 sensor, check wiring or sencor I2C addres!"); //If this text to long Adafruit_SH110X.h dies
while (1);
}

timeout = millis() + TIMEOUT; // Return to main screen function
update_seriel = millis() + UPDATE_SERIEL;
}

void main_screen() { // Main status screen. State 0

display.clearDisplay(); // Clear the buffer.
display.setCursor(0, 0);
display.print(irset);
display.println("%");
display.print(c_vac);
display.println(" kPa");
display.print(temp);
display.println("C");
display.print(c_on_time);
display.println("HH:MM");
display.display();
}

void IRset(char* captione, int val) {

display.clearDisplay(); // Clear the buffer.
display.setCursor(0, 0);
display.println("IRset");
display.print(val);
display.println("%");
analogWrite(3, irset); // PMW out
display.display();
}

void VacSet(char* captione, int val, int vacset) {

display.clearDisplay(); // Clear the buffer.
display.setCursor(0, 0);
display.println("VacSet");
display.println(val);
analogWrite(5, vacset); //////////////////////////// Skal reguleres med c_vac
display.display();
}

void FanSet(char* captione, int val, int curent) {

display.clearDisplay(); // Clear the buffer.
display.setCursor(0, 0);
display.println("FanSet");
display.println(val);
display.println(curent);
display.display();
}

void OnTimeSet(char* captione, int val, int curent) {

display.clearDisplay(); // Clear the buffer.
display.setCursor(0, 0);
display.println("OnTimeSet");
display.println(val);
display.println(curent);
display.display();
}

void loop() {

// c_vac = (bme.readPressure() / 1000.0F); //If i use this Adafruit_SH110X.h dies
// temp = bme.readTemperature(); //If i use this Adafruit_SH110X.h dies

int input = 0;
if (digitalRead(4) == LOW) { // Read + Button
input = 1;
} else if (digitalRead(8) == LOW) { // Read Select Button
input = 2;
} else if (digitalRead(12) == LOW) { // Read - Button
input = 3;
}

if (millis() > timeout) {
state = 0;
timeout = millis() + TIMEOUT;
main_screen();
}

if (input != oldinput) {
newinput = input;
delay(10);
timeout = millis() + TIMEOUT;
}
oldinput = input;

if (millis() > update_seriel) {
update_seriel = millis() + UPDATE_SERIEL;
Serial.print("IR Intensity:");
Serial.print(irset);
Serial.print("%, ");
Serial.print("Vacuum:");
Serial.print(c_vac);
Serial.print("mBar, ");
Serial.print("Temp:");
Serial.print(temp);
Serial.print("°C,");
Serial.print("On time:");
Serial.print(c_on_time);
Serial.println("HH:MM");
}

switch (state) {

case 0:  // Main status screen

  if (newinput == 2) {
    IRset("Set IR%", irset);
    state = 1;
  }
  break;
case 1:  // Set IR light intensity

  if (newinput == 1) {
    irset += 5;
    if (irset > 100) {irset = 0;}    // Loop de hoop
    IRset("Set IR%", irset);
  }
  if (newinput == 3) {
    irset -= 5;
    if (irset < 0) {irset = 100;}    // Loop de hoop
    IRset("Set IR%", irset);
  }
  if (newinput == 2) {
    VacSet("Set Vac", vacset, c_vac);
    state = 2;
  }
  break;
case 2:  // Set vacuum

  if (newinput == 1) {
    vacset += 10;
      if (vacset > 400) {vacset = 0;}   // Loop de hoop
    VacSet("Set Vac", vacset, c_vac);
  }
  if (newinput == 3) {
    vacset -= 10;
    if (vacset < 0) {vacset = 400;}   // Loop de hoop
    VacSet("Set Vac", vacset, c_vac);
  }
  if (newinput == 2) {
    FanSet("Set Fan T", fanset, c_fanset);
    state = 3;
  }
  break;
case 3:  // Set max temp for fan to start

  if (newinput == 1) {
    fanset++;
     if (fanset > 12) {fanset = 0;}   // Loop de hoop
    FanSet("Set Fan T", fanset, c_fanset);
  }
  if (newinput == 3) {
    fanset--;
     if (fanset < 0) {fanset = 12;}   // Loop de hoop
    

    FanSet("Set Fan T", fanset, c_fanset);
  }
  if (newinput == 2) {
    OnTimeSet("Set Time", on_time, c_on_time);
    state = 4;
  }
  break;

case 4:  // Set desired on time

  if (newinput == 1) {
    on_hh++;
    OnTimeSet("Set Time", on_hh, on_mm);
  }
  if (newinput == 3) {
    on_hh--;
    OnTimeSet("Set Time", on_hh, on_mm);
  }

  if (newinput == 2) {
        if (newinput == 1) {
    on_mm+=5;
    OnTimeSet("Set Time", on_hh, on_mm);
  } 
  if (newinput == 3) {
    on_mm-=5;
    OnTimeSet("Set Time", on_hh, on_mm);
  }}
  if (newinput == 2) {
    main_screen();
    state = 0;
  }
  break;

}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant