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
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.substring(0, 9).equals(feedTime.substring(0, 9)));
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);
}
}
}

