Multiple Load Cell+HX711 and OLED display

Hi all,

the following code working properly and permit to read on an OLED SSD1306 64x128 the weight readed from a single load cell. My scope is to add other two cells, but when I add the code at lines 60 - 62 the display show the text "inf" instead of the weight cell value.
I'm not an Arduino guru and really don't understand where is my error.

#include "HX711.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

const int LOADCELL1_DOUT_PIN = 5;
const int LOADCELL2_DOUT_PIN = 6;
const int LOADCELL3_DOUT_PIN = 7;
const int LOADCELL_SCK_PIN = 4;

#define SENSORCNT 1
HX711 *scale[SENSORCNT];

void setup() {
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();

  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Load Cell");
  display.display(); 
     
  scale[0]->begin(LOADCELL1_DOUT_PIN, LOADCELL_SCK_PIN);
  //scale[1]->begin(LOADCELL2_DOUT_PIN, LOADCELL_SCK_PIN);
  //scale[2]->begin(LOADCELL3_DOUT_PIN, LOADCELL_SCK_PIN);

  scale[0]->set_scale(493.f);
  scale[0]->tare();
  scale[0]->read();
  scale[0]->read_average(20);
  scale[0]->get_value(5);
  scale[0]->get_units(5), 1;
}

void loop() {
  char str[12];
  float units = 0.0;
  display.setTextSize(1);
  display.setTextColor(WHITE, BLACK);
  
  display.setCursor(0, 16);
  display.print("Scale1 : ");
  units = scale[0]->get_units();
  display.println(units, 1);
  display.display(); 

  display.setCursor(0, 16);
  display.print("Scale1 : ");
  units = scale[0]->get_units(10);
  display.println(units, 1);
  display.display();
  
//  display.setCursor(0, 25);
//  display.print("Scale2 : ");
//  display.display();
  
  scale[0]->power_down();
  delay(1000);
  scale[0]->power_up();
}

This declares an array of pointers, that point to nothing.
Using them like you do on a processor that supports exception (like ESPs)

would immediately crash with a null pointer exeption.

On an AVR you just get garbage and mess up the register set,
which is located at the bottom of the address space.

Loose the '*' in the definition and use '.' instead of '->' for access.

Many thanks Whandall, now it works as expected. :grinning:

You should learn a little more about pointers, I think.

In an AVR environment, pointers are mostly used to pass the address of already existing
objects around, because the scare RAM does not allow normal dynamic memory usage.

If you have a fixed number of objects, an array is the safest and smallest incarnation.
Of course, you can use a pointer to select/address one specific instance inside the array,
but make sure it points to something, before you use the object pointed to.

I'm coming from java programming in the past, this is not my daily work from long time.
I just playing with Arduino for my RC projects but I'm not a professional programmer and I was not confident with the usage of the pointers (java = No pointers :slightly_smiling_face: ).
I try to understand better, but require time, you know.....and as usual the scope is to have the solution working as soon as possible, so in general the study going down and the cut&paste going up :crazy_face:

Java is very forgiving, Python even more.

C++ is strictly typed, which gives a different, but less error-prone environment,
while it allows you direct access to memory and I/0 (at least on the smaller controllers).
There are no checks on array boundaries, you are free to access memory outside the array,
by using bogus indices.

If you are not familiar with the myriads of ways to shoot yourself into the foot,
an ESP32 based Arduino compatible system would be beneficial in learning not to hit the foot.
The ESP32 sports exceptions and has a full-blown RTOS running on the 2 cores, WiFi, Bluetooth,
and many useful integrated hardware features.

You can run all the basic stuff on such a system also, if you can live with the 3.3V,
many peripherals already need 3.3V, so maybe that is even an advantage.

You can get ESP32 variants with attached video camera for less than $10.

Learning by analyzing, using and understanding foreign code is absolutely ok,
solving puzzles by making unrelated pieces fit with a hammer, rarely gives good results.

As per your indication i'll check the ESP32. But to do experience I want complete this project, that is not so far. I jest need to manage 4 buttons, i see something about the interrupt....can you suggest a path and a link or example code to study ?

Thanks,
M.

Don't use interrupts for buttons, you can poll pins fast enough.
The best connection for buttons is to use INPUT_PULLUP and a switch closing to GND.
I use the Bounce2 library to handle state change detection and debouncing.

Hi rock_roll,

even if you have a very similar problem. You should not hi-jack other users thread. Because you are using a second set of hardware in a different place.
So the cause of the problems will be different.

report your posting to the moderator to move your posting into its own thread.

To narrow down the problem you should test the display with a demo-code that does nothing else than showing some text on the display. And you should connect only the display - nothing else

This ensures to exclude interference between your display and other code or other hardware.
If a demo-code that is well known to work properly and the display does not show the text it is very likely that there is a hardware-problem.

If this demo-code works you know the hardware is connected the right way.
If it was a hardware-problem you can try your complete code again.
If it works good.

If it does not work
you should adding additional code step by step to see when does it not to work anymore.

best regards Stefan

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