Hi MorganS,
Thank you for your feedback. Noted on points 1, 2 and 3 and amended in initial post.
OK, HX711 is an ADC - does that mean that I should not use the analog pins on the Arduino (A0-A0) but use the other numerated 2-15 pins instead?
When you suggest loading up an HX711 library examples and get it working - what would that mean essentially? Should I tailor-amend the existing code or use it straight as it is? Apologies for my ignorance here but this really is all new to me.
Below is the code for the three libraries I am attempting to incorporate - there is no analogRead() contained in it:
1) Joystick.h
#ifndef JOYSTICK_h
#define JOYSTICK_h
#include "HID.h"
#if ARDUINO < 10606
#error The Joystick library requires Arduino IDE 1.6.6 or greater. Please update your IDE.
#endif
#if !defined(USBCON)
#error The Joystick library can only be used with a USB MCU (e.g. Arduino Leonardo, Arduino Micro, etc.).
#endif
#if !defined(_USING_HID)
#warning "Using legacy HID core (non pluggable)"
#else
//================================================================================
//================================================================================
// Joystick (Gamepad)
class Joystick_
{
private:
bool autoSendState;
int8_t xAxis;
int8_t yAxis;
int8_t zAxis;
int16_t xAxisRotation;
int16_t yAxisRotation;
int16_t zAxisRotation;
uint32_t buttons;
uint8_t throttle;
uint8_t rudder;
int16_t hatSwitch[2];
public:
Joystick_();
void begin(bool initAutoSendState = true);
void end();
void setXAxis(int8_t value);
void setYAxis(int8_t value);
void setZAxis(int8_t value);
void setXAxisRotation(int16_t value);
void setYAxisRotation(int16_t value);
void setZAxisRotation(int16_t value);
void setButton(uint8_t button, uint8_t value);
void pressButton(uint8_t button);
void releaseButton(uint8_t button);
void setThrottle(uint8_t value);
void setRudder(uint8_t value);
void setHatSwitch(int8_t hatSwitch, int16_t value);
void sendState();
};
extern Joystick_ Joystick;
#endif
#endif
2) HX711.h
#ifndef HX711_h
#define HX711_h
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class HX711
{
private:
byte PD_SCK; // Power Down and Serial Clock Input Pin
byte DOUT; // Serial Data Output Pin
byte GAIN; // amplification factor
long OFFSET = 0; // used for tare weight
float SCALE = 1; // used to return weight in grams, kg, ounces, whatever
public:
HX711();
virtual ~HX711();
// Initialize library with data output pin, clock input pin and gain factor.
// Channel selection is made by passing the appropriate gain:
// - With a gain factor of 64 or 128, channel A is selected
// - With a gain factor of 32, channel B is selected
// The library default is "128" (Channel A).
void begin(byte dout, byte pd_sck, byte gain = 128);
// Check if HX711 is ready
// from the datasheet: When output data is not ready for retrieval, digital output pin DOUT is high. Serial clock
// input PD_SCK should be low. When DOUT goes to low, it indicates data is ready for retrieval.
bool is_ready();
// Wait for the HX711 to become ready
void wait_ready(unsigned long delay_ms = 0);
bool wait_ready_retry(int retries = 3, unsigned long delay_ms = 0);
bool wait_ready_timeout(unsigned long timeout = 1000, unsigned long delay_ms = 0);
// set the gain factor; takes effect only after a call to read()
// channel A can be set for a 128 or 64 gain; channel B has a fixed 32 gain
// depending on the parameter, the channel is also set to either A or B
void set_gain(byte gain = 128);
// waits for the chip to be ready and returns a reading
long read();
// returns an average reading; times = how many times to read
long read_average(byte times = 10);
// returns (read_average() - OFFSET), that is the current value without the tare weight; times = how many readings to do
double get_value(byte times = 1);
// returns get_value() divided by SCALE, that is the raw value divided by a value obtained via calibration
// times = how many readings to do
float get_units(byte times = 1);
// set the OFFSET value for tare weight; times = how many times to read the tare value
void tare(byte times = 10);
// set the SCALE value; this value is used to convert the raw data to "human readable" data (measure units)
void set_scale(float scale = 1.f);
// get the current SCALE
float get_scale();
// set OFFSET, the value that's subtracted from the actual reading (tare weight)
void set_offset(long offset = 0);
// get the current OFFSET
long get_offset();
// puts the chip into power down mode
void power_down();
// wakes up the chip after power down mode
void power_up();
};
#endif /* HX711_h */
3) HX711_ADC.h (whatever ADC stands for)
#ifndef HX711_ADC_h
#define HX711_ADC_h
#include <Arduino.h>
#include "config.h"
/*
Note: HX711_ADC configuration values has been moved to file config.h
*/
#define DATA_SET SAMPLES + IGN_HIGH_SAMPLE + IGN_HIGH_SAMPLE // total samples in memory
#if (SAMPLES != 4) & (SAMPLES != 8) & (SAMPLES != 16) & (SAMPLES != 32) & (SAMPLES != 64) & (SAMPLES != 128)
#error "number of SAMPLES not valid!"
#endif
#if (SAMPLES == 4)
#define DIVB 2
#elif (SAMPLES == 8)
#define DIVB 3
#elif (SAMPLES == 16)
#define DIVB 4
#elif (SAMPLES == 32)
#define DIVB 5
#elif (SAMPLES == 64)
#define DIVB 6
#elif (SAMPLES == 128)
#define DIVB 7
#endif
class HX711_ADC
{
public:
HX711_ADC(uint8_t dout, uint8_t sck); //constructor
void setGain(uint8_t gain = 128); //value should be 32, 64 or 128*
void begin();
void begin(uint8_t gain);
int start(unsigned int t); // start and tare one HX711
int startMultiple(unsigned int t); //start and tare multiple HX711 simultaniously
void tare(); // zero the scale, wait for tare to finnish
void tareNoDelay(); // zero the scale, do tare in loop without waiting for tare to finnish
void setCalFactor(float cal); //calibration factor, raw data is divided by this value to convert to readable data
float getCalFactor(); // returns the current calibration factor
float getData(); // returns data from the moving average data set
float getSingleConversion(); //for testing and debugging
long getSingleConversionRaw(); //for testing and debugging
int getReadIndex(); //for testing and debugging
float getConversionTime(); //for testing and debugging
float getSPS(); //for testing and debugging
bool getTareTimeoutFlag(); //for testing and debugging
void disableTareTimeout(); //for testing and debugging
long getSettlingTime(); //for testing and debugging
void powerDown();
void powerUp();
bool getTareStatus(); // returns 1 if tareNoDelay() operation is complete
long getTareOffset();
void setTareOffset(long newoffset);
uint8_t update(); //if conversion is ready; read out 24 bit data and add to data set
protected:
uint8_t conversion24bit(); //if conversion is ready: returns 24 bit data and starts the next conversion
long smoothedData();
uint8_t sckPin; //HX711 pd_sck pin
uint8_t doutPin; //HX711 dout pin
uint8_t GAIN;
float calFactor;
volatile long dataSampleSet[DATA_SET + 1]; // data set, make voltile if interrupt is used
long tareOffset;
int readIndex = 0;
unsigned long conversionStartTime;
unsigned long conversionTime;
uint8_t isFirst = 1;
uint8_t tareTimes;
const uint8_t divBit = DIVB;
bool doTare;
bool startStatus;
long startMultipleTimeStamp;
long startMultipleWaitTime;
uint8_t convRslt;
bool tareStatus;
unsigned int tareTimeOut = (SAMPLES + IGN_HIGH_SAMPLE + IGN_HIGH_SAMPLE) * 150; // tare timeout time in ms, no of samples * 150ms (10SPS + 50% margin)
bool tareTimeoutFlag;
bool tareTimeoutDisable = 0;
};
#endif
Best regards,
Kris