// kode untuk kalibrasi
// //HX711 load cell calibration program code https://www.circuitschools.com
// #include "HX711.h"
// //data pin and clock pin
// byte pinData = A0;
// byte pinClk = A1;
// HX711 scale;
// //Parameter to calibrate weight and sensor, different for different sensors
// float calibration_factor = 137326; //put some value and adjust it through serial monitor
// // float calibration_factor = 426;
// void setup() {
// Serial.begin(9600);
// Serial.println("HX711 calibration");
// Serial.println("Remove all weight from scale");
// Serial.println("After readings begin, place known weight on scale");
// Serial.println("Press a,s,d,f to increase calibration factor by 10,100,1000,10000 respectively");
// Serial.println("Press z,x,c,v to decrease calibration factor by 10,100,1000,10000 respectively");
// scale.begin(pinData, pinClk);
// //apply the calibration
// scale.set_scale();
// //initializing the tare.
// scale.tare(); //Reset the scale to 0
// long zero_factor = scale.read_average(); //Get a baseline reading
// Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
// Serial.println(zero_factor);
// }
// void loop() {
// scale.set_scale(calibration_factor); //Adjust to this calibration factor
// Serial.print("Reading: ");
// Serial.print(scale.get_units(), 2);
// Serial.print(" kgs"); //Change this to grams and re-adjust the calibration factor if you follow SI units like a sane person
// Serial.print(" calibration_factor: ");
// Serial.print(calibration_factor);
// Serial.println();
// if(Serial.available())
// {
// char temp = Serial.read();
// if(temp == '+' || temp == 'a')
// calibration_factor += 10;
// else if(temp == '-' || temp == 'z')
// calibration_factor -= 10;
// else if(temp == 's')
// calibration_factor += 100;
// else if(temp == 'x')
// calibration_factor -= 100;
// else if(temp == 'd')
// calibration_factor += 1000;
// else if(temp == 'c')
// calibration_factor -= 1000;
// else if(temp == 'f')
// calibration_factor += 10000;
// else if(temp == 'v')
// calibration_factor -= 10000;
// else if(temp == 't')
// scale.tare(); //Reset the scale to zero
// }
// }
//Program code for digital weighing scale using Arduino and HX711 with load cell
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include "HX711.h"
Servo myservo1;
Servo myservo2;
int pos1 = 0;
int pos2 = 180;
int count = 0;
int count1 = 0;
int count2 = 0;
int pilihan = 0;
float setting = 1.000;
float gram = 0.000;
#define DEBUG_HX711
// calibration parameter from calibrating code with known values
#define CALIBRATION_FACTOR 420.0
// Create the lcd object address 0x3F and 16 columns x 2 rows
LiquidCrystal_I2C lcd (0x27, 16,2);
// data pin and clock pin
byte pinData = A0;
byte pinClk = A1;
#define BUTTON1 2
#define BUTTON2 3
#define BUTTON3 4
#define BUTTON4 5
#define BUTTON5 6
bool state1;
bool state2;
bool state3;
bool state4;
bool state5;
bool stopstate = LOW;
bool startstate = LOW;
bool stateposition = LOW;
int lastState1 = HIGH;
int lastState2 = HIGH;
int lastState3 = HIGH;
int lastState4 = HIGH;
int lastState5 = HIGH;
// define HX711
HX711 scale;
void setup() {
myservo1.attach(11);
myservo2.attach(10);
myservo1.write(pos1);
myservo2.write(pos1);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print( "TIMBANGAN JAJAN" );
lcd.setCursor(0,1);
lcd.print( " OTOMATIS " );
delay(1000);
lcd.setCursor (0, 0);
lcd.print(" PILIH BERAT ");
lcd.setCursor (0, 1);
lcd.print("JAJAN : ");
pinMode(BUTTON1, INPUT_PULLUP);
pinMode(BUTTON2, INPUT_PULLUP);
pinMode(BUTTON3, INPUT_PULLUP);
pinMode(BUTTON4, INPUT_PULLUP);
pinMode(BUTTON5, INPUT_PULLUP);
#ifdef DEBUG_HX711
// Initialize serial communication
Serial.begin(9600);
Serial.println("[HX7] Sensor start HX711");
#endif
//Initializing sensor
scale.begin(pinData, pinClk);
// apply the calibration value
scale.set_scale(CALIBRATION_FACTOR);
// Initialize the tare
//Assuming there is no weight on the scale at start up, reset the scale to 0
scale.tare();
}
void loop() {
int value1 = digitalRead((BUTTON1));
int value2 = digitalRead((BUTTON2));
int value3 = digitalRead((BUTTON3));
int value4 = digitalRead((BUTTON4));
int value5 = digitalRead((BUTTON5));
int position = myservo2.read();
if (lastState5 != value5) {
lastState5 = value5;
if (value5 == LOW) {
count1 ++;
if (count1==1){
stopstate = HIGH;
lcd.setCursor(0,0);
lcd.clear();
lcd.print( " SISTEM STOP " );
Serial.println("stop");
}else{
lcd.clear();
lcd.print( " PILIH BERAT " );
Serial.println("start");
}
if (count1 >=2){
count1=0;
}
Serial.print("count1 =");
Serial.println(count1);
state5 = HIGH;
}else{
state5 = LOW;
}
}
if (lastState1 != value1) {
lastState1 = value1;
if (value1 == LOW) {
state1 = HIGH;
stopstate = HIGH;
startstate = LOW;
lcd.clear();
lcd.print( " SISTEM STOP " );
Serial.println("stop");
myservo1.write(pos2);
delay(50);
}else{
state1 = LOW;
stopstate = LOW;
// stopstate = HIGH;
lcd.clear();
lcd.print( " PILIH BERAT " );
Serial.println("start");
myservo1.write(pos1);
delay(50);
}
}
if (lastState2 != value2) {
lastState2 = value2;
if (value2 == LOW) {
count2 ++;
stopstate = HIGH;
startstate = LOW;
lcd.clear();
lcd.print( " SISTEM STOP " );
Serial.println("stop");
if (count2==1){
if (position==pos1){
myservo2.write(pos2);
delay(50);
}else{
myservo2.write(pos1);
delay(50);
}
count2=0;
}
}
delay(1000);
state2 = LOW;
stopstate = LOW;
lcd.clear();
lcd.print( " PILIH BERAT " );
Serial.println("start");
}
// if (lastState2 != value2) {
// lastState2 = value2;
// if (value2 == LOW) {
// state2 = HIGH;
// stopstate = HIGH;
// startstate = LOW;
// lcd.clear();
// lcd.print( " SISTEM STOP " );
// Serial.println("stop");
// if (position==pos1){
// myservo2.write(pos2);
// delay(50);
// }else{
// myservo2.write(pos1);
// delay(50);
// }
// }else{
// state2 = LOW;
// stopstate = LOW;
// // stopstate = HIGH;
// lcd.clear();
// lcd.print( " PILIH BERAT " );
// Serial.println("start");
// if (position==pos1){
// myservo2.write(pos2);
// delay(50);
// }else{
// myservo2.write(pos1);
// delay(50);
// }
// }
// }
// if (lastState2 != value2) {
// lastState2 = value2;
// if (value2 == LOW) {
// state2 = HIGH;
// }else{
// state2 = LOW;
// }
// }
if (lastState3 != value3) {
lastState3 = value3;
if (value3 == LOW) {
state3 = 0;
pilihan += 1;
Serial.print("pilihan = ");
Serial.println(pilihan);
if (pilihan >=4){
pilihan = 1;
}
}
}
if (state3==0){
if (pilihan == 1){
setting = 0.250;
lcd.setCursor (0, 1);
lcd.print("JAJAN : 250 gr ");
} else if (pilihan == 2){
setting = 0.500;
lcd.setCursor (0, 1);
lcd.print("JAJAN : 500 gr ");
} else if (pilihan == 3){
setting = 1.000;
lcd.setCursor (0, 1);
lcd.print("JAJAN : 1000 gr ");
}
}
gram = scale.get_units();
#ifdef DEBUG_HX711
#endif
if (lastState4 != value4) {
lastState4 = value4;
if (value4 == LOW) {
state4 = HIGH;
startstate=HIGH;
lcd.clear();
lcd.setCursor (0, 0);
lcd.print("PILIHAN : ");
lcd.print(setting);
lcd.print("kg");
lcd.setCursor (0, 1);
lcd.print(" ");
state3 = 1;
}
// if (startstate==HIGH){
// myservo1.write(pos2);
// delay(50);
// }
}
if (state3==1){
lcd.setCursor (0, 1);
lcd.print( "BERAT : " );
lcd.print(gram, 3);
lcd.print(" kg");
}
// if (startstate==HIGH){
// if (gram<5){
// myservo1.write(pos2);
// delay(50);
// startstate=LOW;
// }
// }
if (gram >= setting && stopstate==LOW){
myservo1.write(pos1);
delay(50);
// Serial.println(stateposition);
if (stateposition==0){
// Serial.println("servo2 gerak");
myservo2.write(pos2);
delay(50);
} else {
myservo2.write(pos1);
delay(50);
}
// }else if (gram>5 && gram<setting && stopstate==LOW){
}else{
if (state1==LOW && startstate==HIGH){
myservo1.write(pos2);
delay(50);
if (position==pos2){
stateposition=HIGH;
}else{
stateposition=LOW;
}
// Serial.println(stateposition);
}
}
Serial.print("startstate =");
Serial.println(startstate);
// if (gram >= setting){
// myservo1.write(pos2);
// delay(50);
// if (state2==HIGH){
// if (count==1){
// myservo2.write(pos1);
// delay(50);
// } else {
// myservo2.write(pos2);
// delay(50);
// }
// }
// }else{
// if (state1==1){
// myservo1.write(pos1);
// delay(50);
// count = count + 1;
// if (count>=3){
// count = 1;
// }
// }
// }
}