/*
working 1
-------------------------------------------------------------------------------------
HX711_ADC
Arduino library for HX711 24-Bit Analog-to-Digital Converter for Weight Scales
Olav Kallhovd sept2017
-------------------------------------------------------------------------------------
*/
/*
Settling time (number of samples) and data filtering can be adjusted in the config.h file
For calibration and storing the calibration value in eeprom, see example file "Calibration.ino"
The update() function checks for new data and starts the next conversion. In order to acheive maximum effective
sample rate, update() should be called at least as often as the HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS.
If you have other time consuming code running (i.e. a graphical LCD), consider calling update() from an interrupt routine,
see example file "Read_1x_load_cell_interrupt_driven.ino".
This is an example sketch on how to use this library
*/
#include <ThreeWire.h>
#include <RtcDS1302.h>
#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif
#include <Servo.h>
Servo servoBase;
//pins:
const int HX711_dout = 2; //mcu > HX711 dout pin
const int HX711_sck = 3; //mcu > HX711 sck pin
//const int forwardPin = 8;
//const int backwardPin = 12;
//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);
const int calVal_eepromAdress = 0;
unsigned long t = 0;
const int IO = 3; // DAT
const int SCLK = 5; // CLK
const int CE = 2; // RST
const int releaseAmt = 200; //set here the amount to release in grams
ThreeWire myWire(6, 5, 10); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
float prevLoad = 0.00;
void setup() {
//pinMode(forwardPin, OUTPUT);
//pinMode(backwardPin, OUTPUT);
//RGB LIGHT
//pinMode(A1, OUTPUT);
//pinMode(A2, OUTPUT);
//pinMode(A3, OUTPUT);
//Servi pins
servoBase.attach(A0);
servoBase.write(190);
Serial.begin(57600); delay(10);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
// printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid()) {
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (Rtc.GetIsWriteProtected()) {
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning()) {
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled) {
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
} else if (now > compiled) {
Serial.println("RTC is newer than compile time. (this is expected)");
} else if (now == compiled) {
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
LoadCell.begin();
//LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
float calibrationValue; // calibration value (see example file "Calibration.ino")
calibrationValue = 52.08;//38.87;//143.23; //368.12; // uncomment this if you want to set the calibration value in the sketch
unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
LoadCell.start(stabilizingtime, _tare);
LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
}
void loop() {
//servoBase.write(120);
//delay(5000);
//servoBase.write(180);
//delay(5000);
static boolean newDataReady = 0;
const int serialPrintInterval = 0; //increase value to slow down serial print activity
RtcDateTime now = Rtc.GetDateTime();
LoadCell.update();
float currentLoad = LoadCell.getData();
float loadinKG = currentLoad / 1000;
Serial.print("Weight[kg]: ");
Serial.println(currentLoad);
if (currentLoad > 0){
if (!(prevLoad == loadinKG)){
Serial.print("Weight[kg]: ");
Serial.println(loadinKG);
prevLoad = loadinKG;
}
//light indication
//if(loadinKG <= 0.10){ //100g
//red Low level
//analogWrite(A1, 255);
//analogWrite(A2, 0);
//analogWrite(A3, 0);
//}
//else{
//green
//analogWrite(A1, 0);
//analogWrite(A2, 255);
//analogWrite(A3, 0);
//}
}
//check current time. FOR Debuging ONLY
Serial.print(now.Hour());
Serial.print(":");
Serial.print(now.Minute());
Serial.print(":");
Serial.print(now.Second());
Serial.println();
//
//this part you can set the time
if ((now.Hour() == 10 && now.Minute() == 14 && now.Second() == 0) || (now.Hour() == 17 && now.Minute() == 00 && now.Second() == 0)) {
if (currentLoad > releaseAmt){
float newLoad = currentLoad - releaseAmt;
boolean _resume = false;
while(_resume == false){
// check for new data/start next conversion:
if (LoadCell.update()) newDataReady = true;
// get smoothed value from the dataset:
if (newDataReady) {
if (millis() > t + serialPrintInterval) {
float i = LoadCell.getData();
if (i > newLoad){ //current load is greater than supposedly load
if (i > 0){ //did not accept negative value
Serial.print("Releasing... Weight[kg]: ");
Serial.println(i/1000);
//start spinning
//digitalWrite(forwardPin, HIGH);
//digitalWrite(backwardPin, LOW);
servoBase.write(120);
}
}
else{
///stop spining
//digitalWrite(forwardPin, HIGH);
//digitalWrite(backwardPin, HIGH);
servoBase.write(180);
_resume = true;
}
newDataReady = 0;
t = millis();
}
}
}
}
else{
//print low level
}
}
delay(1000); // 1 second
}