Can't use hx711 with joystick library

Hi ! I am using a :

  • HX711 ADC with a 1kg Load cell
  • Arduino Micro
  • Joystick.h from MHeironimus
  • Hx711.h library from Bodge

The joystick library needs an int : "Joystick.setXAxis(int16_t value)"
The HX711 library gives me a long :
"// waits for the chip to be ready and returns a reading
long read();
"

I didn't succed to use :

int currentAcceleratorState = (int) map(rawReadAccelerator,200000,4000000,-32768,32767);

It gives me really weird results when I press on the load cell. By example a 2384590 reading (about the middle of the input range) gives me -32391 (about the lowest ouput), while a value above or under could give 32718 (around the highest output)

CurrentAcceleratorState is : -32518. RawReadAccelerator is calibrated : 214531. Scale.read() is : 214552
CurrentAcceleratorState is : -32518. RawReadAccelerator is calibrated : 214542. Scale.read() is : 214563
CurrentAcceleratorState is : -32518. RawReadAccelerator is calibrated : 214551. Scale.read() is : 214519
CurrentAcceleratorState is : -32518. RawReadAccelerator is calibrated : 214521. Scale.read() is : 214556
CurrentAcceleratorState is : -32517. RawReadAccelerator is calibrated : 214582. Scale.read() is : 214684
CurrentAcceleratorState is : 32513. RawReadAccelerator is calibrated : 250713. Scale.read() is : 468289
CurrentAcceleratorState is : 32395. RawReadAccelerator is calibrated : 637116. Scale.read() is : 826500
CurrentAcceleratorState is : 32254. RawReadAccelerator is calibrated : 956615. Scale.read() is : 1021243
CurrentAcceleratorState is : 32718. RawReadAccelerator is calibrated : 1245666. Scale.read() is : 1740287
CurrentAcceleratorState is : -32391. RawReadAccelerator is calibrated : 2384590. Scale.read() is : 2976566
CurrentAcceleratorState is : 32738. RawReadAccelerator is calibrated : 3671695. Scale.read() is : 4551800
CurrentAcceleratorState is : 32497. RawReadAccelerator is calibrated : 5427189. Scale.read() is : 5980023
CurrentAcceleratorState is : 32623. RawReadAccelerator is calibrated : 5762228. Scale.read() is : 4279086

The code :

#include <HX711.h>//https://github.com/bogde/HX711
#include "Joystick.h"

#define DEBUG   //allow to tune min and max

Joystick_ Joystick(0x04, 0x04, 0, 0, true, true, false,  true, false, false, false, false, true, true, false);
                   

long rawReadAccelerator;
int lastAcceleratorState ;
bool sendDebug = false;
int unsigned long lastDebug = 0;
int DebugRefreshRate = 250;

const int doutPin = 12;
const int sckPin = 13;

HX711 scale ;

void setup() {

 //Init Serial USB
 delay(2000);
 //pinMode(ledPin,OUTPUT);
 Serial.begin(9600);
Joystick.setXAxisRange(-32768, 32767);
 scale.begin(doutPin, sckPin);
 }
 
void loop() {

  if (millis() - lastDebug > DebugRefreshRate) {
    sendDebug = true;
    lastDebug = millis();
  }

rawReadAccelerator = scale.read();
int currentAcceleratorState = (int) map(rawReadAccelerator,200000,4000000,-32768,32767);

  if (currentAcceleratorState != lastAcceleratorState)
  {
    Joystick.setXAxis(currentAcceleratorState);
    lastAcceleratorState = currentAcceleratorState;
  }
if(sendDebug){
Serial.print("CurrentAcceleratorState is :  ");
Serial.print(currentAcceleratorState);
Serial.print(". RawReadAccelerator is calibrated :  ");
Serial.print(rawReadAccelerator);
Serial.print(". Scale.read() is : ");
Serial.println(scale.get_units());
sendDebug=false;
}
}

I'd first eliminate all the joystick code and #ifdefs to just create absolutely dead simple code to focus on understanding what's going on with the HX711 and why "really weird" (<-- undefined) results occur.

1 Like

Edited following your comment :
It gives me really weird results when I press on the load cell. By example a 2384590 reading (about the middle of the input range) gives me -32391 (about the lowest ouput), while a value above or under could give 32718 (around the highest output)

Let me put it another way:

With no load on the load cell, what is the value of scale.read()?
With full load on the cell, what is the value of scale.read()?

Do those two numbers make sense?

scale.read() works perfectly, with values going from 200 000 to 4 000 000. The problem is to map the values to -32768 - 32767

Ohhhhhhhhhhhhhhhh. So the problem is with the map function. :face_with_diagonal_mouth:

Well, map can't handle very large numbers, because...

long map(long x, long in_min, long in_max, long out_min, long out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

...the first multiplication overflows a long.

You could try rolling your own. Give this a try:

long largeMap(long x, long in_min, long in_max, long out_min, long out_max) {
  return ((x - in_min) / (in_max - in_min)) * (out_max - out_min)  + out_min;
}

Other ideas here: "map" function and large numbers - #11 by robtillaart

Yes I also think it comes from it. I tried your solution but that didn't change anything, I will check your link :slight_smile:
thanks a lot !

Oops - yeah, now the first integer division results in zero for all values except the upper limit, so it always returns -32768.

Give this a try:

long myMap(long i) {

 return 0.01725*i-36217;

}
1 Like

YES ! It works ! Thanks you so much !

You're welcome.

This would do it, too, approximately:

long myMapNoFloatingPointOp(long i) {

 return i/58-36217;

}
1 Like

Now I am wondering how to replace the number by something dynamic using two long variables (maxInput and minInput)... I tried
float coefficient (2^16)/(maxInput-minInput), but it doesn't seem to work...

EDIT : this seems to work :

int coeff = (maxAccelerator-minAccelerator)/(65536);
int myMap(long i) {
  long mymap = (i-minAccelerator)/coeff-32768;
  if (mymap==32768){mymap -=1;}
 return mymap;

}

Thanks again for your help DaveEvans. If you have time to spare, please check my DIY projects on the Discord :slight_smile:

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