I am using HX711 loadcell to measure weight.
So i just checked it in a testing code so that it can show the values in LCD monitor.
[LCD_and_HX.ino]
#include <LiquidCrystal.h>
#include "HX711.h" //You must have this library in your arduino library folder
int Contrast = 60;
#define DOUT 3
#define CLK 2
HX711 scale(DOUT, CLK);
LiquidCrystal lcd(A0, A1, 50, 51, 52, 53); //(rs, enable, d4, d5, d6, d7)
float calibration_factor = -106600;
void setup() {
Serial.begin(9600);
analogWrite(6,Contrast);
lcd.begin(16, 2);
scale.set_scale();
scale.tare(); //Reset the scale to 0
long zero_factor = scale.read_average(); //Get a baseline reading
lcd.setCursor(0,0);
lcd.print("Hey raja");
delay(1000);
lcd.clear();
}
void loop() {
weight();
}
void weight(){
lcd.clear();float measure;
scale.set_scale(calibration_factor); //Adjust to this calibration factor
measure=scale.get_units();
lcd.setCursor(0,0);
lcd.print("weight placed is");
lcd.setCursor(5,1);
lcd.print(measure);
lcd.setCursor(10,1);
lcd.print(" kg");
delay(1000);
lcd.clear();
}
is the code i used.
AND IT WORKS
BUT WHEN I use the same code in my larger programme i.e., [modified.ino]
#include <LiquidCrystal.h>
#include <Keypad.h>
#include<Servo.h>
#include "HX711.h"
#define DOUT 3
#define CLK 2
HX711 scale(DOUT, CLK);
float calibration_factor = -106600;
const int gasPin = A2; //output of gas pin
int x=1;
int l=1;
void servomotor();
int sensorvalue;
int servoPin = 3;//output of servo in
Servo Servo1;
int LCDset(int);
int q;
int KeypadTimer(int);
char entryStr[4];
signed short minutes, secondes,mi,se;
char timeline[16];
int MK = 0;
int as=0;;
String readString;
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
int value[4];
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {22,24,26,28}; //maroo, grey,white
byte colPins[COLS] = {23,25,27,29}; //orange,yellow,green
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int i=0;
int Contrast=60;
LiquidCrystal lcd(A0, A1, 50, 51, 52, 53); //(rs, enable, d4, d5, d6, d7)
float weight;
void setup() {
Serial.begin(9600);
analogWrite(6,Contrast);
lcd.begin(16, 2);
Servo1.attach(servoPin);
Servo1.write(0); //write the on position if the gas regulaotor
Servo1.write(0);
Servo1.write(0);
scale.set_scale();
scale.tare();
long zero_factor = scale.read_average();
lcd.clear();
}
void loop() {
/* //weight measurement
// scale.set_scale(calibration_factor);
//delay(100);
weight=scale.get_units();
weight=scale.get_units();
weight=scale.get_units();
weight=scale.get_units();
weight=scale.get_units();
weight=scale.get_units();
weight=scale.get_units();
delay(100);
Serial.println(weight);
delay(1000);
/*lcd.setCursor(0,0);
lcd.print("weight placed is");
lcd.setCursor(5,1);
lcd.print(weight);
lcd.setCursor(10,1);
lcd.print(" kg");
delay(1000);
//weight measurement completed*/
// Gas Leakage Detection
sensorvalue=analogRead(gasPin);
if(sensorvalue>400){
//tone(8,10,10000); //setup the buzzer
//Servo1.write(180); //to the angle servo to be roated
Serial.println("Gas is Leaking");
}
// Timer through Keypad
char key = keypad.getKey();
if(key){
if(MK==0){
int p = KeypadTimer(MK);
q = LCDset(2);
}
}
//Timer through Mobile
while (Serial.available()) {
delay(10); //small delay to allow input buffer to fill
char c = Serial.read(); //gets one byte from serial buffer
readString += c;
}
Serial.print(readString); // Should be removed
for(i=0;i<4;i=i+1){
if(readString[i]>=48){
value[i]=readString[i];
value[i]=readString[i]-48;
}
}
if(as==0){
if(value[0]>=0 && value[1]>=0 &&value[2]>=0 && value[0]>=0){
MK=1;
q=LCDset(2);
}
}
//Turning on Servo Motor
if(q==2){
servomotor();
}
weight(); //to measure weight
}
int KeypadTimer(int a){
MK=1;
char key1 = keypad.getKey();
while(key1!='#'){
char key = keypad.getKey();
if (key){
if (key == '*'){
memset(entryStr, 0, sizeof(entryStr));
i=0;
key=0;
Serial.println("Set Time");
}
else if (key != '#'){
entryStr[i]= key;
i++;
//lcd.print(entryStr[i]);
//Serial.print(key);
}
else {
Serial.println("");
i=0;
key=0;
String fileName = entryStr;
memset(entryStr, 0, sizeof(entryStr));
Serial.println(fileName);
key1='#';
value[0]=0;value[1]=0;value[2]=0;value[3]=0;
for(i=0;i<4;i=i+1){
value[i]=fileName[i];
value[i]=value[i]-48;}
}
}
}
return(1);
}
int LCDset(int a){
as=1;
minutes=value[0]*10+value[1]; //start of LCD code
secondes=value[2]*10+value[3];
int k =(value[0]*10+value[1])*60+(value[2]*10+value[3]);
mi= minutes;
se =secondes;
if(k!=0){
for(;k!=0;k=k-1){
char key = keypad.getKey();
if(key=='*'){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reser Timer ");
int p = KeypadTimer(MK);
if(p){
q= LCDset(2);
/*
if(q==2){
//servo motor code;
}
*/
}
}
// Gas Leakage Detection
sensorvalue=analogRead(gasPin);
if(sensorvalue>400){
//tone(8,10,10000); //setup the buzzer
//Servo1.write(180); //to the angle servo to be roated
Serial.println("Gas is Leaking");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Gas Leaking ");
delay(1000);
exit(0);
}
lcd.setCursor(0, 0);
lcd.print("Time : ");
lcd.print(mi);
lcd.print("M ");
lcd.print(se);
lcd.print("S");
lcd.setCursor(0, 1);
sprintf(timeline,"%0.2dm %0.2ds", minutes, secondes);//should change
lcd.print(timeline);
delay(1000);
if(secondes!=0)
secondes--;
if (secondes == 0)
{
secondes = 60;
minutes --;
}}
value[0]=0;value[1]=0;value[2]=0;value[3]=0;
lcd.clear();
}
MK=0;
as=0;
return(2);
}
void servomotor(){
Servo1.write(180);
Servo1.write(180);
Servo1.write(180);
}
void weight(){
lcd.clear();
float measure;
scale.set_scale(calibration_factor);
Serial.println("hey raja");
delay(100);
measure=scale.get_units();
measure=scale.get_units();
measure=scale.get_units();
measure=scale.get_units();
measure=scale.get_units();
measure=scale.get_units();
delay(100);
Serial.println("2016068");
lcd.setCursor(0,0);
lcd.print("weight placed is");
lcd.setCursor(5,1);
lcd.print(measure);
Serial.println(measure);
Serial.println();
lcd.setCursor(10,1);
lcd.print(" kg");
delay(1000);
lcd.clear();
}
it always shows 0.00 and DOES NOT CHANGE when i place any weight.
PLZZZ HELP ME OUT
measure=scale.get_units(); is not able to receive values in 2nd code
LCD_and_HX.ino (967 Bytes)
modified.ino (5.59 KB)