Alarm

Hi,

I am making a alarm that will go off several times a day.
The time is syncronized by a one second pulse at midnight.
It is based on the Time/TimeAlarms library.
I have wrote the software, but I am getting the following errors

Here is the code:

//This program recieves a 1 second pulse at middnight for syncronization
//Alarms are referenced off that event
//The alarms sound a buzzer for one second on pin 13
//The circuit recieves a pulse on pin 2

#include "Time.h"
#include "TimeAlarms.h"

const int midnightPin= 2;
const int timerPin= 13;


int midnightState=0;

void setup()
{ 
  pinMode(midnightPin, INPUT);
  pinMode(timerPin, OUTPUT);
  
  Alarm.alarmRepeat(8,45,0,MeetingTwo); 
  Alarm.alarmRepeat(9,00,0,MeetingThree);
  Alarm.alarmRepeat(9,15,0,MeetingFour);
  Alarm.alarmRepeat(9,30,0,MeetingFive);
  Alarm.alarmRepeat(10,00,0,MeetingSix);
  Alarm.alarmRepeat(10,30,0,MeetingSeven);
  Alarm.alarmRepeat(11,00,0,MeetingEight);
  Alarm.alarmRepeat(15,00,0,MeetingNine);
  
}

void MeetingOne()
{
  if (timeSet) {  //checks to see if time has been set
  digitalWrite(timerPin, HIGH);  //turns the buzzer on
  Alarm.delay(1000);  //waits one second
  digitalWrite(timerPin, LOW);  //turns the buzzer off
  }    
}

void MeetingTwo()
{
 if (timeSet) {
  digitalWrite(timerPin, HIGH);
  Alarm.delay(1000);
  digitalWrite(timerPin, LOW);
  }               
}

void MeetingThree()
{
 if (timeSet) {
  digitalWrite(timerPin, HIGH);
  Alarm.delay(1000);
  digitalWrite(timerPin, LOW);
  }            
}

void MeetingFour()
{
 if (timeSet) {
  digitalWrite(timerPin, HIGH);
  Alarm.delay(1000);
  digitalWrite(timerPin, LOW);
  }    
}

void MeetingFive()
{
 if (timeSet) {
  digitalWrite(timerPin, HIGH);
  Alarm.delay(1000);
  digitalWrite(timerPin, LOW);
  }    
}

void MeetingSix()
{
 if (timeSet) {
  digitalWrite(timerPin, HIGH);
  Alarm.delay(1000);
  digitalWrite(timerPin, LOW);
  }    
}

void MeetingSeven()
{
 if (timeSet) {
  digitalWrite(timerPin, HIGH);
  Alarm.delay(1000);
  digitalWrite(timerPin, LOW);
  }    
}

void MeetingEight()
{
 if (timeSet) {
  digitalWrite(timerPin, HIGH);
  Alarm.delay(1000);
  digitalWrite(timerPin, LOW);
  }    
}

void MeetingNine()
{
 if (timeSet) {
  digitalWrite(timerPin, HIGH);
  Alarm.delay(1000);
  digitalWrite(timerPin, LOW);
  }    
}

void  loop()
{ 
 midnightState = digitalRead(midnightPin);  //reads the pulse
  if (midnightState == HIGH) {  
    setTime(23,59,0,1,1,10);  //sets time to one second before midnight
  }
}
 In function `loop':
 undefined reference to `setTime(int, int, int, int, int, int)'

o: In function `MeetingNine()':
undefined reference to `Alarm'

undefined reference to `Alarm'
undefined reference to `TimeAlarmsClass::delay(unsigned long)'

o: In function `MeetingEight()':
undefined reference to `Alarm'
undefined reference to `Alarm'
undefined reference to `TimeAlarmsClass::delay(unsigned long)'

o: In function `MeetingSeven()':
undefined reference to `Alarm'
undefined reference to `Alarm'

undefined reference to `TimeAlarmsClass::delay(unsigned long)'

o: In function `MeetingSix()':
undefined reference to `Alarm'
undefined reference to `Alarm'

undefined reference to `TimeAlarmsClass::delay(unsigned long)'

o: In function `MeetingFive()':
undefined reference to `Alarm'
undefined reference to `Alarm'
undefined reference to `TimeAlarmsClass::delay(unsigned long)'

o: In function `MeetingFour()':
undefined reference to `Alarm'

undefined reference to `Alarm'

undefined reference to `TimeAlarmsClass::delay(unsigned long)'

o: In function `MeetingThree()':
: undefined reference to `Alarm'

unundefined reference to `TimeAlarmsClass::delay(unsigned long)'

o: In function `MeetingTwo()':
undefined reference to `Alarm'

undefined reference to `Alarm'

undefined reference to `TimeAlarmsClass::delay(unsigned long)'

o: In function `setup':
undefined reference to `Alarm'
undefined reference to `Alarm'
undefined reference to `TimeAlarmsClass::alarmRepeat(int, int, int, void (*)())'
undefined reference to `TimeAlarmsClass::alarmRepeat(int, int, int, void (*)())'
undefined reference to `TimeAlarmsClass::alarmRepeat(int, int, int, void (*)())'
undefined reference to `TimeAlarmsClass::alarmRepeat(int, int, int, void (*)())'
undefined reference to `TimeAlarmsClass::alarmRepeat(int, int, int, void (*)())'

more undefined references to `TimeAlarmsClass::alarmRepeat(int, int, int, void (*)())' follow

I don't see an "Alarm" object.

Sorry, this could be a silly question,
But what is an alarm object?

The code does contain this:

#include "Time.h"
#include "TimeAlarms.h"

Alarm.alarmRepeat(8,45,0,MeetingTwo);

This code is calling the alarmRepeat method of the Alarm object, which does not exist.

You have not created an instance of the Alarm class. That instance of the Alarm class is referred to as an object.

You need to do something like this:

Alarm noisyDamnClock;

void setup()
{
   noisyDamnClock.alarmRepeat(8, 45, 0, BoringMeeting);
}

Thanks, I was tearing my hair out about this as well but on a different issue. The certain alarm doesn't trigger . It's odd that it works in the example given. No mention of an instance was used. Here's the code of the example given:

I assumed Alarm is an instance defined in TimeAlarms.h etc...

/*
 * TimeAlarmExample.pde
 *
 * This example calls alarm functions at 8:30 am and at 5:45 pm (17:45)
 * and simulates turning lights on at night and off in the morning
 *
 * A timer is called every 15 seconds
 * Another timer is called once only after 10 seconds
 *
 * At startup the time is set to Jan 1 2010  8:29 am
 */

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

void setup()
{
  Serial.begin(9600);    
  Serial.println("TimeAlarms Example");
  Serial.println("Alarms are triggered daily at 8:30 am and 17:45 pm");
  Serial.println("One timer is triggered every 15 seconds");
  Serial.println("Another timer is set to trigger only once after 10 seconds");
  Serial.println();
  
  setTime(8,29,40,1,1,10); // set time to 8:29:40am Jan 1 2010 

  Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day
  Alarm.alarmRepeat(17,45,0,EveningAlarm);  // 5:45pm every day 
 
  Alarm.timerRepeat(15, RepeatTask);            // timer for every 15 seconds    
  Alarm.timerOnce(10, OnceOnlyTask);            // called once after 10 seconds 
}

void MorningAlarm()
{
  Serial.println("Alarm: - turn lights off");    
}

void EveningAlarm()
{
  Serial.println("Alarm: - turn lights on");           
}

void RepeatTask()
{
  Serial.println("15 second timer");         
}

void OnceOnlyTask()
{
  Serial.println("This timer only triggers once");  
}

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

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

void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

make sure the Time library is in your libraries folder, perhaps.

error: 'Alarm' does not name a type In function 'void setup()':

I tried to add an object like this:
but I am still getting an error

Alarm noisydarnClock;

void setup()
{ 
  
  pinMode(midnightPin, INPUT);
  pinMode(timerPin, OUTPUT);
  
  noisydarnClock.alarmRepeat(0,00,10,MeetingOne); 
  noisydarnClock.alarmRepeat(0,00,30,MeetingTwo); 
  noisydarnClock.alarmRepeat(0,00,40,MeetingThree);
  noisydarnClock.alarmRepeat(0,00,59,MeetingFour);
  noisydarnClock.alarmRepeat(9,30,0,MeetingFive);
  noisydarnClock.alarmRepeat(10,00,0,MeetingSix);
  noisydarnClock.alarmRepeat(10,30,0,MeetingSeven);
  noisydarnClock.alarmRepeat(11,00,0,MeetingEight);
  noisydarnClock.alarmRepeat(15,00,0,MeetingNine);
  
}

What about :

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

?

Or contact user "mem".
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1262783962/0

I assumed Alarm is an instance defined in TimeAlarms.h

A correct assumption, verified by this line in TimeAlarms.h extern TimeAlarmsClass Alarm;  // make an instance for the user
The alarm library creates the Alarm object for the user (in the same way the serial library creates the Serial object), which is why there is no mention of objects in the documentation.

The Arduino convention of pre-creating objects that can only have a single instance can be surprising to experienced programmers at first, but as there always must be one and only one Alarm object in a sketch that uses this library, its a convenience for novices to have this ready to use.

BTW, there was an earlier version of the library that had a user created instance for each alarm but feedback indicated that the current version that has a single pre-created instance handling multiple alarms was easier for most Arduino users.

cne, the sketch you posted at the start of this thread compiles ok for me. Perhaps download and install the libraries again and see if it compiles for you.

The FAQ supplied with the library says that up to six alarms can be created but
after you have it compiling ok, you can change a define in TimeAlarms.h to set the maximum number of alarms to 8
#define dtNBR_ALARMS 8 // this is 6 in the distributed library

#include <Time.h>

#include <TimeAlarms.h>

const int ledPin =  13;      // the number of the LED pin
const int onPin = 5;

void setup()
{
  
  setTime(8,29,40,1,1,10); // set time to 8:29:40am Jan 1 2010 

  Alarm.alarmRepeat(8,30,0,morningAlarm);  // 8:30am every day
  Alarm.alarmRepeat(8,30,10,otherAlarm);  
  
  pinMode(ledPin, OUTPUT); 
  pinMode(onPin, OUTPUT); 
 
}
void morningAlarm()
{
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
}

void otherAlarm()
{
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);          
}

void  loop()
{  
  digitalWrite(onPin, HIGH);
}

I made a program to test out the alarm. The alarms should both go off in less than a miniute from start up, but they don't. Am I doing something wrong?

You need to call the Alarm.delay funtion from within loop.

The following is from the FAQ included in the readme file supplied with the library:

Task scheduling is handled in the Alarm.delay function.
Tasks are monitored and triggered from within the Alarm.delay call so Alarm.delay should be called
whenever a delay is required in your sketch.
If your sketch waits on an external event (for example, a sensor change),
make sure you repeatedly call Alarm.delay while checking the sensor.
You can call Alarm.delay(0) if you need to service the scheduler without a delay.

try the following:

void  loop()
{  
  digitalWrite(onPin, HIGH);
  Alarm.delay(1);
}

I have updated my main code, but when I turn it on, the alarm goes three times about 10s apart. When I turn pin 2 high, it sounds the alarm 6 times very fast. After this the program seems to stop.

//This program recieves a 1 second pulse at midnight for syncronization
//Alarms are referenced off that event
//The alarms sound a buzzer for one second on pin 13
//The circuit recieves a pulse on pin 2

#include "Time.h"
#include "TimeAlarms.h"

const int midnightPin= 2;
const int timerPin= 13;
const int buzzerPin= 4;


int midnightState=0;

void setup()
{ 
  pinMode(midnightPin, INPUT);
  pinMode(timerPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  
  Alarm.alarmRepeat(0,00,10,MeetingOne); 
  Alarm.alarmRepeat(0,00,20,MeetingTwo); 
  Alarm.alarmRepeat(0,00,40,MeetingThree);
  Alarm.alarmRepeat(0,00,59,MeetingFour);
  Alarm.alarmRepeat(9,30,0,MeetingFive);
  Alarm.alarmRepeat(10,00,0,MeetingSix);
  Alarm.alarmRepeat(10,30,0,MeetingSeven);
  Alarm.alarmRepeat(11,00,0,MeetingEight);
  Alarm.alarmRepeat(15,00,0,MeetingNine);
  
}

void MeetingOne()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH); 
  Alarm.delay(90);  
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);
 
}

void MeetingTwo()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH); 
  Alarm.delay(90);  
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);
 
}

void MeetingThree()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH); 
  Alarm.delay(90);  
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);
 
}

void MeetingFour()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH); 
  Alarm.delay(90);  
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);
 
}

void MeetingFive()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH); 
  Alarm.delay(90);  
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);
 
}

void MeetingSix()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH); 
  Alarm.delay(90);  
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);
 
}

void MeetingSeven()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH); 
  Alarm.delay(90);  
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);
 
}

void MeetingEight()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH); 
  Alarm.delay(90);  
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);
 
}

void MeetingNine()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH); 
  Alarm.delay(90);  
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);
 
}

void  loop()
{ 
 midnightState = digitalRead(midnightPin);  //reads the pulse
  if (midnightState == HIGH) {  
    setTime(23,59,0,1,1,10);  //sets time to one second before midnight
    Alarm.delay(0);
  }
  
}

why did you put the Alarm.delay call in the if statement?

alarms are only services when Alarm.Delay is called, but you only call this within the brief period of your midnight pulse.

Think of Alarm.delay as an enhanced version of the standard Arduino delay that while it is delaying also checks to see if an alarm has triggered.

//This program recieves a 1 second pulse at midnight for syncronization
//Alarms are referenced off that event
//The alarms sound a buzzer for one second on pin 13
//The circuit recieves a pulse on pin 2

#include "Time.h"
#include "TimeAlarms.h"

const int midnightPin= 2;
const int timerPin= 13;
const int buzzerPin= 4;


int midnightState=0;

void setup()
{
  pinMode(midnightPin, INPUT);
  pinMode(timerPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);

  Alarm.alarmRepeat(0,00,10,MeetingOne);
  Alarm.alarmRepeat(0,00,20,MeetingTwo);
  Alarm.alarmRepeat(0,00,40,MeetingThree);
  Alarm.alarmRepeat(0,00,59,MeetingFour);
  Alarm.alarmRepeat(9,30,0,MeetingFive);
  Alarm.alarmRepeat(10,00,0,MeetingSix);
  Alarm.alarmRepeat(10,30,0,MeetingSeven);
  Alarm.alarmRepeat(11,00,0,MeetingEight);
  Alarm.alarmRepeat(15,00,0,MeetingNine);

}

void MeetingOne()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH);
  Alarm.delay(90);
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);

}

void MeetingTwo()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH);
  Alarm.delay(90);
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);

}

void MeetingThree()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH);
  Alarm.delay(90);
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);

}

void MeetingFour()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH);
  Alarm.delay(90);
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);

}

void MeetingFive()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH);
  Alarm.delay(90);
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);

}

void MeetingSix()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH);
  Alarm.delay(90);
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);

}

void MeetingSeven()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH);
  Alarm.delay(90);
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);

}

void MeetingEight()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH);
  Alarm.delay(90);
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);

}

void MeetingNine()
{
  digitalWrite(timerPin, HIGH);
  digitalWrite(buzzerPin, HIGH);
  Alarm.delay(90);
  digitalWrite(timerPin, LOW);
  Alarm.delay(910);
  digitalWrite(buzzerPin, LOW);

}

void  loop()
{
 midnightState = digitalRead(midnightPin);  //reads the pulse
  if (midnightState == HIGH) {
    setTime(23,59,0,1,1,10);  //sets time to one second before midnight
    
  }
Alarm.delay(0);
}

I updated the code, but it is still acting funny.

but it is still acting funny.

Do you mean to say that it has exactly the same symptoms as before you made the change in loop?

If not, what are the symptoms?

edit: it would help you if you added some serial print statements in the alarm code to see which alarm was being triggered. You could also print the minute and second to show the time the alarm function was called.

When I turn it on, it cycles through all of the alarms correctly, but after I turn pin 2 high, it sounds the alarm about 6 times really fast.

The alarm logic does not expect time to run backwards and when you reset the clock back to the start of the previous day the scheduler can get confused.

If you want to adjust for drift, you could use the adjustTime function. This moves the clock forwards or backwards by a given number of seconds but does not disrupt the day and date.

Here is an untested fragment of code you could use when you get your sync pulse. To try this you should run it in real time- the drift calculation will not be realistic if you feed it sync pulses much more frequently than every 24 hours.

  long  drift = elapsedSecsToday(now()); 
  // if the elapsed time since previous midnight is a big number,
  // then its not quite reached midnight so the clock is slow
  // as small positive number means it running fast
  if(drift > 3600)  
    drift = drift - SECS_PER_DAY ;   // clock slow - this will be a negative number 
  // a positive number indicates that the clock has passed midnight so its fast 
  Serial.print("the drift in seconds is ");
  Serial.println(drift);
  adjustTime(-drift); // compensate by adding  seconds if slow, subtract if fast

BTW, where are your sync pulses coming from?

I used your suggestion on the serial.
The program works great, except for the fact that when the midnight pulse is recieved, the program cyles through all the alarms quickly. Is there a way to prevent this?
Also, when the power is turned on, the time is set to 0:00:00 automaticly. Is there a way to prevent this?

Here is the serial monitor:

Power Up:
0:00:00
0:00:01
0:00:02
0:00:03
..........


MIDNIGHT PULSE RECIEVED
MEETING ONE CALLED
MEETING TWO CALLED
MEETING THREE CALLED
MEETING FOUR CALLED
MEETING FIVE CALLED
MEETING SIX CALLED
1:00:05
1:00:06
1:00:07
.........

I am building a meeting alarm that will be tied in with an existing clock system. All the clocks are syncronised at midnight via 1 second DC pulse. Using this reference point I can make sure all the meetings are correctly timed compared to the clocks.