I am trying to make a auto fish feeder to feed my fish every 6hours. I want to implement RTC module to my code. So far I've been using only an arduino and a servo using this as a code:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
      // Wait for 12h
    delay(6 * 3600000);

The RTC module I have is the DS1302. It's library cn be downloaded here The code for the clock is this:

// DS1302_Serial_Hard 
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
// web: http://www.RinkyDinkElectronics.com/
// A quick demo of how to use my DS1302-library to 
// retrieve time- and date-date for you to manipulate.
// I assume you know how to connect the DS1302.
// DS1302:  CE pin    -> Arduino Digital 2
//          I/O pin   -> Arduino Digital 3
//          SCLK pin  -> Arduino Digital 4

#include <DS1302.h>

// Init the DS1302
DS1302 rtc(2, 3, 4);

// Init a Time-data structure
Time t;

void setup()
  // Set the clock to run-mode, and disable the write protection
  // Setup Serial connection

  // The following lines can be commented out to use the values already stored in the DS1302
  rtc.setDOW(FRIDAY);        // Set Day-of-Week to FRIDAY
  rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(6, 8, 2010);   // Set the date to August 6th, 2010

void loop()
  // Get data from the DS1302
  t = rtc.getTime();
  // Send date over serial connection
  Serial.print("Today is the ");
  Serial.print(t.date, DEC);
  Serial.print(". day of ");
  Serial.print(" in the year ");
  Serial.print(t.year, DEC);
  // Send Day-of-Week and time
  Serial.print("It is the ");
  Serial.print(t.dow, DEC);
  Serial.print(". day of the week (counting monday as the 1th), and it has passed ");
  Serial.print(t.hour, DEC);
  Serial.print(" hour(s), ");
  Serial.print(t.min, DEC);
  Serial.print(" minute(s) and ");
  Serial.print(t.sec, DEC);
  Serial.println(" second(s) since midnight.");

  // Send a divider for readability
  Serial.println("  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -");
  // Wait one second before repeating :)
  delay (1000);

So the question here is, how can I set up my code to work with RTC, in order to move the servo every 6 hours?

  • 1
    if you can print the time, then you can operate a servo when the time is equal to a predetermined value ... it is unclear what is giving you trouble
    – jsotola
    Sep 13, 2020 at 2:37
  • 1
    I have trouble coding, to be honest. Do you think you could help me with that? Sep 13, 2020 at 2:39
  • 1
    Thanks for your help, I'll try to figure it out with your tips... Sep 13, 2020 at 2:44
  • 2
    stary by doing the simplest modification to existing code ... printing a message when second is at particular value teaches you how to run a command when a variable contains a specific value ... choosing seconds means that you do not have to wait very long for results ... next, you add code that moves a servo ... that will also move every 60 seconds ... when it works correctly, then change the watched variable to hours
    – jsotola
    Sep 13, 2020 at 2:52
  • 1
    This is my first time trying to code, those codes you see are found open source hahaha... What you said, sounds hard for me. But I guess this would be my best shot if no one else shows up to help me out of that hahaha. Thanks buddy. Sep 13, 2020 at 3:07

1 Answer 1


You have to spend some time and practice coding, it does not come over night. As far as your timing look up for an alarm clock sketch that will do what you want. Once you understand that you should only have to add a few lines of code. As far as open source the tools you are using are open source. Remember there are people out there that are expertes and there people just starting so expect what you find to fall some in that range of quality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.