#include "HX711.h" // HX711 circuit wiring
#include <Wire.h> // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include "RTClib.h"
DS1307 rtc; //initialize Real Time Clock (RTC) object
HX711 scale1; //initialize the first Load Cell object
HX711 scale2; //initialize the second Load Cell object
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
long bridge_capacity = 10; //KG
const int Green = 7;
const int Red = 6;
int Vehicle_Counter = 0;
long total = 0;
const int LOADCELL_DOUT_PIN1 = 2; //for sensor 1
const int LOADCELL_SCK_PIN1 = 3; //for sensor 1
const int LOADCELL_DOUT_PIN2 = 4; //for sensor 2
const int LOADCELL_SCK_PIN2 = 5; //for sensor 2
void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
/////////////
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // This line sets the RTC with an explicit date & time
}
//////////////
pinMode(Green, OUTPUT);
pinMode(Red, OUTPUT);
scale1.begin(LOADCELL_DOUT_PIN1, LOADCELL_SCK_PIN1);
scale2.begin(LOADCELL_DOUT_PIN2, LOADCELL_SCK_PIN2);
scale1.tare(); // reset the weight to zero
scale2.tare(); // reset the weight to zero
digitalWrite(Green, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
if (scale1.is_ready() | scale2.is_ready()) {
long reading1 = (scale1.read_average(20)/420); //read the average of 20 readings of weight from scale 1 in KGs
long reading2 = (scale2.read_average(20)/420); //read the average of 20 readings of weight from scale 2 in KGs
Serial.print("Weight on Scale 1 in KGs is: ");
Serial.println(reading1);
if(reading2 > 0){
Vehicle_Counter= Vehicle_Counter+1; //if a vehicle crosses the bridge, the counter will increase
}
Serial.print("Weight on Scale 2 in KGs is: ");
Serial.println(reading2);
Serial.print("Number of vehicles that crossed the bridge is: ");
Serial.println(Vehicle_Counter);
total = total + (reading1);
scale1.tare();
total = total - (reading2);
scale2.tare();
if (total < 0){ //to prevent negative weight
total = 0;}
Serial.print("Total Weight is: ");
Serial.println(total); //print the total weight
DateTime now = rtc.now();
Serial.print("The date and time is: ");
int year = now.year();
int month = now.month();
int day = now.day();
int hour = now.hour();
int minute = now.minute();
Serial.print(year, DEC);
Serial.print('/');
Serial.print(month, DEC);
Serial.print('/');
Serial.print(day, DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfWeek()]);
Serial.print(") ");
Serial.print(hour, DEC);
Serial.print(':');
Serial.print(minute, DEC);
Serial.print(':');
Serial.print('/');
FILE* fp = fopen("file_path", "a+");
if (!fp) {
// Error in file opening
printf("Can't open file\n");
return 0;
}
// Saving data in file
fprintf(fp, "%d , %d, %d,%d,d%,d% \n", year,
month, day,hour,minute);
printf("\nNew Account added to record");
fclose(fp);
if(total >= (bridge_capacity-5)){
digitalWrite(Green, LOW);
//delay(5000);
digitalWrite(Red, HIGH);
}
else{
digitalWrite(Red, LOW);
//delay(5000);
digitalWrite(Green, HIGH);
}
}
else {
Serial.println("Weight sensor not found.");
}
delay(1000);
}