// This pet feeder timing dispense food at 0 hours (startup), next is 5 hours,
// then 5 hours then 14 hours, total 24 hours. Suggestion to turn on the machine
// at 8am, next feed is 1pm, and 5pm after that, then wait. Next feeding is 8am next
// morning.
// Trial time is 0 second start up, 5 second feed 1, 5 seconds feed 2 and
// wait for 14 seconds
#include <Servo.h>
Servo feederServo;
const int servoPin = 9;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
const unsigned long servoRunTime = 1000; // 1 second rotation
const unsigned long fiveSeconds = 5000; // 5 second rotation
const unsigned long fourteenSeconds = 14000; // 14 second rotation
const unsigned long fiveHours = 18000000; // 5 hours in milliseconds
const unsigned long nineHours = 32400000; // 9 hours in milliseconds
int state = 0; // To track the sequence of operations
void setup() {
Serial.begin(9600); // Start serial communication
feederServo.attach(servoPin);
feederServo.write(90); // Start at neutral position
previousMillis = millis(); // Initialize previousMillis at startup
}
void loop() {
currentMillis = millis();
switch (state) {
case 0: // Rotate the servo for 1 second at startup
if (currentMillis - previousMillis < servoRunTime) {
feederServo.write(60); // Rotate servo (adjust angle for your servo)
//Serial.println("Dispensing 8am"); // Print "Dispensing" when servo rotates
} else {
feederServo.write(90); // Stop rotation
previousMillis = currentMillis;
//Serial.println("Next dispense 1pm"); // Print "Waiting" after servo rotation
state = 1; // Move to next state
}
break;
case 1: // Pause for 5 hours
if (currentMillis - previousMillis >= fiveSeconds) {
previousMillis = currentMillis;
state = 2; // Move to next state
}
break;
case 2: // Rotate servo for 1 second
if (currentMillis - previousMillis < servoRunTime) {
feederServo.write(60); // Rotate servo
//Serial.println("Dispensing 1pm"); // Print "Dispensing" when servo rotates
} else {
feederServo.write(90); // Stop rotation
previousMillis = currentMillis;
//Serial.println("Next dispense 6pm"); // Print "Waiting" after servo rotation
state = 3; // Move to next state
}
break;
case 3: // Pause for 5 hours
if (currentMillis - previousMillis >= fiveSeconds) {
previousMillis = currentMillis;
state = 4; // Move to next state
}
break;
case 4: // Rotate servo for 1 second
if (currentMillis - previousMillis < servoRunTime) {
feederServo.write(60); // Rotate servo
//Serial.println("Dispensing 6pm"); // Print "Dispensing" when servo rotates
} else {
feederServo.write(90); // Stop rotation
previousMillis = currentMillis;
//Serial.println("Next dispense tomorrow 8am"); // Print "Waiting" after servo rotation
state = 5; // Move to next state
}
break;
case 5: // Pause for 14 hours / seconds
if (currentMillis - previousMillis >= fourteenSeconds) {
previousMillis = currentMillis;
state = 0; // Restart the cycle
}
break;
}
}