[SOLVED] Alarm library not working.

Hello.

I’m doing a little clock with and 16x2 LCD just to learn how to use the time library and LCD.

I made a little project where I show the current time on a LCD shield, which also have buttons.

If the ‘Select’ button is pressed, then we enter in a editing mode to modify the time. up/down/right/left are used to modify the hour, minute and second digit. Pressing select again, sets up the new time and show the current time again.

I put an alarm to a specifc time, so I could star to work in a menu to set an alarm. But it doesn’t work. I have tried the example from the library web site, and it worked, but when I put it in my code, it doesn’t.

The example code can be found here: http://www.pjrc.com/teensy/td_libs_TimeAlarms.html

I’m using an arduino MEGA 2560 - The LCD shield has the compatible pins for it: http://www.dx.com/p/118059

Here is my code:

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

int led = 13;

//Controla o menu
const int MENU_NONE = 0;
const int MENU_HOUR = 1;
int CurrentMenu = 0;

// Initialize LCD
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

//Used to read Analog buttons
int ButtonVoltage = 0;
int ButtonPressed = 0;
int LastButton = 7;

//Control Button Debounce
long lastDebounceTime = 0;  
long debounceDelay = 30;

//Hour Control

int hourEditing = 0; // 0 - Hour, 1 - minuto, 2
int Hour;
int Min;
int Sec;
boolean wasEdited = false; // Flag para definir se a Hour foi ou não editada

void setup() {
  Serial.begin(9600);
  delay(50);
  Serial.write("Init\n\r");
  //SETUP LCD
  lcd.begin(16, 2);
  lcd.clear();
  setupClock(10,10,10);
  printNow();

  Alarm.alarmRepeat(10,10,15, turnLedOn);
}

void setupClock(int h, int m, int s){
  setTime(h,m,s,10,04,2013);
}

//Prints the current time
void printNow(){
  printTime(hour(),minute(),second());
}

//Print the specificTime:
void printTime(int h,int m,int s){
  char time[8];
  lcd.setCursor(8,0);
  sprintf(time,"%02d:%02d:%02d",h, m, s);
  lcd.print(time);
}

/* 
  Places the cursor on the hour, minute or second digit. 
  The cursor will be blinking indicating which digit will
  be changed
*/
void placeHourCursor(){
  switch(hourEditing){
  case 0:
    lcd.setCursor(8,0);
    break;
  case 1:
    lcd.setCursor(11,0);
    break;
  case 2:
    lcd.setCursor(14,0);
    break;
  }
}

//Stores the current time
void saveCurrentHour(){
  Hour = hour();
  Min = minute();
  Sec = second();
}

/* 
   Adjusts the time. dir is direction (up or down);
   Depending on where the cursor is, it will increment or
   decrement the hour, minute or second digit. 
 */
void adjustHour(int dir){
  wasEdited = true;
  char digito[2];
  switch(hourEditing){
  case 0: //Hour
    Hour = getIncremento(Hour, 23, dir);
    break;
  case 1: //MINUTO
    Min = getIncremento(Min, 59, dir);
    break;
  case 2: //SEGUNDO
    Sec = getIncremento(Sec, 59, dir);
    break;
  }
  printTime(Hour,Min,Sec);
}

/* 
  General purpose funtion. 
  Increments or decrements a number considering a minumum (zero) and a 
  maximum (maxInc) value. dir is direction (UP or DOWN).
 */
int getIncremento(int number, int maxInc, int dir){
  if(dir == 0){ //INCREMENTO
    if(number == maxInc) {
      number = 0;
    }
    else{
      number++;
    }
  }
  else{ //DECREMENTO
    if(number == 0) {
      number = maxInc;
    }
    else{
      number--;
    }
  }
  return number;
}

/*
  Handles the button pressed.
  We have five buttons: Up, Down, Right, Left and Select.
  
  When selected is pressed, and the CurrentMenu is NONE, then 
  we enter in time editing mode. The cursor starts to blink in 
  the hour digit. Pressing up and down will increment or decrement 
  the hour. Pressing right and left will move the cursor to the other
  digits (minute and second). 
  
  When select is pressed again, if the time was modified, the clock is
  adusjted and starts to show current time again.
*/
void readButton(int btn){
  switch (btn)
  {
  case 0: //RIGHT
    if(CurrentMenu == MENU_HOUR){
      hourEditing = getIncremento(hourEditing, 2, 0);
    }
    break; 
  case 1: //UP
    if(CurrentMenu == MENU_HOUR){
      adjustHour(0);
    }
    break;
  case 2: //DOWN
    if(CurrentMenu == MENU_HOUR){
      adjustHour(1);
    }
    break;
  case 3: //LEFT
    if(CurrentMenu == MENU_HOUR){
      hourEditing = getIncremento(hourEditing, 2, 1);
    }
    break;
  case 5: //SELECT
    if(CurrentMenu == MENU_HOUR){
      lcd.noBlink();
      CurrentMenu = MENU_NONE;
      if(wasEdited){
        setupClock(Hour, Min, Sec);
      }
      wasEdited = false;
    }
    else if (CurrentMenu == MENU_NONE){
      CurrentMenu = MENU_HOUR;
      saveCurrentHour();
      lcd.blink();
    }
    break;
  }

  if(CurrentMenu == MENU_HOUR){
    //If I'm editing the hour, place the blinking cursor
    placeHourCursor();
  }
  else{
    //For now.. fo nothing;
  }
  delay(200);
}

void turnLedOn(){
  Serial.write("On\n\r");
}

void loop() {
  ButtonVoltage = analogRead(0);
  ButtonPressed = ButtonVoltage >> 7; //takes the most significant bit.

  if(ButtonPressed != LastButton){
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    readButton(ButtonPressed);
  }

  LastButton = ButtonPressed;  
  if(CurrentMenu == MENU_NONE){
    printNow();
  }
}

You don't have a call to Alarm.delay() in your code.

But |I don’t call alarm.delay I just call the regular delay … so If a keep the button pressed the numbers don’t get incremented too fast.

A call to Alarm.delay() is needed in order for the alarms to work.

To quote from the TimeAlarms readme

Your sketch should call the Alarm.delay() function instead of the Arduino delay() function when
using the Alarms library. The timeliness of triggers depends on sketch delays using this function.
Alarm.delay( period); // Similar to Arduino delay - pauses the program for the period (in milliseconds).

Sorry, I misread your first answer. That did the trick. Thanks.