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);
}
}