Go Down

Topic: HX711 Continuous reading : How to (Read 106 times) previous topic - next topic

arjfca

Hi

First project with Arduino and C coding.  The project, a weighting system to evaluate residual quantity in  a container. 

I'm reading the weight value using HX711.h library

The problem is I'm loosing the weight after few hours and reading go back 0kg.  I read that there is a shutoff mode where the HX711 go in low power mode after the CLK stay high for a given period.

Is there a way to keep the module up and running and not lose the weight information?

Martin

Note: The code is a work in progress.  Some parts not debugged yet.  But I got the problem since I first started with HX711

Note 2: Excuse for my bad English.

Code: [Select]


#include "HX711.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x21,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

#define calibration_factor 16500.0 //This value is obtained using the SparkFun_HX711_Calibration sketch   5VDC
//#define calibration_factor 15350.0 //This value is obtained using the SparkFun_HX711_Calibration sketch  3.3 VDZ


#define DOUT  3
#define CLK  2

float weight;
int weight_read_step = 10;
int scale_wait_time =1000;
bool scale_in_wait_time = false;
byte _data; //KeySwitch
byte aButton;//KeySwitch
unsigned long previousMillis = 0;     
const long interval = 1000;
long Poids;
int weight_count =0;
unsigned read_delay_time = 0;
int read_delay = 1000;  //Read every 1 second
bool in_a_read_delay = false;
bool started_debounce = false;
int debounce = 15;
unsigned debounce_time = 0;
bool debounce_finish = false;
byte aButton_ = (0);

HX711 scale;

void setup() {
  Serial.begin(115200);
 
  //Key SwitchWire.begin();
  Wire.beginTransmission(0x20);
  Wire.write(0xff); // put a high level on pins
  Wire.endTransmission(); 
  Serial.println("HX711 scale demo");

  scale.begin(DOUT, CLK);
  scale.set_scale (calibration_factor); //(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  lcd.init();
  lcd.init();
  lcd.setCursor(0,0);
  lcd.print("Balance: ");
  Serial.println("Readings:");
}

void loop() {
  displayweight();
  Button();
  if (started_debounce == true){ a_debounce();}
  if (aButton == 1) {(Button1);}
 
  if (aButton == 3) {Serial.println(aButton);}
  if (aButton == 4) {Serial.println(aButton);}
  if (aButton == 5) {Serial.println(aButton);}
  if (aButton == 6) {Serial.println(aButton);}

  //Call reset
  if (aButton == 7) {
  lcd.setCursor(0,2);
  lcd.print ("Reset");
  delay(1000);
  softReset(); 
  }
  aButton ==10;
 
  //____________________
 
 
  //Serial.print("Reading: ");
  //Serial.print(scale.get_units(), 2); //scale.get_units() returns a float
  //Serial.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor
 // Serial.println();
 
}
//Function______________________________________________

void displayweight(){ // will read
 
  weight_count ++;
  if (weight_count <weight_read_step){
    scale.power_up();
    weight = (weight + (scale.get_units()));
    scale.power_down();
    }
  else {//weight_count >=6
    weight_count = 0;
    weight = (weight/weight_read_step);
    lcd.setCursor(0,3);
    lcd.print (weight,2);
    lcd.print(" ");
    lcd.print("kg");
    lcd.print(" ");
   } 
}

byte Button(){
   if (started_debounce == false){  // read key switch if not in a debounce
      aButton = 0;
      Wire.begin();
      Wire.requestFrom(0x20, 1);
      if(Wire.available()){
        _data = Wire.read();
        if(_data < 255){ // a button is pressed
          Witch_Key();
          started_debounce = true;
          debounce_time = millis();
        }
      }
   }
   if (debounce_finish == true){
       debounce_finish = false;
       started_debounce = false;           
       aButton = aButton_;
    }
 }
// Witch_Key
void Witch_Key(){
  if (_data ==254) aButton_=1;
  if (_data ==251) aButton_=3;
  if (_data ==247) aButton_=4;
  if (_data ==239) aButton_=5;
  if (_data ==223) aButton_=6;
  if (_data ==191) aButton_=7;
}

void Button1(){
  lcd.setCursor(0,1);
  lcd.print("TARE");
  scale.tare();
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("      ");
}
 //Reset
 void softReset(){
asm volatile ("  jmp 0");
}

//Base De Temp
void a_debounce(){
   Serial.print("entering base_time");
   if((unsigned long)(millis() - debounce_time) > debounce){
        debounce_finish = true;
        Serial.println("debounce finished");
   }
}

MorganS

Which Arduino?

Every time you reset that program, it calls scale.tare() in setup() which zeroes out the current weight. Don't do that.
"The problem is in the code you didn't post."

wvmarle

Indeed: why do you even do that reset? You should never, ever need this. If you do think you need it, there's something else terribly wrong with your program.

The HX711 is just a Wheatstone bridge sensor. It won't ever "lose weight". It measures a voltage, and sends that value to your Arduino, which uses it to calculate the weight on the load cell.

Messy code. lcd.init() called twice, line 4 of loop() doesn't look right, indentation is a mess, there's even a lengthy delay(1000) call in there.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

arjfca

Thank you for your input,


I will remove the Tare function from the initialisation.  It will be a forced using a selected switch.

I know that the code is messy.  This is a work in progress, first time coding in C, first time playing with Arduino, etc,  I'm an old timer, be indulgent  :)


Comments do help me to learn how to properly code and getting better with the tool.

Martin

DaveEvans

and there's this....

Code: [Select]
aButton ==10;

:smiley-confuse:


Go Up