Running 3 LED's at different times.

I’ve got a setup with a RTC, and 3 different LED’s. What I’m trying to acchieve is to:

-run a red LED at night, starting 20.00h
-when a set alarm (stated by alarmh and alarmm) runs, the red LED switches off, and the blue LED switches on.
-after half an hour, the blue LED switches, of, and the green LED switches on.

As it looks now, I can either get the blue, or the green LED on. If any of you could point me in the right direction, that would be great. (Not looking for a copy-paste answer, just a push…)

The code I’m working with:

void sleeptime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  
  if ((hour<20 && hour>alarmh) or (hour==alarmh && minute>alarmm && minute<alarmm+29)){
       digitalWrite(ledPinGreen, LOW);
       digitalWrite(ledPinRed, LOW);
       digitalWrite(ledPinBlue, HIGH);}
      
       if ((hour<20 && hour>alarmh) or (hour==alarmh && minute>alarmm+30)){
       digitalWrite(ledPinGreen, HIGH);
       digitalWrite(ledPinRed, LOW);
       digitalWrite(ledPinBlue, LOW);}

  else {
       digitalWrite(ledPinBlue, LOW);
       digitalWrite(ledPinGreen, LOW);
       digitalWrite(ledPinRed, HIGH);}
}

Are you familiar with Unix timestamps? Usually a lot simpler that way.

don't forget that minutes don't get bigger than 59:

alarmm+29

don't forget years have been greater than 255 for some time now:

byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

*** Look into the TimeAlarms library ***

Itwasme:
I’ve got a setup with a RTC, and 3 different LED’s. What I’m trying to acchieve is to:

-run a red LED at night, starting 20.00h
-when a set alarm (stated by alarmh and alarmm) runs, the red LED switches off, and the blue LED switches on.
-after half an hour, the blue LED switches, of, and the green LED switches on.

As it looks now, I can either get the blue, or the green LED on. If any of you could point me in the right direction, that would be great. (Not looking for a copy-paste answer, just a push…)

The code I’m working with:

void sleeptime()

{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
 
  if ((hour<20 && hour>alarmh) or (hour==alarmh && minute>alarmm && minute<alarmm+29)){
      digitalWrite(ledPinGreen, LOW);
      digitalWrite(ledPinRed, LOW);
      digitalWrite(ledPinBlue, HIGH);}
     
      if ((hour<20 && hour>alarmh) or (hour==alarmh && minute>alarmm+30)){
      digitalWrite(ledPinGreen, HIGH);
      digitalWrite(ledPinRed, LOW);
      digitalWrite(ledPinBlue, LOW);}

else {
      digitalWrite(ledPinBlue, LOW);
      digitalWrite(ledPinGreen, LOW);
      digitalWrite(ledPinRed, HIGH);}
}

 int TimeNow;
 int AlarmOn;
 int AlarmOff;
 int RedOn;

 // Compute all times to MINUTES
 TimeNow = ( (int)Hour * 60 ) + Min;
 AlarmOn = ( (int)AlarmHour * 60 ) + AlarmMin;
 AlarmOff = ( (int)AlarmHour * 60 ) + AlarmMin + 30;
 RedOn = 20 * 60;


 // When is RED ON ?
 If ( ( TimeNow >= RedOn ) AND ( TimeNow < AlarmON ) )
 {
     Red ON
     Grn OFF
     Blu OFF
 }
 else
 {
     // When is BLUE ON ? 
     If ( ( TimeNow >= AlarmOn ) AND ( TimeNow < AlarmOff ) )
     {
        Red OFF
        Grn OFF
        Blu ON
    }

    // When is Green ON ? 
    Else
    {
        Red OFF
        Grn ON
        Blu OFF
    }
 }

The pseudo code above assumes two things:

  1. Alarm ON & OFF is between 8PM and Midnight
  2. Green is ON when both Red and Blue are OFF

mrsummitville:
The pseudo code above assumes two things:

  1. Alarm ON & OFF is between 8PM and Midnight
  2. Green is ON when both Red and Blue are OFF

doing this exercise with TimeStamps is easier and your off time need not be larger than your on time. For example, I turn my alarm on at 20:00 and off at 06:00.

I’m using an overloaded timerEvaluate() function so that you can see two ways of creating a clock-based timer. One with start and end time, the other with start time and duration (in seconds).

So, if you can work your way through this, you can see you can accomplish what you need more easily with TimeStamps!

(compiles and tested)

#include <Time.h>

struct Timer{
  byte tHour, tMinute;
};

Timer redOnTime = {0,1};   //  on at 00:01 (times used for demonstration
Timer redOffTime = {0,10}; // off at 00:10  <<<<<see note below

Timer alarmSetTime = {0,3}; // 00:03 the alarm is set
unsigned long colorSwitchTime = 2 * 60UL; //two minutes to colorchange

bool lastRedState = false;
bool lastAlarmState = false;
unsigned long lastUpdateTime = 0;

const byte redPin = 2;
const byte greenPin = 3;
const byte bluePin = 4;

void setup() 
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  delay(2000);
}

void loop() 
{
  bool redState = timerEvaluate(redOnTime.tHour, redOnTime.tMinute, redOffTime.tHour, redOffTime.tMinute);
  if (redState != lastRedState)
  {
    if (redState)
    {
      digitalWrite(13, HIGH);
      digitalWrite(redPin, HIGH);
      Serial.println("Red LED ON");
    }
    else
    {
      digitalWrite(13, LOW);
      //digitalWrite(redPin, LOW);    //<<<<<<< you can optionally turn them all off at some time...
      //digitalWrite(greenPin, LOW);
      //digitalWrite(bluePin, LOW);
    }
  }
  lastRedState = redState;
  bool alarmState = timerEvaluate(alarmSetTime.tHour, alarmSetTime.tMinute, colorSwitchTime);
  if(lastAlarmState != alarmState)
  {
    if(alarmState)
    {
      digitalWrite(redPin, LOW);
      digitalWrite(bluePin,HIGH);
      Serial.println("Blue LED ON");
    }
    else
    {
      digitalWrite(bluePin, LOW);
      digitalWrite(greenPin,HIGH);
      Serial.println("Green LED ON");
    }
  }
  lastAlarmState = alarmState;
  if (millis() - lastUpdateTime >1000UL)
  {
    char dateTime[125] = "";
    sprintf(dateTime, "Date: %02d/%02d/%d Time: %02d:%02d:%02d", month(), day(), year(), hour(), minute(), second());
    Serial.println(dateTime);
    lastUpdateTime += 1000;
  }
}

bool timerEvaluate(const byte startHour, const byte startMinute, const byte endHour, const byte endMinute)  // comparing time here is easier with Unix timestamps...
{
  time_t on_time = tmConvert_t(0, startMinute, startHour, day(), month(), year());
  time_t off_time = tmConvert_t(0, endMinute, endHour, day(), month(), year());
  time_t now_time = tmConvert_t(second(), minute(), hour(), day(), month(), year());
  //
  if (on_time < off_time)
  {
    return (now_time > on_time && now_time < off_time);
  }
  else if (off_time < on_time)
  {
    return (now_time > on_time || now_time < off_time);
  }
  else // if both on and off are set to the same time, I'm confused...
  {
    return false;
  }
}

bool timerEvaluate(const byte startHour, const byte startMinute, const unsigned long duration)  // comparing time here is easier with Unix timestamps...
{
  time_t on_time = tmConvert_t(0, startMinute, startHour, day(), month(), year());
  time_t off_time = tmConvert_t(0, startMinute, startHour, day(), month(), year()) + duration;
  time_t now_time = tmConvert_t(second(), minute(), hour(), day(), month(), year());
  //
  if (on_time < off_time)
  {
    return (now_time > on_time && now_time < off_time);
  }
  else if (off_time < on_time)
  {
    return (now_time > on_time || now_time < off_time);
  }
  else // if both on and off are set to the same time, I'm confused...
  {
    return false;
  }
}

time_t tmConvert_t(const byte ss, const byte mm, const byte hh, const byte DD, const byte MM, const byte YYYY)
{
  tmElements_t tm;
  tm.Year = YYYY-1970;
  tm.Month = MM;
  tm.Day = DD;
  tm.Hour = hh;
  tm.Minute = mm;
  tm.Second = ss;
  tm.Wday = NULL; // not needed for makeTime() but just to show you that is is part of the tmElements_t struct
  time_t timeStamp = makeTime(tm);
  return timeStamp;
}

to those die-hards, yes I may not have the tmElements_t struct members adjusted correctly (e.g. the Year) but for the purpose of this sketch, it works.