#include <Stepper.h>
#include <Wire.h>
#include <RTClib.h>
#define STEPS 200 // Change this value according to your stepper motor
#define STEPPER_PIN1 8 // Define the pins connected to the stepper motor
#define STEPPER_PIN2 9
#define STEPPER_PIN3 10
#define STEPPER_PIN4 11
#define FEED_BUTTON_PIN 2 // Pin connected to the feed button
Stepper stepper(STEPS, STEPPER_PIN1, STEPPER_PIN2, STEPPER_PIN3, STEPPER_PIN4);
RTC_DS3231 rtc;
const int feed1Hour = 11; // First feeding time (hour)
const int feed1Minute = 30; // First feeding time (minute)
const int feed2Hour = 11; // Second feeding time (hour)
const int feed2Minute = 32;// Second feeding time (minute)
volatile bool feedingEnabled = false; // Flag to enable feeding
void setup() {
Serial.begin(9600);
Wire.begin();
rtc.begin();
stepper.setSpeed(100); // Set the speed of the stepper motor in RPM (adjust as needed)
pinMode(FEED_BUTTON_PIN, INPUT_PULLUP); // Set feed button pin as input with internal pull-up resistor
}
void loop() {
DateTime now = rtc.now();
// Check if it's time for feeding
if ((now.hour() == feed1Hour && now.minute() == feed1Minute) ||
(now.hour() == feed2Hour && now.minute() == feed2Minute) ||
(digitalRead(FEED_BUTTON_PIN) == LOW))
{
dispenseFood();
}
if (feedingEnabled) {
dispenseFood();
}
// Add a delay to conserve CPU cycles
delay(1000); // 1-second delay
}
void dispenseFood() {
// Rotate stepper motor to dispense food
// You may need to adjust the number of steps and direction based on your setup
stepper.step(800); // Rotate 100 steps (adjust as needed)
Serial.println("Food dispensed!");
}
void enableFeeding() {
feedingEnabled = true; // Set flag to enable feeding when button is pressed
}