Alarm clock project

Hello I need your help with my project.
my project has an option to set alarm clock. I’m using Arduino mega 2560, matrix keypad 4x4, lcd 16x2 and rtc ds1302.
My question is: how can I recive input from the keypad to set the alarm time?
this is my code:
Please ignore the other functions, help me after the state is 3 (after I’m pressing C).

#include <DS1302.h>
DS1302 rtc(11, 12, 13); // (CE, I/O, CLK)

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // (rs, e, d4, d5, d6, d7)

#include <Keypad.h>
char keys[4][4]={
 {'1','2','3','A'},
 {'4','5','6','B'},
 {'7','8','9','C'},
 {'*','0','#','D'}};
byte colPin[4]={38,39,40,41};
byte rowPin[4]={42,43,44,45};
Keypad keypad=Keypad(makeKeymap(keys),rowPin,colPin,4,4);

int enA=22,enB=23,in1=24,in2=25,in3=26,in4=27;

int h=0, m=0; //hour and minute

void setup()
{ 
  rtc.halt(false); // Set the clock to run-mode, and disable the write protection
  rtc.writeProtect(false);
  //rtc.setTime(12, 30, 0);     // Set the time 
  //rtc.setDate(15, 5, 2018);   // Set the date  
 
  lcd.begin(16, 2); // Setup LCD to 16x2 characters
  lcd.clear();
  lcd.setCursor(4, 0);
  lcd.print("Welcome!");
  
  pinMode(enA, OUTPUT); //ena
  pinMode(enB, OUTPUT); //enb
  pinMode(in1, OUTPUT); //in1
  pinMode(in2, OUTPUT); //in2
  pinMode(in3, OUTPUT); //in3
  pinMode(in4, OUTPUT); //in4

  digitalWrite(enA, HIGH);
  digitalWrite(enB, HIGH);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  delay(1500);
  digitalWrite(enA, LOW);
  digitalWrite(enB, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
}

void loop()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(rtc.getTimeStr()); //print time
  lcd.print(" Press A");
  lcd.setCursor(0, 1);
  lcd.print(rtc.getDateStr()); //print date
  delay (1000);
  char pressed = keypad.getKey(); //Read the keypad
  int state=0, act=0, b=0;
  if(pressed == 'A')
  {
   state = 1;
   func1();
   while (state == 1)
   {
    char keyB = keypad.getKey();
    if(keyB == 'B'){
    state = 2;
    }
    if(keyB == 'C'){
      state = 3;
    }
    if(keyB == '#')
      state = 0;
   }
   if(state == 2)
   feed();
   if(state == 3)
   {
    act= 1;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Set Time: __:__");
    lcd.setCursor(0, 1);
    lcd.print("*.Ok  #.Cancel");
    while (act == 1)
    {
      char keyC = keypad.getKey();
      if(keyC == '#')
      act = 0;
        
    }
    
   }
  }  
}

 void func1(void)
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("B. Feed Now");
  lcd.setCursor(0, 1);
  lcd.print("C. Set Time");
  return;
}

void feed(void)
{
  lcd.clear();
  lcd.setCursor(3,0);
  lcd.print("Feeding...");
  
  digitalWrite(enA, HIGH); //motor a
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  delay(2000); 
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  delay(2000);
  digitalWrite(enA, LOW);
  digitalWrite(in2, LOW); 
  
  digitalWrite(enB, HIGH); // motor b
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  delay(2000); 
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  delay(2000);
  digitalWrite(enB, LOW);
  digitalWrite(in3, LOW);
  return;
}

My question is: how can I recive input from the keypad to set the alarm time?

Have you tried

char digit = keypad.getKey();

Maybe I didn't understand your problem.

When you decide the user is setting the alarm, you will have to assemble the time for the alarm to happen.
Probably 2 digits for the hour and 2 digits for the minute.
Once the digits are entered, maybe look for a * as a terminator that defines the numbers are okay.
Maybe # would cancel setting the alarm as data entry is in error.
If after the 4th digit you enter more digits, display ERROR, or go back to the 'hours' entry.
Each time through loop, you will then compare the entered hours and minutes to the current hours and minutes.
After you get a compare the alarm is turned on/off/canceled.

Also, the TimeAlarm library allows you to make a 'one time alarm'.
See examples.
Alarm.alarmOnce(hours, minutes, seconds, function);

larryd:
If after the 4th digit you enter more digits

After the fourth digit, why even wait for more digits?

Can give the user a chance to make corrections to the time.
It is up to the OP to do what they what.

OK thank you, I'll try some things with your advice.

larryd:
Can give the user a chance to make corrections to the time.

It's an alarm. For an alarm clock.
If they punch in the wrong four digits, they can just go back and punch in the right four digits.

odometer:
It's an alarm. For an alarm clock.
If they punch in the wrong four digits, they can just go back and punch in the right four digits.

I think the OP can figure out what She/He wants, oh i think I implied that already :wink: