Untitled

 avatar
unknown
plain_text
5 months ago
3.2 kB
3
Indexable
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HX711.h>
#include <RTClib.h>
#include <ESP32Servo.h>

// LCD I2C setup
LiquidCrystal_I2C lcd(0x27, 16, 2);

// HX711 setup
#define LOADCELL_DOUT_PIN 32
#define LOADCELL_SCK_PIN 33
HX711 scale;
float calibration_factor = -7050; // Sesuaikan dengan kalibrasi Anda

// RTC setup
RTC_DS1307 rtc;

// Servo setup
Servo feedServo;
#define SERVO_PIN 13

// Feeding times (adjust as needed)
const int FEEDING_TIMES = 3;
const int feedingHours[FEEDING_TIMES] = {7, 12, 17}; // 7 AM, 12 PM, 5 PM
const int feedingMinutes[FEEDING_TIMES] = {0, 0, 0}; // 0 minutes

// Feed amount
const float FEED_AMOUNT = 50.0; // grams per feeding

float remainingFeed = 1000.0; // Start with 1kg of feed
float dailyFeedAmount = FEED_AMOUNT * FEEDING_TIMES;

void setup() {
  Serial.begin(115200);
  
  // Initialize I2C
  Wire.begin(21, 22);  // SDA = GPIO 21, SCL = GPIO 22 for ESP32 DevKit 1
  
  // Initialize LCD
  lcd.init();
  lcd.backlight();
  
  // Initialize HX711
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(calibration_factor);
  scale.tare();
  
  // Initialize RTC
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  
  if (!rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set RTC to compilation time
  }
  
  // Initialize Servo
  ESP32PWM::allocateTimer(0);
  feedServo.setPeriodHertz(50);
  feedServo.attach(SERVO_PIN, 500, 2400);
  
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Fish Feeder");
  lcd.setCursor(0, 1);
  lcd.print("Initializing...");
  delay(2000);
}

void loop() {
  DateTime now = rtc.now();
  
  // Update remaining feed
  if (scale.is_ready()) {
    remainingFeed = scale.get_units();
  }
  
  // Display info on LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Feed: ");
  lcd.print(remainingFeed, 1);
  lcd.print("g");
  
  lcd.setCursor(0, 1);
  lcd.print("Next: ");
  lcd.print(dailyFeedAmount, 1);
  lcd.print("g");
  
  // Check if it's feeding time
  for (int i = 0; i < FEEDING_TIMES; i++) {
    if (now.hour() == feedingHours[i] && now.minute() == feedingMinutes[i] && now.second() == 0) {
      feedFish();
      break;
    }
  }
  
  // Print time to Serial for debugging
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
  
  delay(1000); // Wait for a second before next loop
}

void feedFish() {
  lcd.clear();
  lcd.print("Feeding fish...");
  
  // Open the feed valve (adjust angles as needed)
  feedServo.write(90); // Open position
  delay(1000); // Keep open for 1 second
  feedServo.write(0);  // Close position
  
  // Update remaining feed
  remainingFeed -= FEED_AMOUNT;
  if (remainingFeed < 0) remainingFeed = 0;
  
  // Update daily feed amount
  dailyFeedAmount -= FEED_AMOUNT;
  if (dailyFeedAmount < 0) dailyFeedAmount = 0;
  
  delay(3000); // Display feeding message for 3 seconds
}
Editor is loading...
Leave a Comment