TimeAlarms Summary -- What Runs and What Doesn't Run

Hi,
Nothing new here, just a summary, in one place, of TimeAlarms running – and not running. Bottom line: TimeAlarms must have an Alarm.delay() executed every time through the void loop(). Hope this is useful & helpful to those trying to use TimeAlarms…

With my Arduino Yun (just love it), I setTime() with time, in seconds, from the Python side. And then ran various sketches blinking led 13 and the alarm triggering a simple function printing “I RAN!!!”.

This runs, Alarm.delay(500) is executed (twice) every loop.

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

const byte ledPin = 13;
String timeFormat;
const byte alarmHour = 12;
const byte alarmMinute = 30;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Bridge.begin();
  Console.begin();
  while (!Console);
  timeFormat = "date +%s";
  setTime(GetTimeShell(timeFormat).toInt() - 25200); //adjust to Mt Std Time
  Alarm.alarmRepeat(alarmHour, alarmMinute, 0, RunFunction);
}

void loop()
{
  digitalWrite(ledPin, HIGH);
  Alarm.delay(500);
  digitalWrite(ledPin, LOW);
  Alarm.delay(500);
}

String GetTimeShell(String&timeFormatInFunc) {
  String timeStringInFunc = "";
  Process GetTime;
  GetTime.runShellCommand(timeFormatInFunc);
  while (GetTime.available()) {
    timeStringInFunc += (char) GetTime.read();
  }
  timeStringInFunc.trim();
  return timeStringInFunc;
}

void RunFunction() {
  Console.println("I RAN!!!!!!!!!!!!!!!");
}

And, if you’re so inclined, this runs, Alarm.delay(0) executed every time through loop.

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

const byte ledPin = 13;
String timeFormat;
const byte alarmHour = 12;
const byte alarmMinute = 32;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Bridge.begin();
  Console.begin();
  while (!Console);
  timeFormat = "date +%s";
  setTime(GetTimeShell(timeFormat).toInt() - 25200); //adjust to Mt Std Time
  Alarm.alarmRepeat(alarmHour, alarmMinute, 0, RunFunction);
}

void loop()
{
  digitalWrite(ledPin, HIGH);
  unsigned long currentTime = millis();
  unsigned long exitTime = currentTime + 500;
  while (currentTime < exitTime) {
    currentTime = millis();
  }
  digitalWrite(ledPin, LOW);
  currentTime = millis();
  exitTime = currentTime + 500;
  while (currentTime < exitTime) {
    currentTime = millis();
  }
  Alarm.delay(0);
}

String GetTimeShell(String&timeFormatInFunc) {
  String timeStringInFunc = "";
  Process GetTime;
  GetTime.runShellCommand(timeFormatInFunc);
  while (GetTime.available()) {
    timeStringInFunc += (char) GetTime.read();
  }
  timeStringInFunc.trim();
  return timeStringInFunc;
}

void RunFunction() {
  Console.println("I RAN!!!!!!!!!!!!!!!");
}

This doesn’t run; no Alarm.delay().

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

const byte ledPin = 13;
String timeFormat;
const byte alarmHour = 12;
const byte alarmMinute = 34;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Bridge.begin();
  Console.begin();
  while (!Console);
  timeFormat = "date +%s";
  setTime(GetTimeShell(timeFormat).toInt() - 25200); //adjust to Mt Std Time
  Alarm.alarmRepeat(alarmHour, alarmMinute, 0, RunFunction);
}

void loop()
{
  digitalWrite(ledPin, HIGH);
  unsigned long currentTime = millis();
  unsigned long exitTime = currentTime + 500;
  while (currentTime < exitTime) {
    currentTime = millis();
  }
  digitalWrite(ledPin, LOW);
  currentTime = millis();
  exitTime = currentTime + 500;
  while (currentTime < exitTime) {
    currentTime = millis();
  }
}

String GetTimeShell(String&timeFormatInFunc) {
  String timeStringInFunc = "";
  Process GetTime;
  GetTime.runShellCommand(timeFormatInFunc);
  while (GetTime.available()) {
    timeStringInFunc += (char) GetTime.read();
  }
  timeStringInFunc.trim();
  return timeStringInFunc;
}

void RunFunction() {
  Console.println("I RAN!!!!!!!!!!!!!!!");
}

Neither does this work; Alarm.delay() in setup.

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

const byte ledPin = 13;
String timeFormat;
const byte alarmHour = 12;
const byte alarmMinute = 44;
bool alarmFlag = true;
void setup()
{
  pinMode(ledPin, OUTPUT);
  Bridge.begin();
  Console.begin();
  while (!Console);
  timeFormat = "date +%s";
  setTime(GetTimeShell(timeFormat).toInt() - 25200); //adjust to Mt Std Time
  Alarm.alarmRepeat(alarmHour, alarmMinute, 0, RunFunction);
  Alarm.delay(0);
}

void loop()
{
  digitalWrite(ledPin, HIGH);
  unsigned long currentTime = millis();
  unsigned long exitTime = currentTime + 500;
  while (currentTime < exitTime) {
    currentTime = millis();
  }
  digitalWrite(ledPin, LOW);
  currentTime = millis();
  exitTime = currentTime + 500;
  while (currentTime < exitTime) {
    currentTime = millis();
  }
  if (alarmFlag){
    Alarm.delay(0);
    alarmFlag = false;
  }
}

String GetTimeShell(String&timeFormatInFunc) {
  String timeStringInFunc = "";
  Process GetTime;
  GetTime.runShellCommand(timeFormatInFunc);
  while (GetTime.available()) {
    timeStringInFunc += (char) GetTime.read();
  }
  timeStringInFunc.trim();
  return timeStringInFunc;
}

void RunFunction() {
  Console.println("I RAN!!!!!!!!!!!!!!!");
}

And in the spirit of completeness, this doesn’t run; Alarm.delay() only executed first time through loop.

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

const byte ledPin = 13;
String timeFormat;
const byte alarmHour = 12;
const byte alarmMinute = 44;
bool alarmFlag = true;
void setup()
{
  pinMode(ledPin, OUTPUT);
  Bridge.begin();
  Console.begin();
  while (!Console);
  timeFormat = "date +%s";
  setTime(GetTimeShell(timeFormat).toInt() - 25200); //adjust to Mt Std Time
  Alarm.alarmRepeat(alarmHour, alarmMinute, 0, RunFunction);
  Alarm.delay(0);
}

void loop()
{
  digitalWrite(ledPin, HIGH);
  unsigned long currentTime = millis();
  unsigned long exitTime = currentTime + 500;
  while (currentTime < exitTime) {
    currentTime = millis();
  }
  digitalWrite(ledPin, LOW);
  currentTime = millis();
  exitTime = currentTime + 500;
  while (currentTime < exitTime) {
    currentTime = millis();
  }
  if (alarmFlag){
    Alarm.delay(0);
    alarmFlag = false;
  }
}

String GetTimeShell(String&timeFormatInFunc) {
  String timeStringInFunc = "";
  Process GetTime;
  GetTime.runShellCommand(timeFormatInFunc);
  while (GetTime.available()) {
    timeStringInFunc += (char) GetTime.read();
  }
  timeStringInFunc.trim();
  return timeStringInFunc;
}

void RunFunction() {
  Console.println("I RAN!!!!!!!!!!!!!!!");
}