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;
}
}