TimeAlarm (Manual time setter)

Alarm.alarmRepeat(10,30,0,timeout);

how can i change this code so that the time will first be set using keypad before the alarm starts..
the time (HH, MM, SS) will be inputed using keypad and by clicking '*' (ok key) the alarm will automatically starts..

There must surely be examples of data entry that come with the keypad library.

Once you have verified the HH MM SS in your code and then the * is received you call the function with the values inserted. Am I missing something?

can you help me to create an example code.. there are some examples in keypad but i am confused on how to enter the data in Alarm.alarmRepeat(10,30,0,timeout);
for example,
int x,y,z;
Alarm.alarmRepeat(x,y,z,timeout);
then how can i replace the 3 integer according to the numbers inputed on the keypad..

i'm still a beginner in this arduino so i need more help in making this..

Here is a simple example that you should be able to add to so you get what you want:

#include <Time.h>
#include <TimeAlarms.h>

unsigned long startMillis;       //used for timing in this simple example
int data = 5;                    //this is the number of seconds before the alarm goes off
//                                 you would set data equal to the number(s) entered on your keypad                                 
boolean alarmWasCreated = false; //used to make sure only one alarm is created

void setup()
{
  Serial.begin(9600);
  
  // set time to Saturday 8:29:00am Jan 1 2011
  setTime(8,29,0,1,1,11); 

    startMillis =millis();
} // END of setup()

void  loop(){  
  digitalClockDisplay();
  
  // wait one second between clock display
  Alarm.delay(1000); 

  //for this example, after 10 seconds create the alarm
  if(millis() - startMillis >= 10000UL && !alarmWasCreated)
  {
    Serial.println();
    Serial.println("The alarm has been created at:");
    digitalClockDisplay();
    Serial.println();
    
    //by now, you would have set data equal to the number entered on your keypad
    //in data seconds the alarm will happen
    Alarm.timerOnce(data, OnceOnly);

    alarmWasCreated = true;    //a flag to prevent another alarm from being created
  }  

} // END of loop()

//*******************************************************************************

void OnceOnly(){
  Serial.println("The Alarm has gone off");  
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println(); 
}

void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

@LarryD
thank you.. i'll try it now..

how can i change the output if the keypad when i press HOLD?
keyevent(key);
case HOLD:
if (key == ' #')
{ key = ','
}
??

Please explain a bit more what you want to happen, give an example.

You know ' #' has a space in front of the #
You want '#' not ' #'
You also need a ; after an instruction line
key=',';

i need to use the ',' character in entering data for Alarm.alarmRepeat(data, Repeats);
for example is: Alarm.alarmRepeat(11,5,0, Repeats);
so i would like to add an event in the '#' key in the keypad which gives the ',' character by case HOLD.

You would create the alarm in a similar manner as shown here:

//Alarm Timers at run time

#include <Time.h>
#include <TimeAlarms.h>

//We are defining these now however, in the final sketch these are captured from the keypad
byte Hr = 8;   //hour
byte Min = 30; //minute
byte Sec = 10; //second

unsigned long startMillis;       //used for timing in this simple example
boolean alarmWasCreated = false; //used to make sure only one alarm is created

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

  // set time to Saturday 8:29:00am Jan 1 2011
  setTime(8,29,0,1,1,11); 

  startMillis =millis();
} // END of setup()

void  loop(){  
  digitalClockDisplay();

  // wait one second between clock display
  Alarm.delay(1000); 

  //for this example only, after 10 seconds create the alarm
  if(millis() - startMillis >= 10000UL && !alarmWasCreated)
  {
    Serial.println();
    Serial.println("The alarm has been created at:");
    digitalClockDisplay();
    Serial.println();

    //by now, you would have set Hr Min and Sec equal to the numbers entered on your keypad
    //for example, lets say we have entered 8 for Hr and 30 for Min and 10 for Sec
    //Now you will create the alarm with these values
    Alarm.alarmRepeat(Hr,Min,Sec, Repeats);
    //so an alarm will go off at 8:30:10 AM each day
    
    alarmWasCreated = true;    //for this example, a flag to prevent another alarm from being created
  }  

} // END of loop()

//*******************************************************************************

void Repeats(){
  Serial.println();
  Serial.println("The Alarm has gone off at:"); 
  digitalClockDisplay();
  Serial.println();
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println(); 
}

void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

my program stops when reaching setAlarm() after pressing '1'
i think the problem is in this part

while ( (unsigned long)(millis() - timeRef) < 5000 )
{
// THE PROCESS STOPS HERE...
// int8_t button = Serial.read();
char key = keypad.getKey();
switch (keypad.getState())

but i have no idea how to fix this.. the time runs out returning back to the loop even if i'm pressing the '#' , '0' and '*' key..

inulit_q.ino (5.32 KB)

here is the code..

#include <Wire.h>
#include <DS1307.h>

#include <LiquidCrystal.h>
LiquidCrystal lcd(53, 51, 49, 47, 45, 43);

#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'},
    {'*','0',','}
};
  byte rowPins[ROWS] = {40, 42, 44, 46};
  byte colPins[COLS] = {48, 50, 52};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

/*
about set time:
format: year,month,day,week,hour,min,sec
example: 14,03,25,02,13,55,10   2014.03.25 tuesday 13:55:10
*/

      int8_t button;  // Holds the current button pressed
      uint8_t alarmHours = 0, alarmMinutes = 0;  // Holds the current alarm time
      uint8_t tmpHours;
      boolean alarm = false;  // Holds the current state of the alarm
      unsigned long timeRef;
   //   DateTime now;  // Holds the current date and time information

String comdata = "";
int mark=0;
//store the current time data
int rtc[7];
//store the set time data
byte rr[7];
//light pin
int ledPin =  13;
//initial light
char data;

void setup()

{
  DDRC |= _BV(2) | _BV(3); // POWER:Vcc Gnd
  PORTC |= _BV(3); // VCC PINC3
  pinMode(ledPin, OUTPUT);
  //initial baudrate
  Serial.begin(9600);

  //get current time
  RTC.get(rtc, true);
  //if time is wrong reset to default time
  if (rtc[6] < 12) {
    //stop rtc time
    RTC.stop();
    RTC.set(DS1307_SEC, 1);
    RTC.set(DS1307_MIN, 27);
    RTC.set(DS1307_HR, 01);
    RTC.set(DS1307_DOW, 7);
    RTC.set(DS1307_DATE, 12);
    RTC.set(DS1307_MTH, 2);
    RTC.set(DS1307_YR, 12);
    //start rtc time
    RTC.start();
  }
  RTC.SetOutput(DS1307_SQW32KHZ);
}
 
void loop()
{
  showTime();  
    timeRef = millis();
    char key = keypad.getKey();
    if (key) { Serial.println(key);
          }
      switch (keypad.getState())
      case PRESSED:
        {
           if (key == '1') {
             makeAlarm();
           }
        }
}
            
int showTime() 
    {
lcd.begin(16,2);
lcd.print("BioAMS");
    {  
          int i;
  //get current time 
  RTC.get(rtc, true);
  //print current time format : year month day week hour min sec
  for (i = 0; i < 7; i++)
  {
    Serial.print(rtc[i]);
    Serial.print(" ");
    
 //Time
    lcd.setCursor(7, 0);
    lcd.print(rtc[0]);
    lcd.setCursor(9, 0);
    lcd.print("/");
   lcd.setCursor(10, 0);
   lcd.print(rtc[1]);
    lcd.setCursor(12, 0);
    lcd.print("/");
    lcd.setCursor(14, 0);
    lcd.print(rtc[2]);
 //Week  
    lcd.setCursor(0, 1);
    lcd.print(rtc[3]);
 //Date
    lcd.setCursor(2, 1);
    lcd.print(",");
    lcd.setCursor(3, 1);
    lcd.print(rtc[4]);
    lcd.setCursor(5, 1);
    lcd.print("/");
    lcd.setCursor(6, 1);
    lcd.print(rtc[5]);
    lcd.setCursor(8, 1);
    lcd.print("/");   
    lcd.setCursor(10, 1);
    lcd.print(rtc[6]);
  }
  //blink the light
  Serial.println();
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
  delay(500);
  //
  int j = 0;
  //read all the data
  while (Serial.available() > 0)
  {
    comdata += char(Serial.read());
    delay(2);
    mark = 1;
  }
  //if data is all collected,then parse it
  if (mark == 1)
  {
    Serial.println(comdata);
//lcd.setCursor(0,1);
//lcd.print((rtc[i]));
    Serial.println(comdata.length());
    //parse data
    for (int i = 0; i < comdata.length() ; i++)
    {
      //if the byte is ',' jump it,and parse next value
      if (comdata[i] == ',')
      {
        j++;
      }
      else
      {
        rr[j] = rr[j] * 10 + (comdata[i] - '0');
      }
    }
    comdata = String("");
    RTC.stop();
    RTC.set(DS1307_SEC, rr[6]);
    RTC.set(DS1307_MIN, rr[5]);
    RTC.set(DS1307_HR, rr[4]);
    RTC.set(DS1307_DOW, rr[3]);
    RTC.set(DS1307_DATE, rr[2]);
    RTC.set(DS1307_MTH, rr[1]);
    RTC.set(DS1307_YR, rr[0]);
    RTC.start();
    mark = 0;
  }
    }
   }
    
 int makeAlarm()
 {
    unsigned long timeRef;
    boolean timeOut = true;
    uint8_t tmpMinutes = 0;
    
    lcd.clear();
    lcd.print("Alarm Time");

    timeRef = millis();
    lcd.setCursor(0,1);
    lcd.print("Set minutes:  0");
    
    while ( (unsigned long)(millis() - timeRef) < 5000 )
    {
      // THE PROCESS STOPS HERE...
   //    int8_t button = Serial.read();
     char key = keypad.getKey();
     switch (keypad.getState())

          if (key== '*')
        {
            tmpMinutes = tmpMinutes < 55 ? tmpMinutes + 1 : tmpMinutes;
            lcd.setCursor(13,1);
            lcd.print("  ");
            lcd.setCursor(13,1);
            if ( tmpMinutes < 10 ) lcd.print(" ");
            lcd.print(tmpMinutes);
            timeRef = millis();
        }
        else if ( key == '0' )
        {
            tmpMinutes = tmpMinutes > 0 ? tmpMinutes - 1 : tmpMinutes;
            lcd.setCursor(13,1);
            lcd.print("  ");
            lcd.setCursor(13,1);
            if ( tmpMinutes < 10 ) lcd.print(" ");
            lcd.print(tmpMinutes);
            timeRef = millis();
        }
        else if ( key == '#' )
        {
            while ( button != NO_KEY ) ;
            timeOut = false;
            break;
        }
        delay(150);

    if ( !timeOut )
    {
        alarmHours = tmpHours;
        alarmMinutes = tmpMinutes;
     //   transition(Serial.read());
   // Turn off the display:
  lcd.noDisplay();
  delay(3000);
   // Turn on the display:
  lcd.display();
  delay(3000);  
  }
  //  else transition(TIME_OUT);
}
lcd.clear();
 }

[code/]

:cry:
You have delay()s all over which is blocking.
The way you have while loops is blocking.
You do not need (unsigned long) in this:
while ( (unsigned long)(millis() - timeRef) < 5000 )
You are using String, most would recommend never use it!
What happened to Alarm.alarmRepeat(11,5,0, Repeats); which is what you originally asked about?

Honestly, I recommend you to start over, but first layout in words what you want to do.
Add one section at a time and when each works, continue.
Write the code to output to the Serial monitor only, when the code works replace the Serial.prints with your lcd stuff.

ok.. thanks for the recomendations. i appriciate you on helping me..
this is a different code i've tested and it does not use the alarmTime library.. i think ill go back to the older code wich use the timeAlarm lib.
should i start coding first using the serial monitor instead of using already the LCd and Keypad??
thanks a lot..

I usually start something like this with the Serial monitor only.
When everything works as I want then I would get the keypad to work as needed.
Finally I would add LCD display stuff.
What exactly do you want as a final product?

our project is biometric attendance monitoring syst.
its purpose is to take the attendance of every sudent during the day.. but we need to create a time limiter in the system so that taking attendance from the fingerprint sensor will bee stopped at a giver time..
for example,
after starting to take attendance from the fingerprint, we first set the time (for example 7:30:00 am) so that when it reached that time the fingerprint will no longer accept any attendance..

That's a good start.

Where does the keypad come in?
What is the fingerprint sensor attached to?
"our project" what does this mean?

i'll use the matrix keypad and it will be used as option menu.. for example. (press1 for start Enroll, press2 for show time and date, press3 for printing and etc.) and also use it to enter the data in the alarm function..
the fingerprint sensor attached to arduino to capture the fingerprint that serves as the attendance of every person so that it will be unique for every person and the time recorded will be constant,,

What Arduino board are you going to use?
How may finger prints can be saved?

So you are going to have:
RTC
Finger print sensor
Keypad 4X4?
LCD
Anything else?
Will you draw a schematic and attach it?
"our project" what does this mean?

before i start, i am using arduino uno, but the pins are very limited so i changed to arduino Mega because it has 50+ pins.. and also, i also using:
RTC,
LCD(16x2),
Matrix Keypad(3x4),
Fingerprint Scanner,
Thermal Printer and
VB.net application for creating the software.

according to the fingerprint sensor i am using, it can contain 162 templates.. and also.. if i can also add SD Card reader so i can datalog the names attendees so it can be read in VB.NET..
and lastly, Li-ion battery so it can supply without plugging in AC.

what kind of schematic?? do you mean the whole process of the system??
nevermind about "our project" i just get used of saying that always when i;m making something..