NEED HELP to Program a Batching System

Hello to all Arduino Community,

At First, I want to tell you the hardware of my setup.

  1. Arduino Mega,
  2. Loadcell 100 Kgs,
  3. HX711 (weight sensor),
  4. LCD Keypad Shield,
  5. 4x4 Keypad,
  6. 4 Channel relay module,

Sir, I want to make a batching System, Where I want to input the weight of the batch manually by 4x4 keypad currently modify the weight in the program, i am confused how to use multiple digit input with 4x4 keypad.

i will try to explain what i want to do in simple words,

every time i power up the arduino it ask to enter the weight, i will enter the weight with the help of 4x4 keypad e.g. 70 kgs for that I will press 7, 0, and to confirm the weight I will press #,

After the confirmation i will put the weight over load cell and the LCD indicates that the weight is increasing, as the loadcell detect the 70 Kgs a relay is on. and as i remove the weight the relay will off again.

Here i am giving you my codes, where i have reached so far.

#define RELAY_ON 0
#define RELAY_OFF 1
const int rly = 12;
const int rly2 = 11;
#include "HX711.h"
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {21, 20, 19, 18}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {17, 16, 15, 14}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

#define calibration_factor -42990.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define zero_factor -51468 //This large value is obtained using the SparkFun_HX711_Calibration sketch


#define DOUT  3
#define CLK  2

HX711 scale(DOUT, CLK);

void setup() {
  digitalWrite(rly, RELAY_OFF);
  digitalWrite(rly2, RELAY_OFF);
  lcd.begin(16,2);  


  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.set_offset(zero_factor); //Zero out the scale using a previously known zero_factor

  lcd.setCursor(0,0);
  lcd.print("Readings:");

    pinMode(rly, OUTPUT);
    pinMode(rly2, OUTPUT);
}


void loop() {
  lcd.setCursor(0,0);
  lcd.print("Reading: ");
  lcd.setCursor(2,1);
  lcd.print(scale.get_units(), 1); //scale.get_units() returns a float
  lcd.print("0 kg        "); //You can change to kg but you'll need to change the calibration_factor

  
  if (scale.get_units() > 3) { // here, in place of 3 Kgs i want to input the weight through 4x4 keypad
   digitalWrite(rly, RELAY_ON);
    }
  else {
       digitalWrite(rly, RELAY_OFF);
      }

  if (scale.get_units() > 5) {
   digitalWrite(rly2, RELAY_ON);
    }
  else {
       digitalWrite(rly2, RELAY_OFF);
      }

}

Any Suggestion will help me a lot

You haven't got any code to read the keypad.
I'm fairly sure there are examples provided with the keypad library.

There are examples in the library, but i don't get the result from that examples or may be i can't think the right way to use those example.

Sir I just want if any member of this forum could help me in writing the code right, or can modify my code to make it right.

Please this is really very important for me to move ahead.

You haven't posted any of your code to use the keypad or said exactly how it falls short of your expectations, so pointing out to you where you're going wrong is kinda tough.

If you want code writing for you, post your precise spec in the gigs section of the forum, and get your wallet out.

This sounds like it is the same project as in your other Thread which has already got to 18 Replies.

Why have you started a new Thread?

...R

Here is an example video,

I want to make like this system.

the only difference is, in this video the user input the delay value,

and i just want that i entered the weight value, and when the load cell sensor reaches that value, relay will active or deactivate.

Please see reply #4

#define RELAY_ON 0
#define RELAY_OFF 1
const int rly = 12;
const int rly2 = 11;
#include "HX711.h"
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {21, 20, 19, 18}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {17, 16, 15, 14}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

#define calibration_factor -42990.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define zero_factor -51468 //This large value is obtained using the SparkFun_HX711_Calibration sketch


#define DOUT  3
#define CLK  2

HX711 scale(DOUT, CLK);

boolean valOnePresent = false;
String num1, num2;
boolean final = false;


void setup() {
  digitalWrite(rly, RELAY_OFF);
  digitalWrite(rly2, RELAY_OFF);
  lcd.begin(16,2);  


  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.set_offset(zero_factor); //Zero out the scale using a previously known zero_factor

  lcd.setCursor(0,0);
  lcd.print("Readings:");

    pinMode(rly, OUTPUT);
    pinMode(rly2, OUTPUT);
}


void loop() {
  lcd.setCursor(0,0);
  lcd.print("Reading: ");
  lcd.setCursor(2,1);
  lcd.print(scale.get_units(), 1); //scale.get_units() returns a float
  lcd.print("0 kg        "); //You can change to kg but you'll need to change the calibration_factor

  char key = customKeypad.getKey();

  if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')){
    if (valOnePresent != true){
      num1 = num1 + key;
      int numLength = num1.length();
      lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
      lcd.print(num1);
    }
  else if (scale.get_units()> 'num1') {
   digitalWrite(rly, RELAY_ON);
    }
  else {
       digitalWrite(rly, RELAY_OFF);
      }


  }

}
  else if (scale.get_units()> 'num1') {

It hardly makes sense to compare a float to a character. Why are you doing that? While you are trying to determine the answer to that question, please post a picture of your keyboard with the SINGLE key labeled num1 circled.

I can't find the solution, that's why i came here, i have tried to explain everything what i know, what i did, and what i want to do,

so here what i want to do is, I want to take input from the keypad and store it as a variable and use the variable to compare with the current weight of the weight sensor,

and if the current weight is greater that the variable value turn the relay on,

I accept the i am a beginner and i know nothing about this but here i am trying to learn some thing,

and i expect that you, the Arduino Programming expert, can help me to solve this problem.

and tell me where i am going wrong with the code.

Sorry if you feel my behavior is rude, please forgive me, i am very frustrated here in trying codes.

  if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')){
    if (valOnePresent != true){
      num1 = num1 + key;

Ok, you've got a text representation of a number in the String variable "num1".

How do you know when you've finished entering your value?

When you've figured that out, you need to convert that String value into a number.
Have a look at the String class's "toInt" or "toFloat" methods.
Don't forget to clear "num1" when you've finished with it.
It's all in the reference section

As AWOL said, we might help you to solve an issue in your code or give suggestions to improve. But might not able to create a whole system for you. See the post #4.

As you said you are a beginner, don't you think it's better to start with small code base and experiment with each functionality. For e.g.

  • Write code to switch on/off relays.
  • Deal with number pads.
  • Experiment with weight sensors.

Once you are good with the above experiments and tracer code, it will be easy to create what you really wanted.

I could see that you don't have the basics of programming, worth spending time in understanding C programming language.

...and don't forget to include methods to allow you to clear an entry, in case you mis-key.

  if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')){

Since the values '0' to '9' are all covered by this statement, it could be a lot shorter:

  if (key >= '0' && key <= '9')
  {

I got the solution for input from keypad and store as a String and convert the string as Integer,

Many Many thanks to AWOL for toInt method, without the help of toInt method, i can't imagine how long i have to suffer to get the solution from AWOL.

Mr. AWOL. now you understand me, i just need a proper hint,

and thank you so much Mr. PaulS for if (key >= '0' && key <= '9')

and now I want to extend my system,

now i know how to control 1 relay,

buy i want to know how to control multiple relay.

i program the arduino that

wait for the value

as i input the value i.e. 15

and i press A to select the relay

and i press # to confirm the weight value and relay selection.

but if i am trying to connect multiple relay it does nothing only relay 1 is working.

Here is my Code

#define RELAY_ON 0
#define RELAY_OFF 1
const int rly = 12;
const int rly2 =11;

#include "HX711.h"
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {21, 20, 19, 18}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {17, 16, 15, 14}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

#define calibration_factor -42990.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define zero_factor -51468 //This large value is obtained using the SparkFun_HX711_Calibration sketch


#define DOUT  3
#define CLK  2

HX711 scale(DOUT, CLK);

boolean valOnePresent = false;
boolean next = false;
boolean final = false;
String num1;
int ans = 10000;
char op;

void setup() {
  digitalWrite(rly, RELAY_OFF);
  digitalWrite(rly2, RELAY_OFF);
  lcd.begin(16,2);  


  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.set_offset(zero_factor); //Zero out the scale using a previously known zero_factor

  lcd.setCursor(0,0);
  lcd.print("Readings:");

  pinMode(rly, OUTPUT);
  pinMode(rly, OUTPUT);

}

void loop() {
  lcd.setCursor(0,0);
  lcd.print("Reading: ");
  lcd.setCursor(2,1);
  lcd.print(scale.get_units(), 1); //scale.get_units() returns a float
  lcd.print("0 kg        "); //You can change to kg but you'll need to change the calibration_factor

  char key = customKeypad.getKey();
  if (key != NO_KEY && (key >= '0' && key <= '9')){
    if (valOnePresent != true){
      num1 = num1 + key;
      int numLength = num1.length();
      lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
      lcd.print(num1);
      final = true;
      
    }
  }

  else if (valOnePresent == false && key != NO_KEY && (key == 'A' || key == 'B' || key == 'C' || key == 'D')){
    if (valOnePresent == false){
    valOnePresent = true;
    op = key;
    lcd.setCursor(15,0); //operator on right corner
    lcd.print(op);
    }
  }

  else if (final == true && key != NO_KEY && key == '#'){
    if (op == 'A'){
      ans = num1.toInt();
      digitalWrite(rly, RELAY_ON);
      lcd.clear();
      lcd.setCursor(15,0);
      lcd.autoscroll();
      lcd.print(ans);
      lcd.noAutoscroll();
    }
    if (op == 'B'){
      ans = num1.toInt();
      digitalWrite(rly2, RELAY_ON);
      lcd.clear();
      lcd.setCursor(15,0);
      lcd.autoscroll();
      lcd.print(ans);
      lcd.noAutoscroll();
    }
  }

  
  else if (key != NO_KEY && key == '*'){
    lcd.clear();
    valOnePresent = false;
    final = false;
    num1 = "";
    ans = 10000;
    op = ' ';
  }

     if (scale.get_units()> ans) {
   digitalWrite(rly, RELAY_OFF);
   digitalWrite(rly2, RELAY_OFF);
  }
}

but if i am trying to connect multiple relay it does nothing only relay 1 is working.

You don't have anything in your code that looks like "relay 1". You have rly and rly2 (why not the more logical rly1 and rly2?).

So, what actually works?

  pinMode(rly, OUTPUT);
  pinMode(rly, OUTPUT);

Hmmm...

Thank You So Much PaulS, :slight_smile: :slight_smile: :slight_smile:

Hope if i Stuck in future, you all are here to help me out.

Thank you All.

Here is my Code

#define RELAY_ON 0
#define RELAY_OFF 1
const int rly = 12;
const int rly2 =11;

#include "HX711.h"
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {21, 20, 19, 18}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {17, 16, 15, 14}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

#define calibration_factor -42990.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define zero_factor -51468 //This large value is obtained using the SparkFun_HX711_Calibration sketch


#define DOUT  3
#define CLK  2

HX711 scale(DOUT, CLK);

boolean valOnePresent = false;
boolean next = false;
boolean final = false;
String num1;
int ans = 10000;
int ans2 = 10000;
char op;

void setup() {
  digitalWrite(rly, RELAY_OFF);
  digitalWrite(rly2, RELAY_OFF);
  lcd.begin(16,2);  


  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.set_offset(zero_factor); //Zero out the scale using a previously known zero_factor

  lcd.setCursor(0,0);
  lcd.print("Readings:");

  pinMode(rly, OUTPUT);
  pinMode(rly2, OUTPUT);

}

void loop() {
  lcd.setCursor(0,0);
  lcd.print("Target Wt: ");
  lcd.setCursor(0,1);
  lcd.print("Wt: ");
  lcd.setCursor(5,1);
  lcd.print(scale.get_units(), 1); //scale.get_units() returns a float
  lcd.print("0 kg      "); //You can change to kg but you'll need to change the calibration_factor

  char key = customKeypad.getKey();
  if (key != NO_KEY && (key >= '0' && key <= '9')){
    if (valOnePresent != true){
      num1 = num1 + key;
      int numLength = num1.length();
      lcd.setCursor(13 - numLength, 0); //to adjust one whitespace for operator
      lcd.print(num1);
      final = true;
      
    }
  }

  else if (valOnePresent == false && key != NO_KEY && (key == 'A' || key == 'B' || key == 'C' || key == 'D')){
    if (valOnePresent == false){
    valOnePresent = true;
    op = key;
    char opLength = sizeof(op);
    lcd.setCursor(13 - opLength, 0); //to adjust one whitespace for operator
    lcd.setCursor(13,0); //operator on right corner
    lcd.print("Kgs");
    }
  }

  else if (final == true && key != NO_KEY && key == '#'){
    if (op == 'A'){
      ans = num1.toInt();
      digitalWrite(rly, RELAY_ON);
      lcd.clear();
      lcd.print("Target Wt:");
      lcd.setCursor(12, 0);
      lcd.print(ans);
      lcd.print("KGs");
      lcd.noAutoscroll();
    }
    if (op == 'B'){
      ans2 = num1.toInt();
      digitalWrite(rly2, RELAY_ON);
      lcd.clear();
      lcd.print("Target Wt:");
      lcd.setCursor(12, 0);
      lcd.print(ans2);
      lcd.print("KGs");
      lcd.noAutoscroll();
    }
  }

  
  else if (key != NO_KEY && key == '*'){
    lcd.clear();
    valOnePresent = false;
    final = false;
    num1 = "";
    ans = 10000;
    op = ' ';
  }

     if (scale.get_units()> ans) {
   digitalWrite(rly, RELAY_OFF);
  }
     if (scale.get_units()> ans2) {
   digitalWrite(rly2, RELAY_OFF);
  }
}

It's more usual to set the pinMode and then write to the pin.