Turn servo when given time and rtc matches

Hello, I am an absolute beginner at Arduino. My main goal is to build an automatic pet feeder using RTC, 4x4 keypad, and a servo motor. I am trying to write the code so that it turns the servo when the given time and rtc time matches (The input is given with the 4x4 keypad). However, whatever I use to compare the strings coming from 4x4 keypad input and rtc time, it is not comparing them, whatever I write inside loop function it does not run. What am I missing? Please help because I am out of ideas.

Here is the code:

#include <Keypad.h>
#include <DS1302.h>
#include <Servo.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns

//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'A', '3', '2', '1'},
  {'B', '6', '5', '4'},
  {'C', '9', '8', '7'},
  {'D', '#', '0', ':'}

byte rowPins[ROWS] = {10, 11, 12, 13}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad
char key;
String feedTime;
boolean feed = true; // condition for alarm
char t1, t2, t3, t4, t5, t6;

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// Init the DS1302
DS1302 rtc(4, 3, 5);
Servo servo_test;      //initialize a servo object for the connected servo
int c = 0;
void setup() {

  servo_test.attach(2);   // attach the signal pin of servo to pin9 of arduino
  // Set the clock to run-mode, and disable the write protection
  feedTime = "";
  // 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(22, 15, 00);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(11, 9, 2021);   // Set the date to August 6th, 2010

void loop() {

  // Send Day-of-Week
  //Serial.print(" ");

  // Send date
  //Serial.print(" -- ");

  // Send time

  // Wait one second before repeating :)
  char customKey = customKeypad.getKey();
  if (customKey == '#') {

  String t = "";
  t = rtc.getTimeStr();

  if ( t.equals(feedTime) && feed == true)
    servo_test.write(100);                   //command to rotate the servo to the specified angle
    Serial.println("IT WORKS!!!!");
    feed = false;
  Serial.print(t.substring(0, 9).equals(feedTime.substring(0, 9)));

void setFeedingTime()

  feedTime = "";
  Serial.println("\nPlease enter feeding time: ");
  while (1) {
    key = customKeypad.getKey();

    if (key == '#')
      key = 0;
      Serial.println("\nFeeding time set to: ");
      feed = true;


    else {


could you post the output of your code?


I was trying to print rtc timer and the given time. Although the rtc time is 22:15:20 and feedTime variable is 22:15:20 .equals returns 0.

code is comparing t to feed.
can you print the complete strings, not substrings?


This still returns 0 even when the t = 22:15:20 feedTime = 22:15:20 is printed see below:

please show me the output

The output is on the second answers' screenshot. I printed t and feedTime with '-' character in between. I think that's what you mean with complete strings. Sorry if I misunderstood completely.

Try changing the substring to 8 characters instead of 9.

  Serial.print(t.substring(0, 9).equals(feedTime.substring(0, 9)));

Serial.print(t.substring(0, 8).equals(feedTime.substring(0, 8)));

Returns 0, it returns 0 even if I compare the complete strings. I print them and they seem to be equal (both 22:15:15 for example), however, the .equals function does not return anything other than 0. I input the feedTime by 4x4 keypad on serial monitor.

do you expect me to answer your questions when you don't answer mine? ??

22:15:20 - 22:15:20.

String t = "22:15:20";
String q = "22:15:20";
bool   feed = true;

void setup() {
    Serial.begin (9600);

    Serial.print (t);
    Serial.print (" - ");
    Serial.print (t);
    Serial.println (".");

void loop() {
    if (t.equals (q) && feed == true)  {
        Serial.println ("works");
        feed = false;

Does this sit and wait for a key press?

Yes. In the loop function it waits for the keypress and if the pressed key is '#' the function is called. In the meantime the rtc clock is ticking.

It's great that the RTC is ticking. But, are you reading it?

I have answered all of your questions so far, is there a problem? Although your code seems to work correctly, in mine it does not return true in Serial.print(t.equals(feedTime)). I sent you the screenshots of complete strings. They are literally printed the same 22:15:20 however the function returns 0.

good luck

t = rtc.getTimeStr(); called in loop and t holds the string for it every second. My problem is that even if it's updated when t gets equal with the entered time it doesn't match for some reason.

In my screen my screenshot seems to be attached to my answer. Either there is something I do while uploading the ss is wrong or you do not get my answers. Anyways, thanks for trying to help :slight_smile:

You showed the output of the substrings but not the complete strings.

What @gcjr observed is that you test your strings to see if they are equal, but what you print out are substrings of those strings. I don't think that matters because even the equals of those substrings don't match, but it's silly to test one thing and print another.

Also, you've changed your code somewhat and not posted the new version, so we're flying blind now.