#include <Wire.h> //library I2C
#include <LiquidCrystal_I2C.h> //library LCD
#include <HX711.h>
#include <Servo.h>
#include "PID_v1.h"
#define DataOut 3
#define DataSCK 2
#define ServoPin 11
#define SetPoint A0
HX711 scale;
float calibrationFactor = 420;
float unit;
double setpoint, input, output;
double Kp = 2, Ki = 0, Kd = 0;
Servo srv;
PID pidcs(&input, &output, &setpoint, Kp, Ki, Kd, P_ON_E, DIRECT);
LiquidCrystal_I2C lcd(0x27,16,2); // set address I2C dan besar karakter untuk lcd 16×2
void setup()
{
pinMode(A0,INPUT);
Serial.begin(9600);
lcd.begin(16,2);
lcd.init(); // inisiasi LCD
lcd.backlight();//menyalakan backlight
scale.begin(DataOut, DataSCK);
scale.set_scale(calibrationFactor);
scale.tare();
srv.attach(ServoPin);
pidcs.SetOutputLimits(-4, 255); // define limit of pid
pidcs.SetMode(AUTOMATIC); // Nyalakan PID
unit=map(scale.get_units(),-244.66,-249.66,255,0);
input=map(scale.get_units(),-244.66,-249.66,255,0);
lcd.setCursor(0,0);
lcd.print("Filling Machine");
delay(200);
lcd.clear();
}
void report(void)
{
static uint32_t last = 0;
const int interval = 1000; // interval proses print
if (millis() - last > interval) {
last += interval;
lcd.setCursor(0,0);
lcd.print("SP:");
Serial.print("SP:");
lcd.setCursor(4,0);
lcd.print(map(setpoint,0,255,1,5));
Serial.println(map(setpoint,0,255,1,5));
lcd.setCursor(0,1);
lcd.print("W :");
Serial.print("W :");
lcd.setCursor(4,1);
lcd.print(map(unit,0,255,1,5));
Serial.println(input);
}
}
void loop()
{
unit=map(scale.get_units(),-244.66,-249.66,255,0);
input=map(scale.get_units(),-244.66,-249.66,255,0);
if (pidcs.Compute()) {
int sudut = map((int) output, 0, 255, -4, 180);
srv.write(sudut);
Serial.println(sudut);
setpoint = map(analogRead(A0), 0, 1023, 0, 255);
report();
delay(1000);
lcd.clear();
}
}
1 Kg
Setpoint
5 Kg