#include <IRremote.h>
#include <HX711.h>
#define LOADCELL_DOUT2_PIN 5
#define LOADCELL_SCK2_PIN 18
#define IR_SENSOR_PIN1 12
#define LOADCELL_DOUT1_PIN 21
#define LOADCELL_SCK1_PIN 22
#define IR_SENSOR_PIN2 13
IRrecv receiver1(IR_SENSOR_PIN1);
IRrecv receiver2(IR_SENSOR_PIN2);
HX711 scale1;
HX711 scale2;
int irValue1 = 0; // Global variable for IR sensor 1
int irValue2 = 0; // Global variable for IR sensor 2
unsigned long previousMillis = 0; // Stores last time readings were updated
const long interval = 3000; // Interval at which to read sensors (milliseconds)
void setup() {
Serial.begin(115200);
// Setup Load Cells
scale1.begin(LOADCELL_DOUT1_PIN, LOADCELL_SCK1_PIN);
scale2.begin(LOADCELL_DOUT2_PIN, LOADCELL_SCK2_PIN);
// Set the calibration factor once in setup
scale1.set_scale(420.00); // Adjust this value based on your calibration
scale2.set_scale(420.00); // Adjust this value based on your calibration
// Setup Reflective IR Sensors
pinMode(IR_SENSOR_PIN1, INPUT);
pinMode(IR_SENSOR_PIN2, INPUT);
// Setup the receivers
receiver1.enableIRIn();
receiver2.enableIRIn();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Declare variables to hold weight readings
float weight1 = 0;
float weight2 = 0;
// Read Load Cell 1
if (scale1.is_ready()) {
weight1 = scale1.get_units(10);
Serial.print("Weight1: ");
Serial.print(weight1, 2);
Serial.println(" kg");
} else {
Serial.println("HX711-1 not found.");
}
// Read Load Cell 2
if (scale2.is_ready()) {
weight2 = scale2.get_units(10);
Serial.print("Weight2: ");
Serial.print(weight2, 2);
Serial.println(" kg");
} else {
Serial.println("HX711-2 not found.");
}
// Read Reflective IR Sensor 1
irValue1 = digitalRead(IR_SENSOR_PIN1); // Read value in each loop iteration
Serial.print("IR Sensor 1: ");
Serial.println(irValue1 == HIGH ? "Object detected" : "No object");
// Read Reflective IR Sensor 2
irValue2 = digitalRead(IR_SENSOR_PIN2); // Read value in each loop iteration
Serial.print("IR Sensor 2: ");
Serial.println(irValue2 == HIGH ? "Object detected" : "No object");
// Categorize based on sensor readings
categorizeSeat(irValue1, weight1, "Seat 1");
categorizeSeat(irValue2, weight2, "Seat 2");
Serial.println(); // Blank line for spacing
}
// Check for received IR signals
if (receiver1.decode()) {
translateIR(receiver1, irValue1);
receiver1.resume(); // Receive the next value
}
if (receiver2.decode()) {
translateIR(receiver2, irValue2);
receiver2.resume(); // Receive the next value
}
}
void translateIR(IRrecv &receiver, int &irValue) {
if (receiver.decodedIRData.command == 104) {
irValue = 1;
} else {
irValue = 0;
}
}
void categorizeSeat(int irValue, float weight, const char* seatName) {
// Define the weight thresholds for categorization
float passengerThreshold = 5.95; // Weight threshold for passenger
float childThreshold = 1.0; // Weight threshold for child
if (irValue == HIGH) {
if (weight > passengerThreshold) {
Serial.print(seatName);
Serial.println(": Kursi Terisi Penumpang");
} else if (weight <= passengerThreshold && weight > childThreshold) {
Serial.print(seatName);
Serial.println(": Kursi Terisi Balita");
} else if (weight <= childThreshold && weight > 0) {
Serial.print(seatName);
Serial.println(": Kursi Terisi Barang");
} else if (weight == 0) {
Serial.print(seatName);
Serial.println(": Kursi Kosong");
}
} else {
if (weight <= passengerThreshold && weight > childThreshold) {
Serial.print(seatName);
Serial.println(": Kursi Terisi Balita");
} else if (weight <= childThreshold && weight > 0) {
Serial.print(seatName);
Serial.println(": Kursi Terisi Barang");
} else if (weight == 0) {
Serial.print(seatName);
Serial.println(": Kursi Kosong");
}
}
}