#include <Wire.h> // LCD I2C
#include <LiquidCrystal_I2C.h> // LCD I2C
LiquidCrystal_I2C lcd(0x27,20,4); // LCD I2C
#include "HX711.h" //Loadcell
const int LOADCELL_DOUT_PIN = 16; //Loadcell
const int LOADCELL_SCK_PIN = 4; //Loadcell
HX711 scale;
//#include <TimeLib.h> //RTC I2C
#include "RTClib.h" //RTC I2C
RTC_DS1307 rtc; //RTC I2C
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
//millis
unsigned long timer_rtc = millis();
unsigned long timer_hx = millis();
//-----------------------------------------------------------------------------------------------------------//
void setup()
{
Wire.begin(); //I2C
Serial.begin(115200); //Serial
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
//GPIO begin
if (! rtc.begin()) { //RTC I2C
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
lcd.init(); // initialize the lcd
}
//-----------------------------------------------------------------------------------------------------------//
void loop()
{
DateTime now = rtc.now(); //RTC I2C
//Serial.print("Current time: ");
//Serial.print(now.year(), DEC);
//Serial.print('/');
//Serial.print(now.month(), DEC);
//Serial.print('/');
//Serial.print(now.day(), DEC);
//Serial.print(" (");
//Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
//Serial.print(") ");
//Serial.print(now.hour(), DEC);
//Serial.print(':');
//Serial.print(now.minute(), DEC);
//Serial.print(':');
//Serial.print(now.second(), DEC);
//Serial.println();
if (millis() - timer_rtc >=200){ //RTC
lcd.setCursor(0,0);
lcd.print(now.year(), DEC); //year ปี
lcd.setCursor(5,0);
lcd.print(now.month(), DEC); //month เดือน
lcd.setCursor(8,0);
lcd.print(now.day(), DEC); //day วัน
lcd.setCursor(0,1);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]); //week
lcd.setCursor(9,1);
lcd.print(now.hour(), DEC); //hour ชั่วโมง
lcd.setCursor(12,1);
lcd.print(now.minute(), DEC); //minute นาที
lcd.setCursor(15,1);
lcd.print(now.second(), DEC); //secound วินาที
timer_rtc +=200;
timer_rtc = millis();
}
if (millis() - timer_hx >=200){ //Loadcell
if (scale.is_ready()) {
long reading = scale.read();
Serial.print("HX711 reading: ");
Serial.println(reading);
} else {
Serial.println("HX711 not found."); //ไม่พบเซนเซอร์น้ำหนัก
}
timer_hx +=200;
timer_hx = millis();
}
}