Arduino - Alarm Logic

Le Sun 05 January 2014

The following code is the adaptation of the Python code found here. The code below has limited the loop so that the weekends generated are only within the year of interest. After having completed the period of interest, it just flashes the LED using the Blink sketch. An earlier version did not have this limitation and rushed off to find all of the weekends (successfully) out to the limits of what the unsigned long variables could hold.

Related Project Pages

The sketch below can be downloaded from here: LogicTester.ino

/*
The following is a program to develop the logic for setting and triggering the alarms
in the Arduino Timer Project
The array used is the same array as the Arduino gets from the SD Card.
*/
unsigned long times[] = {1386347400,1386563400,1387548000,1389155400,1389967200,1390278600,1397743200,1398141000,1398348000,1398659400,1401458400,1401769800,1414159200,1414470600};
int NoF = 14;  // This is the number ofcomponents in the array. This figure is generated in the
               // array loading section of the project.
unsigned long NowTime=1386347000;    // This is the current time in Unix Time format. This will come from the RTC
boolean StatusFlag = false;  // Is the HWC enabled or not?
boolean RedLED = false; //LED pin control.This will be used to indicate the Red LED state for available times exceeded. Basically if this goes off you will need to put more dates in the SD Card
boolean YellowLED=false; //Yellow LED indicator to show that the HWC element is enabled.
boolean GreenLED=false;  //Green LED indicater to show that the system is working.
int InDexer = 2;    //InDexer is the array pointer that indicates which is the next holiday period to be used.
unsigned long AlarmDateOn=0;    //This is justa holder to help with debugging - real alarms will be used instead when coded for real
unsigned long AlarmDateOff=0;   //This is another holder to help with debugging - real alarms will be used instead when coded for real
boolean Completed = false;    //This just signals completion of the loop.
int ledPin = 13;    // LED connected to digital pin 13

void setup()
{
  pinMode(ledPin, OUTPUT);
  //Open Serial Communications and wait for the port to open:
  Serial.begin(9600);
  // Make all weekend alarm dates and holiday period pointers current.
  Serial.println(NowTime);
  Serial.print(times[0]),Serial.print(","),Serial.println(times[1]);
  if (times[0]< NowTime)    // The following updates the weekend dates to be in the future.
  {
    while (times[0]<NowTime)
    {
      times[0]=times[0]+604800;    // Add a week's worth of seconds
      times[1]=times[1]+604800;
    }
  }

  // Step through the times[] array components to find the next START of the holiday period.
  while (times[InDexer]<NowTime)    // Check to see if the date is in the future.
  {
    InDexer = InDexer + 2;       // Adding two means we look ONLY at the start dates.
    if (InDexer >= NoF-1)   //  Have we exceeded the list?  If so enable the error light
    {
      RedLED = true;
      break;
    }
  }
  // Report the next weekend and whether the list of special breaks has been exceeded.
  Serial.print(times[0]);Serial.print(",");Serial.println(times[1]);
  Serial.print(RedLED);
  Serial.print(InDexer);    //This indicates where in the list of holiday breaks we are looking and the next holidays.
  Serial.print("Holiday Alarm");Serial.print(times[InDexer]);Serial.println(times[InDexer+1]);
}


void loop()
{
    // The following section will loop the NowTime to simulate the info from the RTC.
    // To carry over into this new section the up-to-date InDexer will be used to work out
    // Which Holiday periods are coming up and which alarm goes next. InDexer is a Global Variable.
    // The Weekends will be set to skip forward if it turns out that a holiday period is
    // coming up next.
        // Although the loop allows us to repeatedly loop through the process
        // and does give the right dates for weekends many years in the future
        // we will limit the loop to being just a first year so that we can examine the
        // holiday dates. Once it is done we will set the LED to flash so that the
        // loop is kept occupied.
  while(Completed == false)      //This limits the looping. 
    {
    unsigned long dd = AlarmDateOn;    //This is justa holder to help with debugging - these carry the old values of the alarms
    unsigned long ee = AlarmDateOff;    //This is another holder to help with debugging -  these carry the old values of the alarms
    if (times[0]> times[InDexer] && RedLED == true) // It would be a holiday but the list has been exceeded
    {
        AlarmDateOn = times[0];    // In the Arduino this will load the date into the Alarm function
        AlarmDateOff = times[1];
    }
    else if (times[0]>times[InDexer] && RedLED == false) // The next item is a holiday and the list is OK.
    {
        AlarmDateOn = times[InDexer];
        AlarmDateOff = times[InDexer+1];  // Loads the Holiday times into the Alarm function
        while (times[0]< times[InDexer+1])
        {
            times[0]=times[0]+604800;    //Advance the weekend dates to be clear of the holiday
            times[1]=times[1]+604800;
        }
    }
    else                                //Normal Weekend
    {
        AlarmDateOn = times[0];         //In the Arduino this will load the date into the Alarm function
        AlarmDateOff = times[1];
    }

    if (NowTime > times[1]) // When a weekend start has passed jump forward to the next one.  We shall have to check that the logic
                            // does not cause the weekend close off alarm to be overwritten.
    {
        times[0]=times[0]+604800;    // Advance the weekend dates to the next one once the
        times[1]=times[1]+604800;    // current weekend is finished
    }
    if (NowTime > times[InDexer+1]) //When a holiday is in progress the pointer needs to be advanced.
    {
        InDexer = InDexer+2;    //Advance the pointer the the next holiday once current one
                                //finished.
        if (InDexer >= NoF-1)   //Have we exceeded the list?  If so enable the error light
        {
            RedLED = true;          //But carry on by setting InDexer to last good figure
            InDexer = InDexer - 2;
        }
    }
    // The following is to simulate the operation of the alarms.  If needed we can print the Status flags to the serial port.
    // We will need to look at the looptime step size if we do implement this as it is likely to miss the reporting times.
    if (NowTime == AlarmDateOn)
    {
          StatusFlag = true;
          YellowLED = true;
    }
        if (NowTime == AlarmDateOff)
    {
          StatusFlag = false;
          YellowLED = false;
    }
        if (dd != AlarmDateOn)   //The following lines are to produce a new dump of the alarms
    {
      Serial.println(AlarmDateOn); // Whenever a date change is triggered - print the new alarm settings.
        }
        if (ee != AlarmDateOff)
    {
      Serial.println(AlarmDateOff);
    }
    NowTime = NowTime + 500;
    if(NowTime >= 1419309000)
    {
      Completed = true;
    }
  }
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);          // wait for a second
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(1000);          // wait for a second
}

This is a subset of the output sent to the Serial Monitor.

1386347400  Friday, 6 December 2013 16:30
1386563400  Monday, 9 December 2013 04:30
1386952200  Friday, 13 December 2013 16:30
1387168200  Monday, 16 December 2013 04:30
1387548000  Friday, 20 December 2013 14:00
1389155400  Wednesday, 8 January 2014 04:30
1389371400  Friday, 10 January 2014 16:30
1389587400  Monday, 13 January 2014 04:30
1389967200  Friday, 17 January 2014 14:00
1390278600  Tuesday, 21 January 2014 04:30
1390581000  Friday, 24 January 2014 16:30
1390797000  Monday, 27 January 2014 04:30
1391185800  Friday, 31 January 2014 16:30
1391401800  Monday, 3 February 2014 04:30
1391790600  Friday, 7 February 2014 16:30
1392006600  Monday, 10 February 2014 04:30
1392395400  Friday, 14 February 2014 16:30
1392611400  Monday, 17 February 2014 04:30
1393000200  Friday, 21 February 2014 16:30
1393216200  Monday, 24 February 2014 04:30
1393605000  Friday, 28 February 2014 16:30
1393821000  Monday, 3 March 2014 04:30
1394209800  Friday, 7 March 2014 16:30
1394425800  Monday, 10 March 2014 04:30
1394814600  Friday, 14 March 2014 16:30
1395030600  Monday, 17 March 2014 04:30
1395419400  Friday, 21 March 2014 16:30
1395635400  Monday, 24 March 2014 04:30
1396024200  Friday, 28 March 2014 16:30
1396240200  Monday, 31 March 2014 04:30
1396629000  Friday, 4 April 2014 16:30
1396845000  Monday, 7 April 2014 04:30
1397233800  Friday, 11 April 2014 16:30
1397449800  Monday, 14 April 2014 04:30
1397743200  Thursday, 17 April 2014 14:00
1398141000  Tuesday, 22 April 2014 04:30
1398348000  Thursday, 24 April 2014 14:00
1398659400  Monday, 28 April 2014 04:30
1399048200  Friday, 2 May 2014 16:30
1399264200  Monday, 5 May 2014 04:30
1399653000  Friday, 9 May 2014 16:30
1399869000  Monday, 12 May 2014 04:30
1400257800  Friday, 16 May 2014 16:30
1400473800  Monday, 19 May 2014 04:30
1400862600  Friday, 23 May 2014 16:30
1401078600  Monday, 26 May 2014 04:30
1401458400  Friday, 30 May 2014 14:00
1401769800  Tuesday, 3 June 2014 04:30
1402072200  Friday, 6 June 2014 16:30
1402288200  Monday, 9 June 2014 04:30
1402677000  Friday, 13 June 2014 16:30
1402893000  Monday, 16 June 2014 04:30
1403281800  Friday, 20 June 2014 16:30
1403497800  Monday, 23 June 2014 04:30
1403886600  Friday, 27 June 2014 16:30
1404102600  Monday, 30 June 2014 04:30
1404491400  Friday, 4 July 2014 16:30
1404707400  Monday, 7 July 2014 04:30
1405096200  Friday, 11 July 2014 16:30
1405312200  Monday, 14 July 2014 04:30
1405701000  Friday, 18 July 2014 16:30
1405917000  Monday, 21 July 2014 04:30
1406305800  Friday, 25 July 2014 16:30
1406521800  Monday, 28 July 2014 04:30
1406910600  Friday, 1 August 2014 16:30
1407126600  Monday, 4 August 2014 04:30
1407515400  Friday, 8 August 2014 16:30
1407731400  Monday, 11 August 2014 04:30
1408120200  Friday, 15 August 2014 16:30
1408336200  Monday, 18 August 2014 04:30
1408725000  Friday, 22 August 2014 16:30
1408941000  Monday, 25 August 2014 04:30
1409329800  Friday, 29 August 2014 16:30
1409545800  Monday, 1 September 2014 04:30
1409934600  Friday, 5 September 2014 16:30
1410150600  Monday, 8 September 2014 04:30
1410539400  Friday, 12 September 2014 16:30
1410755400  Monday, 15 September 2014 04:30
1411144200  Friday, 19 September 2014 16:30
1411360200  Monday, 22 September 2014 04:30
1411749000  Friday, 26 September 2014 16:30
1411965000  Monday, 29 September 2014 04:30
1412353800  Friday, 3 October 2014 16:30
1412569800  Monday, 6 October 2014 04:30
1412958600  Friday, 10 October 2014 16:30
1413174600  Monday, 13 October 2014 04:30
1413563400  Friday, 17 October 2014 16:30
1413779400  Monday, 20 October 2014 04:30
1414159200  Friday, 24 October 2014 14:00
1414470600  Tuesday, 28 October 2014 04:30
1414773000  Friday, 31 October 2014 16:30
1414989000  Monday, 3 November 2014 04:30
1415377800  Friday, 7 November 2014 16:30
1415593800  Monday, 10 November 2014 04:30
1415982600  Friday, 14 November 2014 16:30
1416198600  Monday, 17 November 2014 04:30
1416587400  Friday, 21 November 2014 16:30
1416803400  Monday, 24 November 2014 04:30
1417192200  Friday, 28 November 2014 16:30
1417408200  Monday, 1 December 2014 04:30
1417797000  Friday, 5 December 2014 16:30
1418013000  Monday, 8 December 2014 04:30
1418401800  Friday, 12 December 2014 16:30
1418617800  Monday, 15 December 2014 04:30
1419006600  Friday, 19 December 2014 16:30
1419222600  Monday, 22 December 2014 04:30
1419611400  Friday, 26 December 2014 16:30
1419827400  Monday, 29 December 2014 04:30

Par Hamish Trolove, Cat├ęgorie : Tech Projects

Tags : Arduino /