#include <LiquidCrystal.h>
#include <RTClib.h>
#include <Arduino_FreeRTOS.h>
#include "semphr.h"
#include <Servo.h>
#include "HX711.h"
//create handle for the mutex. It will be used to reference mutex
SemaphoreHandle_t mutex;
//LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
const int rs = 8, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
RTC_DS1307 rtc;
Servo pakan;
char dataHari[7][12] = {"Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"};
String hari;
int tanggal, bulan, tahun, jam, menit, detik;
float bacaLoad;
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 8;
const int LOADCELL_SCK_PIN = 9;
HX711 scale;
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
// RTC
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// Setting waktu sesuai waktu laptop
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Button
pinMode(10, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
// Buzzer
pinMode(12, OUTPUT);
//Servo
pakan.attach(11); //motor di pin 11
pakan.write(180); //posisi awal gerbang (derajat)
// LoadCell HX711
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(0.4186);
// Mutex
mutex = xSemaphoreCreateMutex();
// xTaskCreate(Gerbang, "Task2", 128, NULL, 1, NULL);
xTaskCreate(RTC, "Task1", 256, NULL, 2, NULL);
}
void RTC(void *param)
{
(void) param;
while (1)
{
xSemaphoreTake(mutex, portMAX_DELAY);
int menittrigger = 0;
for(;;)
{
// Baca RTC
DateTime now = rtc.now();
hari = dataHari[now.dayOfTheWeek()];
tanggal = now.day(), DEC;
bulan = now.month(), DEC;
tahun = now.year(), DEC;
jam = now.hour(), DEC;
menit = now.minute(), DEC;
detik = now.second(), DEC;
// Baca Button
int menitberjalan = menit;
uint8_t pushButton = 4;
pinMode(pushButton, INPUT_PULLUP);
int buttonState = digitalRead(pushButton);
uint8_t pushButton2 = 13;
pinMode(pushButton2, INPUT_PULLUP);
int buttonState2 = digitalRead(pushButton2);
// Baca Loadcell
bacaLoad = scale.get_units(5);
if(buttonState2 == 0){
xSemaphoreGive(mutex);
Load(param);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
if(buttonState == 1){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(hari+ "," + tanggal + "-" + bulan + "-" + tahun);
lcd.setCursor(1, 1);
lcd.print(String() + "Jam : " + jam + ":" + menit + ":" +detik);
if(bacaLoad < 500){
int selisih= menitberjalan-menittrigger;
if(selisih >= 2){
menittrigger = menit;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Pakan akan habis ");
digitalWrite(12, HIGH);
vTaskDelay(2000 / portTICK_PERIOD_MS);
digitalWrite(12, LOW);
}
}
if(jam == 14 & menit == 25 & detik == 30)
{
pakan.write(60);
vTaskDelay(3000 / portTICK_PERIOD_MS);
pakan.write(180);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
else if(jam == 16 & menit == 11 & detik == 20)
{
pakan.write(60);
vTaskDelay(3000 / portTICK_PERIOD_MS);
pakan.write(180);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
else{
xSemaphoreGive(mutex);
Gerbang(param);
}
}
}
}
void Gerbang(void *param)
{
(void) param;
xSemaphoreTake(mutex, portMAX_DELAY);
for(int i=0; i<4 ; i++){
lcd.clear();
lcd.setCursor(1,0);
lcd.print("SELAMAT MAKAN");
lcd.setCursor(2,1);
lcd.print("KUCINGGG...");
pakan.write(60);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
pakan.write(180);
xSemaphoreGive(mutex);
RTC(param);
}
void Load(void *param)
{
(void) param;
xSemaphoreTake(mutex, portMAX_DELAY);
for(int i=0; i<2 ; i++){
bacaLoad = scale.get_units(5);
lcd.clear();
lcd.setCursor (1,0);
lcd.print("Menghitung");
lcd.setCursor (1,1);
lcd.print("pakan...");
vTaskDelay(1000/portTICK_PERIOD_MS);
}
lcd.clear();
lcd.setCursor (1,0);
lcd.print("Sisa pakan anda : ");
lcd.setCursor (6,1);
lcd.print(String() + bacaLoad + " gr");
vTaskDelay(3000/portTICK_PERIOD_MS);
xSemaphoreGive(mutex);
RTC(param);
}
void loop() {}