/**
Arduino Electronic Safe
Copyright (C) 2020, Uri Shaked.
Released under the MIT License.
*/
#include <LiquidCrystal.h>
#include "HX711.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 12;
const int LOADCELL_SCK_PIN = 13;
const int pb1Pin = 3; //P0 of PCF8574 (+/tare)
const int pb2Pin = 4; //P1 of PCF8574 (-)
const int pb3Pin = 5; //P2 of PCF8574 (mode/enter)
const int relayPin = 6; //P3 of PCF8574 (relay)
const int alarmPin = 7; //D7>>GPIO13
HX711 scale;
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27
//define variable
int mode = 0; //0 for normal mode, 1 for setup mode, 2 for operation mode
float weight;
float weightAlarm = 2.5; //gram unit >>set default weight alarm
float weightError = 0.0;
float weightSend = 0.0;
float weightMax = 0.0;
int pb1CurrentState = LOW;
int pb1PreviousState = LOW;
int pb2CurrentState = LOW;
int pb2PreviousState = LOW;
int pb3CurrentState = LOW;
int pb3PreviousState = LOW;
void setup() {
Serial.begin(9600);
//ThingSpeak.begin(client1);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(445.f); //loadcell 5kg
scale.tare();
lcd.init();
lcd.backlight();
pinMode(alarmPin,OUTPUT);
pinMode(pb1Pin,INPUT);
pinMode(pb2Pin,INPUT);
pinMode(pb3Pin,INPUT);
pinMode(relayPin,OUTPUT);
digitalWrite(alarmPin,LOW); //off red led >> finish
digitalWrite(relayPin,LOW);
}
void loop() {
if (mode == 0){ //normal mode
weight = (scale.get_units(5))*1;
weight = weight; //in gram
//delay(200);
lcd.setCursor(0,0);
lcd.print("weight (g)");
lcd.setCursor(0,1);
lcd.print(weight);
lcd.setCursor(8,1);
lcd.print("g");
pb1CurrentState = digitalRead(pb1Pin); // อ่านค่าจาก pbPin
pb2CurrentState = digitalRead(pb2Pin); // อ่านค่าจาก pbPin
pb3CurrentState = digitalRead(pb3Pin); // อ่านค่าจาก pbPin
if (pb1CurrentState != pb1PreviousState) { // เมื่อมีการเปลี่ยนแปลงสถานะ
if (pb1CurrentState == HIGH) { // ถ้าเป็นสถานะ HIGH (เมื่อมีการกดปุ่ม)
scale.tare(); //set zero of weight scale
Serial.println("tare to zero");
}
}
if (pb2CurrentState != pb2PreviousState) { // เมื่อมีการเปลี่ยนแปลงสถานะ
if (pb2CurrentState == HIGH) { // ถ้าเป็นสถานะ HIGH (เมื่อมีการกดปุ่ม)
mode = 2; //go to operation mode
Serial.println("in porcess");
scale.tare();
lcd.clear();
}
}
if (pb3CurrentState != pb3PreviousState) { // เมื่อมีการเปลี่ยนแปลงสถานะ
if (pb3CurrentState == HIGH) { // ถ้าเป็นสถานะ HIGH (เมื่อมีการกดปุ่ม)
mode = 1; //go to setup mode
lcd.clear();
pb1PreviousState = LOW;
pb2PreviousState = LOW;
pb3PreviousState = LOW;
}
}
pb1PreviousState = pb1CurrentState; // อัปเดตสถานะก่อนหน้า
pb2PreviousState = pb2CurrentState;
pb3PreviousState = pb3CurrentState;
delay(100);
}// mode 0
if (mode == 1){ //setup mode
lcd.setCursor(0,0);
lcd.print("setup mode");
lcd.setCursor(0,1);
lcd.print("wAlarm:");
lcd.setCursor(9,1);
lcd.print(weightAlarm);
pb1CurrentState = digitalRead(pb1Pin); // อ่านค่าจาก pbPin
pb2CurrentState = digitalRead(pb2Pin); // อ่านค่าจาก pbPin
pb3CurrentState = digitalRead(pb3Pin); // อ่านค่าจาก pbPin
if (pb1CurrentState != pb1PreviousState) { // เมื่อมีการเปลี่ยนแปลงสถานะ
if (pb1CurrentState == HIGH) { // ถ้าเป็นสถานะ HIGH (เมื่อมีการกดปุ่ม)
weightAlarm = weightAlarm + 0.01;
}
}
else{
if (pb1CurrentState == HIGH) { // ถ้าเป็นสถานะ HIGH (เมื่อมีการกดปุ่ม)
weightAlarm = weightAlarm + 0.01;
}
}
if (pb2CurrentState != pb2PreviousState) { // เมื่อมีการเปลี่ยนแปลงสถานะ
if (pb2CurrentState == HIGH) { // ถ้าเป็นสถานะ HIGH (เมื่อมีการกดปุ่ม)
weightAlarm = weightAlarm - 0.01;
}
}
else{
if (pb2CurrentState == HIGH) { // ถ้าเป็นสถานะ HIGH (เมื่อมีการกดปุ่ม)
weightAlarm = weightAlarm - 0.01;
}
}
if (pb3CurrentState != pb3PreviousState) { // เมื่อมีการเปลี่ยนแปลงสถานะ
if (pb3CurrentState == HIGH) { // ถ้าเป็นสถานะ HIGH (เมื่อมีการกดปุ่ม)
mode = 0; //set zero of weight scale
lcd.clear();
pb1PreviousState = LOW;
pb2PreviousState = LOW;
pb3PreviousState = LOW;
}
}
pb1PreviousState = pb1CurrentState; // อัปเดตสถานะก่อนหน้า
pb2PreviousState = pb2CurrentState;
pb3PreviousState = pb3CurrentState;
delay(100);
} //mode1
if (mode == 2) { //operation mode
weight = (scale.get_units(5))*1;
weight = weight; //in gram
lcd.setCursor(0,0);
lcd.print("In process");
lcd.setCursor(0,1);
lcd.print(weight);
lcd.setCursor(8,1);
lcd.print("g");
weightError = weightAlarm-weight;
if (weightError>0) {
digitalWrite(alarmPin,HIGH); //on red led >> inprocess
digitalWrite(relayPin,HIGH); //on relay
}
else {
digitalWrite(alarmPin,LOW); //off red led >> finish
digitalWrite(relayPin,LOW); //off relay
mode = 3;
Serial.println("Finish");
weightMax = weight;
Serial.println(weightMax);
}
} //mode2
if (mode == 3) { //send data
weight = (scale.get_units(5))*1;
weight = weight; //in gram
lcd.setCursor(0,0);
lcd.print("wait for send");
lcd.setCursor(0,1);
lcd.print(weight);
lcd.setCursor(8,1);
lcd.print("g");
if (weight-weightMax<-0.1) {
Serial.print("senddata: ");
Serial.println(weightMax);
mode = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("send data >>");
lcd.setCursor(0,1);
lcd.print(weightMax);
lcd.setCursor(8,1);
lcd.print("g");
delay(3000);
lcd.clear();
}
else {
weightMax = weight;
}
}//mode3
}//void