#include <HX711.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include <EEPROM.h>
#include <Servo.h>
Servo servoBarrier;
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 4);
HX711 weight;
#define LOADCELL_DOUT_PIN 12
#define LOADCELL_SCK_PIN 11
#define MAX_WEIGHT 3.5
#define LED_PIN 13
#define servoBarrierPin 3
#define ADDRES_PASSED 0
#define ADDRES_OVERWEIGHT 2
unsigned int countPassed = 0;
unsigned int countOverweight = 0;
float lastWeight = 0;
void setup() {
servoBarrier.attach(servoBarrierPin);
lcd.begin(16, 4);
lcd.init();
lcd.backlight();
pinMode(LED_PIN, OUTPUT);
weight.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
// У Wokwi немає можливості зберегти EEPROM між перезапусками
// Видалити наступні 2 рядки
EEPROM.put(ADDRES_OVERWEIGHT, 0);
EEPROM.put(ADDRES_PASSED, 0);
EEPROM.get(ADDRES_OVERWEIGHT, countOverweight);
EEPROM.get(ADDRES_PASSED, countPassed);
}
void loop() {
float current_weight = weight.get_units() / 420.000;
DateTime now = rtc.now();
if(lastWeight < current_weight) lastWeight = current_weight;
if(current_weight < 0.01 && lastWeight > 0.01){
if(lastWeight > MAX_WEIGHT){
countOverweight ++;
EEPROM.put(ADDRES_OVERWEIGHT, countOverweight);
} else{
countPassed ++;
EEPROM.put(ADDRES_PASSED, countPassed);
}
lastWeight = 0.01;
}
lcdPrint(0, 0, "Weight kg: " + String(current_weight));
lcdPrint(0, 1, "Trucks passed: " + String(countPassed));
lcdPrint(0, 2, "Overweight : " + String(countOverweight));
lcdPrint(0, 3, "Time: " +String(now.hour())+':'+
String(now.minute())+':'+String(now.second()));
if(current_weight > MAX_WEIGHT){
digitalWrite(LED_PIN, 1);
lcdPrint(16, 0, "OVER");
servoBarrier.write(90);
} else{
digitalWrite(LED_PIN, 0);
lcdPrint(16, 0, " ");
servoBarrier.write(0);
}
delay(500);
}
void lcdPrint(byte x, byte y, String str){
lcd.setCursor(x, y); lcd.print(str);
}