//Fish Feeder
//By _Hash_
//Function: Feeds my Fish at a regular interval of 2hrs and also if my Fish swims very close to Feeding spot (cuz its hungry).
//Add.Function: Also Outputs the exact time it is fed to act as a record/log (with exact time & date)...
//Workings: Feeding spot detected by PIR Motion Sensor & Feeding time is 5 second by Servo Mortor....
// When you start simul it feeds the fish for 5 seconds and then output apears again
#include <Wire.h>
#include <Servo.h>
#include <RTClib.h> // Include RTC library
// Define pins
const int servoPin = 9;
const int pirSensorPin = 8;
const int RTC_ADDR = 0x68; // I2C address for RTC module
// Define variables
Servo myservo; // Create servo object
int motionDetected = 0;
int feedingInterval = 2 * 60 * 60; // Set feeding interval in seconds (2 hours)
unsigned long lastFeedingTime = 0; // Stores timestamp of the last feeding
RTC_DS1307 RTC; // Create RTC object
void setup() {
Serial.begin(9600); // Initialize serial communication
myservo.attach(servoPin); // Attach servo motor to pin 9
pinMode(pirSensorPin, INPUT); // Set PIR sensor pin as input
// Initialize RTC
if (!RTC.begin()) {
Serial.println("RTC not found.");
while (1); // Halt if RTC not found
}
}
void loop() {
motionDetected = digitalRead(pirSensorPin); // Read PIR sensor value
dispenseFood();
delay(1000); // Delay for 1 second
}
void readTime() {
DateTime now = RTC.now(); // Get current date and time
Serial.print("Current time: ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.println(now.second());
}
void dispenseFood() {
DateTime now = RTC.now(); // Get current date and time
unsigned long currentTime = now.unixtime(); // Get timestamp from RTC in seconds
// Check if feeding interval has passed and motion is detected
if ((currentTime - lastFeedingTime >= feedingInterval) || motionDetected == HIGH) {
myservo.write(90); // Open food dispenser
delay(5000); // Dispensing food
myservo.write(0); // Close food dispenser
lastFeedingTime = currentTime; // Update last feeding time
Serial.println("Fish fed at:");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.print(now.second());
Serial.print(" ");
Serial.print(now.day());
Serial.print("/");
Serial.print(now.month());
Serial.print("/");
Serial.print(now.year());
Serial.println(" for 5s");
}
}