Comparing time

Hi,

With the following code, I check the status of my store’s blinds to see it they’re opened between 22:30 and 7:59. The blinds open automatically at 8AM and they close manually at 22:00 but I added 30 min margin in the code.

I feel a bit uncomfortable with the code as the time comparison seems a bit rudimental even though it’s working.
That said, is there a way to improve it? I’m using the Yun board and I’m reading the time from Linino.

TIA

#include <Process.h>

// set pin numbers
const int blindsSensorPin = 12; // (digital in) magnetic switch

// variables
int magSwitchState = 0; // magnetic switch status HIGH for closed
bool blindsState = false; // used to send out only one SMS msg

void setup()
{

  //Bridge.begin();

  pinMode(blindsSensorPin, INPUT); // init magnetic switch as input
}

void loop() {

  // read mag switch status. If blinds are closed, the State is HIGH:
  magSwitchState = digitalRead(blindsSensorPin);

  // if time is between 22:30 and 7:59 and if magSwitchState is LOW,
  // send msg that blinds are open!!

  if (blindsOpen() && !blindsState) { // both param must be true
    blindsState = true; // toggle blinds status to prevent sending out many SMS
    // send SMS
  }
  delay(1000);
}

//******** Blinds Open  *********
bool blindsOpen () {

  // get hours & minutes, convert to int and compare
  String timeString = getTimeStamp(); // jump to sub
  String hourString = timeString.substring(9, 11); // strip hours
  String minString = timeString.substring(12, 14); // strip minutes
  int hours = hourString.toInt(); // convert string to int
  int minutes = minString.toInt();

  // if time is between 22:30 and 7:59 and blinds are open, send ALERT
  if (((hours == 22 && minutes > 30) || // checks time between 22:30 and 22:59
       (hours > 22) ||                  // checks time between 23:00 and 24:00
       (hours < 8))                     // checks time between 00:00 and 7:59
      && magSwitchState == LOW) {
    return true;
  }
  else {
    return false;
  }
}

//    Get TimeDate
String getTimeStamp() {
  String result;
  Process time;
  time.begin("date");
  time.addParameter("+%D-%T");
  time.run();

  while (time.available() > 0) {
    char c = time.read();
    if (c != '\n')
      result += c;
  }

  return result;
}
bool blindsState = false; // used to send out only one SMS msg

Good thing you have that comment there. I thought, at first, that you were going to store whether the blinds were there, or not, in this variable.

If you had used a name like messageSent, the comment would not have been needed.

  if (((hours == 22 && minutes > 30) || // checks time between 22:30 and 22:59
       (hours > 22) ||                  // checks time between 23:00 and 24:00
       (hours < 8))                     // checks time between 00:00 and 7:59
      && magSwitchState == LOW) {
    return true;
  }
  else {
    return false;
  }

It would easier to test that the time IS between 8:00 and 22:30. You could do that with one variable, minutesSinceMidnight, containing the appropriate value. I’m going to assume that you can determine what the appropriate value is…

Thanks Paul for the tip on the nomenclature, will use messageSent as it makes more sense.

As comparing a sum of minutes minutesSinceMidnight between 480 (for 8:00) and 1350 (for 22:30) is easy to accomplish but, how do I fill in the variable with actual count of minutes?

how do I fill in the variable with actual count of minutes?

   int minutesSinceMidnight = hours * 60 + minutes;

Will try it, thank you!