Hello.....I have tried to make batch controller with arduino mega, hx711, keypad, 16x2 display, I am not a programmer but somehow I manage to code for my requirements with the help of this as well as othe forum and tutorials.
Following is code which is working however its not clean and proper and I am trying to make it more user friendly
#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <HX711.h>
#define SCALE_DOUT_PIN 31
#define SCALE_SCK_PIN 33
HX711 scale(SCALE_DOUT_PIN, SCALE_SCK_PIN);
LiquidCrystal_I2C lcd(0x3F, 16, 2);
char TareWeight[6];
char Capacity[3];
int i = 0;
int relay_pin = 10;
char key_pressed = 0;
const byte rows = 4;
const byte columns = 4;
char hexaKeys[rows][columns] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'.', '0', '#', 'D'}
};
byte row_pins[rows] = {30, 32, 34, 36};
byte column_pins[columns] = {38, 40, 42, 44};
Keypad keypad_key = Keypad( makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);
void setup()
{
Serial.begin(38400);
pinMode(relay_pin, OUTPUT);
scale.begin(31, 33);
scale.set_scale(21250.f);// <- set here calibration factor!!!
scale.tare();
lcd.begin();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Filling");
lcd.setCursor(0, 1);
lcd.print("Machine ");
delay(2000);
int j = 0;
j = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Capacity:");
lcd.setCursor(0, 1);
while (j < 3)
{
char key = keypad_key.getKey();
if (key)
{
lcd.print(key);
Capacity[j++] = key;
}
key = 0;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Capacity Changed");
delay(1000);
}
void loop()
{
digitalWrite(relay_pin, LOW);
float Weight = scale.get_units(5);
Serial.println(Weight);
Serial.println(Capacity);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter TareWeight");
int i = 0;
i = 0;
lcd.setCursor(0, 1);
while (i < 5)
{
char key = keypad_key.getKey();
if (key)
{
TareWeight[i++] = key;
lcd.print(key);
}
}
if (i == 5) {
while (Weight >= atof(TareWeight) && Weight <= atof(Capacity) + atof(TareWeight))
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Filling Start");
lcd.setCursor(0, 1);
lcd.print(Weight);
digitalWrite(relay_pin, HIGH);
break;
}
}
if (Weight < atof(TareWeight))
{
digitalWrite(relay_pin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Loss Tare Weight");
lcd.setCursor(0, 1);
lcd.print(Weight);
}
if (Weight > (atof(Capacity) + atof(TareWeight)))
{
digitalWrite(relay_pin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("overfilled");
lcd.setCursor(0, 1);
lcd.print(Weight);
}
i = 0;
}
Further I have modified as below but its not working properly.
#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <HX711.h>
#define SCALE_DOUT_PIN 31
#define SCALE_SCK_PIN 33
HX711 scale(SCALE_DOUT_PIN, SCALE_SCK_PIN);
LiquidCrystal_I2C lcd(0x3F, 16, 2);
char TareWeight[6];
char Capacity[3];
int i = 0;
int relay_pin = 10;
char key_pressed = 0;
const byte rows = 4;
const byte columns = 4;
char hexaKeys[rows][columns] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'.', '0', '#', 'D'}
};
byte row_pins[rows] = {30, 32, 34, 36};
byte column_pins[columns] = {38, 40, 42, 44};
Keypad keypad_key = Keypad( makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);
void Change()
{
int j = 0;
j = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Capacity:");
lcd.setCursor(0, 1);
while (j < 3)
{
char key = keypad_key.getKey();
if (key)
{
lcd.print(key);
Capacity[j++] = key;
}
key = 0;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Capacity Changed");
delay(1000);
}
void TW()
{
int i = 0;
i = 0;
float Weight = scale.get_units(5);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter TareWeight");
lcd.setCursor(0, 1);
while (i < 5)
{
char key = keypad_key.getKey();
if (key)
{
TareWeight[i++] = key;
lcd.print(key);
}
}
}
void filling()
{
float Weight = scale.get_units(5);
while (Weight >= atof(TareWeight) && Weight <= atof(Capacity) + atof(TareWeight))
{
float Weight = scale.get_units(5);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Filling Start");
lcd.setCursor(0, 1);
lcd.print(Weight);
digitalWrite(relay_pin, LOW);
}
{
lcd.setCursor(0, 0);
lcd.print("Filling Done ");
delay (500);
}
}
void setup()
{
Serial.begin(38400);
pinMode(relay_pin, OUTPUT);
scale.begin(31, 33);
scale.set_scale(21562.f);// <- set here calibration factor!!!
scale.tare();
lcd.begin();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Filling Machine");
lcd.setCursor(0, 1);
lcd.print("Krunal");
delay(2000);
Change();
}
void loop()
{
TW ();
float Weight = scale.get_units(5);
if (Weight < atof(TareWeight))
{
digitalWrite(relay_pin, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Loss Tare Weight");
lcd.setCursor(0, 1);
lcd.print(Weight);
delay(1000);
}
if (Weight > (atof(Capacity) + atof(TareWeight)))
{
digitalWrite(relay_pin, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("overfilled");
lcd.setCursor(0, 1);
lcd.print(Weight);
delay(1000);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("A Start B Change");
lcd.setCursor(0, 1);
lcd.print("C Restart");
lcd.setCursor(10, 1);
lcd.print(Weight);
char key = keypad_key.getKey();
switch (key)
{
case'A':
filling();
break;
case'B':
Change();
break;
case'C':
TW();
break;
default:
digitalWrite(relay_pin, LOW);
}
}
Requesting to help to make it working......many thanks in advance