Greetings, Hope everyone is doing well.
I'm working on a project which is supposed to be basically a fuel quality checking device.
I'm attaching the code I'm using
//this code includes sending data to esp01 on serial and GPS readings
#include <HX711_ADC.h>
#include <EEPROM.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
//pins:8386427
char *DeviceID = "Device1";
int RXPin = 2;
int TXPin = 3;
int GPSBaud = 9600;
const int HX711_dout = A4; //mcu > HX711 dout pin
const int HX711_sck = A5; //mcu > HX711 sck pin
//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);
TinyGPSPlus gps;
SoftwareSerial gpsSerial(RXPin, TXPin);
static boolean newDataReady = 0;
long t;
const int serialPrintInterval = 500; //increase value to slow down serial print activity
static unsigned long lastSerialTime;
float calibrationValue = 432.54;//436.01; // uncomment this if you want to set the calibration value in the sketch327.49
long TareOffset = 8398661;//9532802;//8344838//8344747//8345467//8345516
#include <LiquidCrystal.h>
LiquidCrystal lcd (10,9,8,7,6,5);
#define BtnA 4
#define BtnB A1
#define BtnC A2
#define BtnD A3
int page = 0;
int weight;
float f_weight;
float units;
float mass1;
float mass2;
float mass3;
float kgs;
float new_factor_Weight;
float new_weight2;
float previous_weight;
float new_weight;
float new_volume;
float previous_volume;
int petrol = 0;
int diesel = 0;
int A_ButtonState = 0;
int B_ButtonState = 0;
int C_ButtonState = 0;
int D_ButtonState = 0;
int last_A_ButtonState = 0;
int last_B_ButtonState = 0;
int last_C_ButtonState = 0;
int last_D_ButtonState = 0;
volatile boolean SwA = false;
volatile boolean SwB = false;
volatile boolean SwC = false;
volatile boolean SwD = false;
unsigned long previousMillis = 0;
const long interval = 60000;
unsigned long currentMillis;
unsigned long previousMillis1 = 0;
const long interval1 = 5000;
unsigned long currentMillis1;
int Long;
int Lat;
//-----------------------------------------
void checkIf_A_ButtonIsPressed()
{
if (A_ButtonState != last_A_ButtonState)
{
if (A_ButtonState == 0)
{
SwA = true;
}
delay(50);
}
last_A_ButtonState = A_ButtonState;
}
//---------------------------------------------
void checkIf_B_ButtonIsPressed()
{
if (B_ButtonState != last_B_ButtonState)
{
if (B_ButtonState == 0)
{
SwB = true;
}
delay(50);
}
last_B_ButtonState = B_ButtonState;
}
//----------------------------------------------------
void checkIf_C_ButtonIsPressed()
{
if (C_ButtonState != last_C_ButtonState)
{
if (C_ButtonState == 0)
{
SwC = true;
}
delay(50);
}
last_C_ButtonState = C_ButtonState;
}
//----------------------------------------------------
void checkIf_D_ButtonIsPressed()
{
if (D_ButtonState != last_D_ButtonState)
{
if (D_ButtonState == 0)
{
SwD = true;
}
delay(50);
}
last_D_ButtonState = D_ButtonState;
}
//---------------------------------------------------------------------------------------------------------------------------------
void DisplayFuel()
{
if (petrol == 1)
{
lcd.setCursor(19,3);
lcd.print("P");
}
else if (diesel == 1)
{
lcd.setCursor(19,3);
lcd.print("D");
}
else
{
lcd.setCursor(19,3);
lcd.print("?");
}
}
//---------------------------------------------------------------------------------------------------------------------------------
void makeDecision()
{
lcd.setCursor(0,1);
if(petrol)
{
if(f_weight > 710 && f_weight < 775)
{
lcd.print("Good");
}
else
{
lcd.print("Bad");
}
}
else if(diesel)
{
if(f_weight > 810 && f_weight < 880)
{
lcd.print("Good");
}
else
{
lcd.print("Bad");
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------
void checkFuelQuality()
{
chkWeight();
makeDecision();
}
//---------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------
void chkWeight()
{
// check for new data/start next conversion:
if (LoadCell.update()) newDataReady = true;
// get smoothed value from the dataset:
if (newDataReady)
{
if (millis() - lastSerialTime >= serialPrintInterval)
{
weight = LoadCell.getData();
f_weight = weight;
Serial.print("Load cell output value:");
Serial.println(f_weight);
newDataReady = 0;
lastSerialTime = millis();
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------
void unit_factor()
{
if(new_weight2 > 49)
{
units = 1000.0/new_weight2;
Serial.print("Units division:");
Serial.println(units);
}
}
//---------------------------------------------------------------------------------------------------------------------------------
void setup()
{
Serial.begin(115200);
LoadCell.begin();
gpsSerial.begin(GPSBaud);
long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
boolean _tare = false; //set this to false if you don't want tare to be performed in the next step
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag())
{
Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
while (1);
}
else {
LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
LoadCell.setTareOffset(TareOffset);
Serial.println("Startup is complete");
}
pinMode(4, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
lcd.begin(20,4);
lcd.setCursor(5,0);
lcd.print("Welcome to");
lcd.setCursor(2,1);
lcd.print("Ready Automotive");
lcd.setCursor(0,3);
lcd.print("Technology Solutions");
delay(3000);
lcd.clear();
}
void loop()
{
//while (gpsSerial.available() > 0)
//{
// gps.encode(gpsSerial.read());
//if (gps.location.isUpdated())
// {
// }
// }
drawMenu();
A_ButtonState = digitalRead(BtnA);
B_ButtonState = digitalRead(BtnB);
C_ButtonState = digitalRead(BtnC);
D_ButtonState = digitalRead(BtnD);
checkIf_A_ButtonIsPressed();
checkIf_B_ButtonIsPressed();
checkIf_C_ButtonIsPressed();
checkIf_D_ButtonIsPressed();
if (SwA && page == 0)
{
SwA = false;
if(petrol == 1 || diesel == 1)
{
page = 2;
}
else
{
page = 5;
}
}
else if (SwA && page == 1)
{
SwA = false;
lcd.setCursor(0,3);
lcd.print("Petrol Selected");
delay(1000);
page = 2;
petrol = 1;
diesel = 0;
}
else if (SwA && page == 2)
{
SwA = false;
previous_volume = mass1;
//previous_weight = f_weight;
unit_factor();
page = 3;
}
else if (SwA && page == 3)
{
SwA = false;
page = 4;
}
if (SwB && page == 1)
{
SwB = false;
lcd.setCursor(0,3);
lcd.print("Diesel Selected");
delay(1000);
page = 2;
diesel = 1;
petrol = 0;
}
else if (SwB && page == 3)
{
SwB = false;
page = 0;
lcd.clear();
}
else if (SwB && page == 4)
{
SwB = false;
page = 0;
lcd.clear();
}
if (SwD && page == 0)
{
SwD = false;
page = 1;
}
else if(SwD && page == 5)
{
SwD = false;
page = 1;
}
}
void drawMenu()
{
delay(150);
switch (page)
{
case 0:
HomePage();
break;
case 1:
lcd.clear();
lcd.print("Select Fuel");
lcd.setCursor(0,1);
lcd.print("Press:");
lcd.setCursor(0,2);
lcd.print("A: Petrol B: Diesel");
break;
case 2:
lcd.clear();
lcd.print("Please fill");
lcd.setCursor(0,1);
lcd.print("1000ml Fuel");
lcd.setCursor(15,1);
chkWeight();
Serial.print("New total Weight:");
Serial.println(weight);
Serial.print("Previous weight:");
Serial.println(previous_weight);
new_weight2 = weight - previous_weight;
Serial.print("New sample weight:");
Serial.println(new_weight2);
lcd.print(new_weight2);
lcd.setCursor(0,2);
lcd.print("Press A");
lcd.setCursor(0,3);
lcd.print("when done");
DisplayFuel();
break;
case 3:
lcd.clear();
lcd.print("Fuel Quality is:");
checkFuelQuality();
lcd.setCursor(0,2);
lcd.print("A -> Yes");
lcd.setCursor(12,2);
lcd.print("B -> NO");
DisplayFuel();
break;
case 4:
lcd.clear();
lcd.print("Tank being filled");
lcd.setCursor(0,1);
lcd.print("Please wait...");
lcd.setCursor(0,2);
chkWeight();
new_weight = weight - previous_weight;
new_volume = new_weight * units;
lcd.print(new_volume);
lcd.setCursor(0,3);
lcd.print("Press B to exit");
DisplayFuel();
break;
case 5:
lcd.clear();
lcd.print("Please Select Fuel");
lcd.setCursor(0,1);
lcd.print("Press D");
lcd.setCursor(0,2);
lcd.print("to select Fuel");
break;
}
}
void HomePage()
{
delay(500);
Serial.println();
Serial.println();
Serial.println("*HOME PAGE*");
chkWeight();
Serial.print("Previous Weight:");
Serial.println(previous_weight);
Serial.print("Current Weight:");
Serial.println(f_weight);
if (f_weight != previous_weight)//current weight is not equal to previous weight
{
mass2 = f_weight - previous_weight;
Serial.print("New Weight:");
Serial.println(mass2);
}
Serial.print("Unit factor being used:");
Serial.println(units);
mass3 = mass2 * units;
Serial.print("New Volume:");
Serial.println(mass3);
Serial.print("previous Volume:");
Serial.println(previous_volume);
mass1 = previous_volume + mass3;
Serial.print("Total Volume:");
Serial.println(mass1);
previous_weight = f_weight;
lcd.setCursor(3,1);
lcd.print("Fuel Quantity:");
lcd.setCursor(5,2);
if(mass1 >= 1000)
{
kgs = mass1/1000.0;
lcd.print(kgs);
lcd.print("Ltrs");
Send_to_ESP(kgs);
}
else
{
lcd.print(mass1);
lcd.print("Ltrs");
Send_to_ESP(mass1);
}
Serial.print("final volume");
Serial.println(mass1);
Serial.println();
//if (previous_weight != mass2)
//{
}
//-------------------------------------------------------------------------------------------------------------------------------------------
void Send_to_ESP(float Weight)
{
chkWeight();
Serial.print("<");
//Serial.print(DeviceID);
//Serial.print(",");
Serial.print(gps.location.lat(), 6);
Serial.print(",");
Serial.print(gps.location.lng(), 6);
Serial.print(",");
Serial.print(Weight);
Serial.print(",");
Serial.print(f_weight);
Serial.print(",");
Serial.print(new_weight2);
Serial.println(">");
}
The code is messy but it works for most of the parts but only with two issues which I'm not able to fix.
- Whenever I gradually reduce the weight from loadcell the relevant reduction is not displayed on the screen.
- After a lil while the system output gets messed up on its own even though I do not change any weight on load cell
The issue #1 is becoz of the line
if (f_weight != previous_weight)//current weight is not equal to previous weight
{
mass2 = f_weight - previous_weight;
Serial.print("New Weight:");
Serial.println(mass2);
}
in the function homepage() but if I remove that if loop my mass2 value becomes 0 and no matter what I put on load cell it gives me zero output.
Any help is much appreciated
Thanks in advance