Oled 0,96 not working with a certain functio anymore -.-

Hi guys,

to be honest, the title is very misleading, but I will explain it.
explain. I am currently building an irrigation system with Hx711 scale
cells. It is well known that multiple HX711s cause problems. I had
hoped that calibrating the scales at full load would allow me to
filter out the interference voltages (if these are responsible for incorrect values
values, as I operate all three hx711s, one 0.96 oled and one dht21
via a 5V power supply) and therefore even if all three
are in operation at the same time.

I had already calibrated all the scales separately, but when I then
the rest of the setup, the values went down the drain.
fell into the stream.

Soooo I wrote a function, which calibrates the scales at every restart.
calibration of the scales one after the other at every restart.

I don't know why, but if I now want to execute this function
function in my void setup, then my Oled display is suddenly no longer recognised
no longer recognised and it cannot output anything.

However, if I exclude the function as it is executed, the display is recognised again. What am I missing?

Thanks guys! Looking forward to some input. slight_smile:

The following code:

#include "DHT.h"
#define DHTPIN A0
#define DHTTYPE DHT21
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include "HX711.h"


HX711 scale1;
HX711 scale2;
HX711 scale3;
uint8_t dataPin1 = 5;
uint8_t clockPin1 = 6;

uint8_t dataPin2 = 11;
uint8_t clockPin2 = 12;

uint8_t dataPin3 = 9;
uint8_t clockPin3 = 10;

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

DHT dht(DHTPIN, DHTTYPE);


const int PinPump1 = 2;
const int PinPump2 = 3;
const int PinPump3 = 4;
void setup() {
  Serial.begin(115200);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  // put your setup code here, to run once:

pinMode(PinPump1, OUTPUT); // (pumpe 9)                                             1 min = 0.2L -> 600000ms = 0.2L -> 30000ms = 0.1L
pinMode(PinPump2, OUTPUT); // (pumpe 10)                                            1 min = 0.2L
pinMode(PinPump3, OUTPUT); // (pumpe 11)                                            1 min = 0.2L                                         1 min = 0.2L

dht.begin();
scale1.begin(dataPin1, clockPin1);
scale2.begin(dataPin2, clockPin2);
scale3.begin(dataPin3, clockPin3);




digitalWrite(PinPump1, HIGH); 
digitalWrite(PinPump2, HIGH); 
digitalWrite(PinPump3, HIGH);
display.setCursor(0,20);
display.println("Booting shit... dont put shit on!");
display.display();

delay(2000);

display.clearDisplay();

calibration(scale1,1,display);
calibration(scale2,2, display);
calibration(scale3,3, display);

} 

void calibration(HX711& scale, int scaleNumber,Adafruit_SSD1306& display) {
    const float KNOWN_WEIGHT_GRAMS = 1000.0;  // 1kg calibration weight
    
    display.clearDisplay();
    display.setCursor(0,0);
    display.print("Calibrating #");
    display.println(scaleNumber);
    
    // First tare the scale
    display.println("Remove all weight");
    display.println("and wait...");
    display.display();
    delay(5000);
    
    scale.set_scale();  // Reset scale factor
    scale.tare();       // Reset to zero
    
    display.clearDisplay();
    display.println("Place 1kg weight");
    display.println("carefully...");
    display.display();
    delay(5000);
    
    // Take multiple readings for stability
    long sum = 0;
    int readings = 10;
    
    display.clearDisplay();
    display.println("Reading...");
    display.println("Don't touch!");
    display.display();
    
    for(int i = 0; i < readings; i++) {
        sum += scale.get_value();
        delay(200);
    }
    
    float rawAverage = sum / (float)readings;
    float scaleFactor = rawAverage / KNOWN_WEIGHT_GRAMS;  // Calculate units per gram
    
    scale.set_scale(scaleFactor);
    
    // Verify calibration
    float verifyWeight = scale.get_units(5);
    
    display.clearDisplay();
    display.print("Scale #");
    display.println(scaleNumber);
    display.print("Factor: ");
    display.println(scaleFactor);
    display.print("Test: ");
    display.print(verifyWeight);
    display.println("g");
    display.display();
    delay(3000);
    
    // Store factor in EEPROM here if needed


}





void loop() {
  display.clearDisplay();



  int w1 = scale1.is_ready() ? scale1.get_units(10) : -1;
  delay(100);
  int w2 = scale2.is_ready() ? scale2.get_units(10) : -1;
  delay(100);
  int w3 = scale3.is_ready() ? scale3.get_units(10) : -1;

  // Temperature and Humidity Display
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    display.setCursor(0, 0);
    display.setTextSize(1);
    display.println(F("Failed to read DHT!"));
    display.display();
    delay(2000);
    return;
  }

  // First Screen: Temperature and Humidity
  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("Temperature: ");
  display.setTextSize(2);
  display.setCursor(0,10);
  display.print(t, 1); // Show one decimal place
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(2);
  display.print("C");

  display.setTextSize(1);
  display.setCursor(0, 35);
  display.print("Humidity: ");
  display.setTextSize(2);
  display.setCursor(0, 45);
  display.print(h, 1); // Show one decimal place
  display.print("%"); 
  display.display();
  delay(3000);

  // Second Screen: Plant Weights
  display.clearDisplay();
  display.setTextSize(1);
  
  // Plant 1
  display.setCursor(0,0);
  display.print("Plant 1: ");
  display.print(w1);
  display.println("g");
  
  // Plant 2
  display.setCursor(0,20);
  display.print("Plant 2: ");
  display.print(w2);
  display.println("g");
  
  // Plant 3
  display.setCursor(0,40);
  display.print("Plant 3: ");
  display.print(w3);
  display.println("g");
  display.display();
  delay(2000);

  // In the pump sections, ensure all display updates are followed by display.display():
  if (w1 > 4000) {
    // Do nothing
  } else {
    unsigned long startTime = millis(); // Record the start time
    unsigned long timeout = 3000; // Timeout after 30 seconds

    while (w1 < 1) {
      display.setCursor(0,10);
      display.print("Pumpe 1 läuft...");
      display.display();  // Make sure this is present
      digitalWrite(PinPump1, LOW); // Turn on pump
      delay(2000);
      w1 = scale1.get_value(10);
      display.clearDisplay(); // Read new weight after irrigation
      if (millis() - startTime > timeout) {
        display.setCursor(0,10);
        display.print("Timeout reached! Stopping pump.");
        display.display();
        delay(2000);
        display.clearDisplay();
        break; 
      }
    }
    digitalWrite(PinPump1, HIGH); // Turn off pump
  }
  display.clearDisplay();
  if (w2 > 1) {
    // Do nothing, don't turn on the pump
  } else {
    unsigned long startTime = millis(); // Record the start time
    unsigned long timeout = 3000; // Timeout after 30 seconds
    while (w2 < 7000) {
      display.setCursor(0,10);
      display.print("Pumpe 2 läuft...");
      display.display(); // Loop until weight reaches 7000
      digitalWrite(PinPump2, LOW); // Turn on pump
      delay(2000);
      w2 = scale2.get_value();
      display.clearDisplay(); // Read new weight after irrigation
      if (millis() - startTime > timeout) {
        display.setCursor(0,10);
        display.print("Timeout reached! Stopping pump.");
        display.display();
        delay(2000);
        display.clearDisplay();
        break;
      }  
    }
    digitalWrite(PinPump2, HIGH); // Turn off pump
  }
  display.clearDisplay();
  if (w3 > 1) {
    // Do nothing, don't turn on the pump
  } else {
    unsigned long startTime = millis(); // Record the start time
    unsigned long timeout = 3000; // Timeout after 30 seconds
    while (w3 < 7000) {
      display.setCursor(0,10);
      display.print("Pumpe 3 läuft...");
      display.display(); // Loop until weight reaches 7000
      digitalWrite(PinPump3, LOW); // Turn on pump
      delay(2000);
      w3 = scale3.get_value();
      display.clearDisplay(); // Read new weight after irrigation
      if (millis() - startTime > timeout) {
        display.setCursor(0,10);
        display.print("Timeout reached! Stopping pump.");
        display.display();
        delay(2000);
        display.clearDisplay();
        break; 
      }  
    }
    digitalWrite(PinPump3, HIGH); // Turn off pump
  }
  display.clearDisplay();
} 

Cross post flagged, to Oled 0,96 will nicht mehr, wenn ich eine random funktion verwende

Quick questions.

Have you tried printing something on the display before you call the calibration function?

Do yoi have more than one display? Why do have the display as a parameter of the function? I don't think it hurts, but it makes me wonder.

a

Yes i am trying to print something before.

I have only one display.
This was only an attempt in finding the problem. :slight_smile:

@baumamstiel ,

Please do not duplicate your questions in the forum because doing so wastes the time of the volunteers trying to help you.

As German seems to be your native language I have locked this topic, please continue to use your German topic.

Thank you.