#include <HX711.h>
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_NeoPixel.h>
#define PIN 25
#define NUM_LEDS 16
int angle = 0;
const int trig = 12;
const int echo = 13;
long duration;
int distance;
const int pir = 23;
bool status =0;
int servoPin = 8;
const int LOADCELL_DOUT_PIN = 10;
const int LOADCELL_SCK_PIN = 11;
float weight_kg;
int per;
int forc=1;
HX711 scale;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servo;
Adafruit_NeoPixel strip(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
lcd.init();
lcd.backlight();
servo.attach(servoPin);
Serial.begin(9600);
strip.begin();
strip.show();
}
void loop()
{
presence();
HCSR();
HX();
if (weight_kg>=40||per>=95 ) {
forc= 0;
colorWipe(strip.Color(255, 0, 0), 50);
lcd.setCursor(0, 1);
lcd.print("THE BIN IS FULL");
delay((5000));
lcd.setCursor(0,1);
lcd.print(" ");
}
else {
forc= 1;
}
}
void HCSR() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = duration * 0.034 / 2;
per=(distance*100)/160;
if (weight_kg<=40&&per<=94){
colorWipe(strip.Color(0,255, 0), 50);
lcd.setCursor(0, 1);
lcd.print(per);
lcd.print("% FULL");
delay((5000));
lcd.setCursor(0,1);
lcd.print(" ");
}
}
void HX() {
if (scale.is_ready()) {
Serial.print("Weight (kg): ");
float calibration_factor = 420; // Exemple de facteur de calibration
weight_kg = scale.get_units() / calibration_factor;
Serial.println(weight_kg, 2); // Afficher le poids en kg avec 2 décimales
} else {
Serial.println("HX711 not found.");
}
delay(1000);
}
void presence() {
status = digitalRead(pir);
if(status== HIGH&&forc==1) {
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(5, 0);
lcd.print("OPNINIG!");
servo.write(angle);
}
else{
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(5, 0);
lcd.print("CLOSED!");
servo.write(180);
}
}
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, color);
strip.show();
delay(wait);
}
}