Displaying Alarm Times from other functions

I am trying to display alarm times by calling a different functions and using the read(AlarmID_t ID) tool in the timealarms.h file. However, I know i am not using it correctly because its not printing the times within that function or ID. The code is psted below and hopefully it makes sense as to what i am trying to do. In the end, I am merely using a timer and when that timer goes off, I have a print out of all the alarm times that are being claled in my program.

The getTriggered function the h file presents works fine but only displays the time of an alarm that went off. I dont care as to when it goes off or what it does when it does go off, I just want a print out of the actual time its supposed to goe off at. the read(AlarmID_t ID) I thought does this functionality but i cant seem to work through it.

Any sugguestions? Thanks

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

AlarmID_t trialID, afternoonAlarmID, GetTriggeredAlarmID;

char buf[50];
char buf2[50];

void setup()
{
Serial.begin(9600);
setTime(8,29,0,1,1,11);

trialID = Alarm.alarmRepeat(9,29,10, GetTriggeredAlarms1);
afternoonAlarmID = Alarm.alarmRepeat(9,29,12,GetTriggeredAlarms2);
Alarm.alarmRepeat(8,29,7, GetAllAlarmTimes);

}

void GetAllAlarmTimes ()
{

time_t read(AlarmID_t trialID);
{
if(isAllocated(trialID))
return Alarm[trialID].value ;
sprintf(buf2,"Morning Alarm Failure: Time %d:%d:%d %d/%d/%d", hour(),minute(),second(),month(),day(),year());
Serial.println(buf2);
else
return dtINVALID_TIME;
}
//time_t now();
//time_t read(AlarmID_t afternoonAlarmID);
//time_t read(AlarmID_t trialID);
//if(trialID == trialID) {

//if(hour() != 9 || minute() != 29){
}
}
afternoonAlarmID;

sprintf(buf,"Morning Alarm Failure: Time %d:%d:%d %d/%d/%d", hour(),minute(),second(),month(),day(),year());

Serial.print(buf);
Serial.println();

}

void GetTriggeredAlarms(){
}
void loop(){
digitalClockDisplay();
Alarm.delay(1000);
}

void digitalClockDisplay()
{

Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}

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

void GetTriggeredAlarms2 ()
{}

THE ERRORS I AM GETTING

alarm_time_latest_and_greatest.ino: In function 'void GetAllAlarmTimes()':
alarm_time_latest_and_greatest:28: error: 'isAllocated' was not declared in this scope
alarm_time_latest_and_greatest:29: error: no match for 'operator[]' in 'Alarm[trialID]'
alarm_time_latest_and_greatest:29: error: return-statement with a value, in function returning 'void'
alarm_time_latest_and_greatest:32: error: 'else' without a previous 'if'
alarm_time_latest_and_greatest:33: error: return-statement with a value, in function returning 'void'
alarm_time_latest_and_greatest.ino: At global scope:
alarm_time_latest_and_greatest:47: error: expected declaration before '}' token

I can't get your code to Auto Format let alone compile. "Too many right curly braces"
You need to fix that first.

How do I do that? I was able to just copy and paste directly from this page into the program and run it. I get the errors below, but the curly brackets I am not quite sure what you are talking about? arent those needed after you do the void function call e.g. void function () {}??

//I get what you mean about the curly brackets........

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

AlarmID_t trialID, afternoonAlarmID, GetTriggeredAlarmID;

char buf[50];
char buf2[50];

void setup()
{
Serial.begin(9600);
setTime(8,29,0,1,1,11);

trialID = Alarm.alarmRepeat(9,29,10, GetTriggeredAlarms1);
afternoonAlarmID = Alarm.alarmRepeat(9,29,12,GetTriggeredAlarms2);
Alarm.alarmRepeat(8,29,7, GetAllAlarmTimes);

}

void GetAllAlarmTimes ()
{

time_t read(AlarmID_t trialID);
{
if(isAllocated(trialID))
return Alarm[trialID].value ;

else
return dtINVALID_TIME;
}
//time_t now();
//time_t read(AlarmID_t afternoonAlarmID);
//time_t read(AlarmID_t trialID);
//if(trialID == trialID) {

sprintf(buf2,"Morning Alarm Failure: Time %d:%d:%d %d/%d/%d", hour(),minute(),second(),month(),day(),year());
Serial.println(buf2);
//if(hour() != 9 || minute() != 29){

//}

//}
afternoonAlarmID;

sprintf(buf,"Morning Alarm Failure: Time %d:%d:%d %d/%d/%d", hour(),minute(),second(),month(),day(),year());

Serial.print(buf);
Serial.println();

}

void GetTriggeredAlarms(){
}
void loop(){
digitalClockDisplay();
Alarm.delay(1000);
}

void digitalClockDisplay()
{

Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}

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

void GetTriggeredAlarms2 ()
{}

void GetAllAlarmTimes ()
{

  time_t read(AlarmID_t trialID);
  {
    if(isAllocated(trialID))
      return Alarm[trialID].value ;

    else    
      return dtINVALID_TIME;  
  }

You have no isAllocated() function in your code.
You are trying to return a variable from a function declared as void.
I have no idea what Alarm[trialID].value is all about and nor, it seems, has the compiler.
The read() function appears to be defined within the GetAllAlarmTimes() function although the semi-colon on the end makes it a function prototype I believe.

Okay, so I was able to correctly call a function that displays the alarm times. The code is posted below. However, I have some questions:

  1. Why must I have one function call another function to call another function to print these times?

  2. Can I call one function that identifies the ID of the alarm and prints the time in that same function without having to call multiple functions?

The code is pasted below and it works for the most part, but I am integrating this with other code that sends sensor data to the web, so i kinda need everything in one function.

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

AlarmID_t firstAlarm;
AlarmID_t secondAlarm;
AlarmID_t third;

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

firstAlarm = Alarm.alarmRepeat(8,30,0, MorningAlarm); // 8:30am every day
secondAlarm = Alarm.alarmRepeat(17,45,0,EveningAlarm); // 5:45pm every day
third=Alarm.alarmRepeat(8,30,4, Hello);
Alarm.alarmRepeat(8,29,48, Trial);
}

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

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

void Hello()
{
Serial.println("This works-- FINALLY");
}
void Trial()
{
showAlarmTime(firstAlarm);
showAlarmTime(secondAlarm);
showAlarmTime(third);
}
void loop()
{
time_t t = now();
digitalClockDisplay(t);

Alarm.delay(1000); // wait one second between clock display
}
void showAlarmTime(AlarmID_t id)
{
time_t alarmTime = Alarm.read(id);
//if( alarmTime <= SECS_PER_DAY)
// Serial.println(id, DEC);
digitalClockDisplay(alarmTime);
}
void digitalClockDisplay(time_t t)
{
Serial.print(hour(t));
Serial.print(":");
if (minute(t)<10)
Serial.print('0');
if (minute(t)<10)
Serial.print('0');
Serial.print(minute(t));
Serial.print(":");
if (second(t)<10)
Serial.print('0');
Serial.print(second(t));
Serial.println();
}

  1. Why must I have one function call another function to call another function to print these times?

  2. Can I call one function that identifies the ID of the alarm and prints the time in that same function without having to call multiple functions?

  1. You don't need to do that. You choose how to structure your program and using functions with parameters is a good idea. The easiest way to simplify things would be to put the code from the digitalClockDisplay() function in the showAlarmTime() function.

  2. Yes. Move the code that you have into a single function if that is what you want to do.

  1. You don't need to do that. You choose how to structure your program and using functions with parameters is a good idea. The easiest way to simplify things would be to put the code from the digitalClockDisplay() function in the showAlarmTime() function.

  2. Yes. Move the code that you have into a single function if that is what you want to do.

So when I move the digitalClockDisplay() into the showAlarmTime() function, I am still arriving at the same issue. Please refer to Using client.print in multiple functions within the same call question i posted a few minutes ago. One function is called to get sensor data and send it to the web. This function has to call another function to retrieve the alarm times. How do i code the first function to read the ID, and print the time in that same function? It has to be more complexed than simply copying and pasting it into the function. time_t is involved in the digitalClockdisplay and alarmID_t is in the showAlarm times. Do I just call one after the other?

showAlarmTime(alarmID_t, time_t)??

In addition, one function is still using another function to get the alarm time. so from 3 layers we went to 2 layer. How do you get just one layer?

It sounds like you want everything to happen in the Trial() function. You can do that if you really must, but it will mean putting the code currently in the showAlarmTime() in the function 3 times along with the code from digitalClockDisplay() 3 times. Do you really want to do that, and if so, why ?

Thank you for understanding my goal. So I am trying to send data to the web. For example temp and humidity. Lets say once every hour I send data to the web. Along with temp and humidity, I would like to send alarm times using the same call. I dont want to make two http requests one to send data and the other to send alarm times. I would rather send it all at once. This has to be done with intricacy because I will eventually open a websocket allowing me to write and change these alarm times from a dashboard on the web. I would like to limit my requests and only send data once. I got PID's, relays, and timers all in my Main code, so I am trying to simplify things in order to prevent a collision or crash. So if I have to do 3 times of each with the digitalClockdisplay() and the showAlarmTime all within my Trial function as you suggested, would this be the most practical and efficient way of doing this? Thanks for the help

So if I have to do 3 times of each with the digitalClockdisplay() and the showAlarmTime all within my Trial function as you suggested, would this be the most practical and efficient way of doing this?

I don't agree that it is either practical or efficient but if that is the way you want to do it then it is, of course, OK with me.

So, then using the client.print command in one function then calling another function and it having a client.print in the same request is possible? A http request in one function can be identified when calling another function and using client.print? If it is, I will try this tonight and give t a whirl. Yeah, I am just looking for a code that is short and simple and doesnt go through a million paths in order to send data. In addition, the alarm times might be in the middle of all of the stuff im sending for example at the bottom....

Would this be an appropriate method of retrieving the alarm time? By listing the showAlarmTime(firstAlarm) right after the client.print, is this properly done? Thanks for your time.

client.print("&ww=");
client.print.(ww);
client.print("&firstAlarm=");
showAlarmTime(firstAlarm);
client.print("&otherdata=");
client.println(otherdata);

I don't know enough about HTTP to give you detailed advice on its use, but if you are worried perhaps that data sent in the way you are doing it will involve several HTTP requests then why not collect all the data in an array before sending it in bulk ?

Yeah, I have been pondering that myself. Using the sprintf command seems pretty useful and the array is a good idea. Do they do the same thing essentially? Anyways, last night i got it to work. The method was what we were talking about yesterday. ..

client.print("&ww=");
client.print.(ww);
client.print("&firstAlarm=");
showAlarmTime(firstAlarm);
client.print("&otherdata=");
client.println(otherdata);

Sticking showAlarmTime in the middle worked out fine. I didnt realize the trickling effect by calling a function that calls another function and using the client.print will be identified and understood and connect with each function that you call. One http request in the Trial function was called and I had client.print for the sensor data there. and in the digitalClockdisplay function I had the minutes seconds, etc. . It worked like a charm. I appreciate your advice and expertise pertaining to microcontrollers and Arduino