Cat automatic feeder update 2, I made it! IT WORKS!!!

I used an arduino nano with tiny RTC and servo.
First I only wanted to use arduino nano. But learned that without RTC if there is power outage poor cat have to wait longer to be fed. With RTC I can control the feeding time very accurate and with open valve time I can control to quantity of food per feeding.
Code probably looks like mess. But at least arduino gets it.

As I learned you never use an arduino to supply power to the servo. Can I use USB 5V adapter, and plug red wire 5V(arduino) and to the servo red. Where do I plug to black wire in arduino(GROUND?). I want use one adapter (phone charger adapter) to supply power to the Arduino and the servo without any accident.

Thanks!

#include <Wire.h>
#include <RTClib.h>
#include <Servo.h>
#include <DS1307RTC.h>


RTC_DS1307 rtc;//define a object of DS1307 class
Servo myservo;  // create servo object to control a servo

const int ONHour1 =   00;  // Define feeding time
const int ONMinute1 = 00;
const int ONSecond1 = 00;
const int ONHour2 =   06;
const int ONMinute2 = 00;
const int ONSecond2 = 00;
const int ONHour3 =   12;
const int ONMinute3 = 00;
const int ONSecond3 = 00;
const int ONHour4 =   18;
const int ONMinute4 = 00;
const int ONSecond4 = 00;

const int OpenValveTime = 1000; //valve open time

int CurrentHour;
int CurrentMinute;
int CurrentSecond;


void setup()
{
  Serial.begin(9600);
  rtc.begin();
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
void loop()
{


 DateTime now = rtc.now();
   // Save check in time;
    CurrentHour = now.hour();
    CurrentMinute = now.minute();
    CurrentSecond = now.second();

  
  if((CurrentHour==ONHour1) && (CurrentMinute == ONMinute1)&& (CurrentSecond == ONSecond1))
  {myservo.write(45);
  delay(500);
    {
  delay(OpenValveTime); //valve open time
  }
  myservo.write(135);
  delay(300);

  Serial.println("HAPPY CAT");
  }
  else {
    myservo.write(135);
  }
 if((CurrentHour==ONHour2) && (CurrentMinute == ONMinute2)&& (CurrentSecond == ONSecond2))
  {myservo.write(45);
  delay(500);
    {
  delay(OpenValveTime); //valve open time
  }
  myservo.write(135);
  delay(300);
  Serial.println("HAPPY CAT");
  
  }
  else {
    myservo.write(135);
  }
   if((CurrentHour==ONHour3) && (CurrentMinute == ONMinute3)&& (CurrentSecond == ONSecond3))
  {myservo.write(45);
  delay(500);
    {
  delay(OpenValveTime); //valve open time
  }
  myservo.write(135);
  delay(300);
  Serial.println("HAPPY CAT");
 
  }
  else {
    myservo.write(135);
  }
   if((CurrentHour==ONHour4) && (CurrentMinute == ONMinute4)&& (CurrentSecond == ONSecond4))
  {myservo.write(45);
  delay(500);
    {
  delay(OpenValveTime); //valve open time
  }
  myservo.write(135);
  delay(300);
  Serial.println("HAPPY CAT");
  }
  else {
    myservo.write(135);
  }
  
}

Nice.

If you hadn’t added the RTC I would have recommended that the solution to a power outage just be to feed the cat immediately after startup.

The risk is that the cat might figure out (!) that interrupting the power, however s/he managed, results in a food delivery…

Also in your time checks you test for exact matches. This is OK but relies on you checking the time within the second it becomes exact. It is safer to test for >= in the seconds.

And instead of dealing always with hours, minutes and seconds, you cou,d convert all times to seconds since whenever, like seconds since midnight, then all your comparisons are just one number against a number you calculate (or your program can, of course) as the right number of seconds since midnight to do a feeding cycle.

But it works, happy cat, so maybe on to the next thing the cat needs a little project solution for. :wink:

a7

Congratulations, the best part of a project is when it is finished and working as wanted.

Congratulations !


Make sure there aren’t any software bugs ;).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.