Turn servo when given time and rtc matches


This was the output for the complete strings. I showed both versions, substring and complete string. As you mention if even substrings do not match, there's no difference. The latest code is below. I have only removed the substring that's all. 22:15:20 for RTC and 22:15:20 for input from keypad. The 0 is the result for comparison.

#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
  rtc.halt(false);
  servo_test.write(55); 
  rtc.writeProtect(false);
  Serial.begin(9600);
  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(rtc.getDOWStr());
  //Serial.print(" ");
  
  // Send date
  //Serial.print(rtc.getTimeStr());
  //Serial.print(" -- ");

  // Send time
  
  
  // Wait one second before repeating :)
  char customKey = customKeypad.getKey();
  if (customKey == '#'){
        setFeedingTime();
                   
  }
  String t = "";
  t = rtc.getTimeStr();
 
   
  
      
  
   if ( t.equals(feedTime)&& feed==true)
 { 
   servo_test.write(100);                   //command to rotate the servo to the specified angle 
   delay(400);   
   servo_test.write(55); 
   Serial.println("IT WORKS!!!!");
   feed = false;
  } 
  Serial.print(t);
  Serial.print("-");
  Serial.print(feedTime);
  Serial.print("-");
  delay(50);
  Serial.print(t.equals(feedTime)); 
  Serial.println();
  delay(1000);
}


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


  if (key == '#')
 {
  key=0;
  Serial.println("\nFeeding time set to: ");
  Serial.print(feedTime);
  feed = true;
 
  break; }
  
  else{
    Serial.print(key);
    delay(50);
    feedTime.concat(key);
   
    
  }
  }
}

i tried simulating your code, without the servo and keyboard stuff and a simulated RTC class

22:15:16-22:15:20-0
22:15:17-22:15:20-0
22:15:18-22:15:20-0
22:15:19-22:15:20-0
IT WORKS!!!!
22:15:20-22:15:20-1
22:15:21-22:15:20-0
22:15:22-22:15:20-0
22:15:23-22:15:20-0
#define MyHW
#ifdef MyHW
class DS1302 {
  public:
    int hour;
    int min;
    int sec;

    DS1302 (int a, int b, int c)  {
        hour = 22;
        min  = 15;
        sec  = 10;
    }

    String getTimeStr (void)  {
        static char s [40];
        sprintf (s, "%02d:%02d:%02d", hour, min, sec++);
        return s;
    }
};

#else
#include <DS1302.h>
#endif


String  feedTime = "22:15:20";
boolean feed     = true; 

DS1302 rtc(4, 3, 5);

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

void loop(){
    String t = rtc.getTimeStr();
    if ( t.equals(feedTime)&& feed==true)
    {
        Serial.println("IT WORKS!!!!");
        feed = false;
    }

    Serial.print(t);
    Serial.print("-");
    Serial.print(feedTime);
    Serial.print("-");
    delay(50);
    Serial.print(t.equals(feedTime));
    Serial.println();
    delay(1000);
}

It's debugging time. I would write a function that takes a String and prints the length and then dump the string character by character as well as the ASCII value of each character so you can see in detail where the Strings differ, assuming that they do.

Maybe taking the keypad input creates the problem. Thanks for your help, I appreciate it.

Which library are you using for the RTC?
The DS1302 library I've seen returns a char* from getTimeStr, which points to a char array, but you are saving to a String so the compiler should be doing the conversion.

i don't think so. i think the logic of your code is correct.

i'm not sure about the values in the Strings.
perhaps even a hex dump of the Strings would reveal something. you could try initializing them at startup to rule out things, including the RTC String

Which library are you using for the DS1302?