#include <HX711_ADC.h>
#include <EEPROM.h>
#include <LiquidCrystal.h>
#include <Servo.h>
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin
#define SERV 9
const int calVal_ea=0 ;
unsigned long t=0;
Servo myServo;
float mass =0;
float targetMass;
float diff;
HX711_ADC LoadCell(HX711_dout,HX711_sck);
#define LCD_RS 22
#define LCD_E 24
#define LCD_D4 26
#define LCD_D5 28
#define LCD_D6 30
#define LCD_D7 32
LiquidCrystal lcd(LCD_RS,LCD_E,LCD_D4,LCD_D5,LCD_D6,LCD_D7);
#define BUTMOD 50
#define BUTSET 48
#define BUTSTOP 46
int mode=0;
int butsetmode=0;
int butstopmode=1;
void setup()
{
//EEPROM.put(0,0.42);
Serial.begin(9600);
LoadCell.begin();
float calVal;
//EEPROM.get(calVal_ea,calVal);
calVal=1934.96;// for irl
calVal=0.42;// for sim
unsigned long stableT =2000;
boolean _tare = 1;
LoadCell.start(stableT,_tare);
LoadCell.setCalFactor(calVal);
myServo.attach(SERV);
lcd.begin(16,2);
lcd.clear();
}
void loop()
{
if(butstopmode)
{setmass();}
getMass();
if(digitalRead(BUTSET))
{setbutop();}
if(digitalRead(BUTSTOP))
{stopbutop();}
}
void getMass()
{
boolean newData =0;
const int prDelay=100; //reduce to increase speed of collecting mass
if(LoadCell.update()) newData=1;
if(newData)
{
if(millis()> t+prDelay )
{
mass = LoadCell.getData();
Serial.print("weight : ");
lcd.setCursor(0,1);
lcd.print(mass);
Serial.println(mass);
newData=0;
t=millis();
}
}
}
void setmass()
{
if(digitalRead(BUTMOD))
{
mode++;
if(mode>2)mode=0;
}
switch (mode)
{
case 0: targetMass=250;
break;
case 1: targetMass=500;
break;
case 2: targetMass=750;
break;
default: targetMass=250;
}
lcd.setCursor(0,0);
lcd.print(targetMass);
Serial.print(" tm :");
Serial.println(targetMass);
}
void setbutop()
{
butstopmode=0;
butsetmode=1;
diff = targetMass-mass;
if(diff>0)
{
myServo.write(150);
}
else
myServo.write(180);
lcd.print(" ");
}
void stopbutop()
{
butstopmode=1;
butsetmode=0;
myServo.write(180);
lcd.setCursor(4,0);
lcd.print("stop");
Serial.print(" stop ");
}