Developing Logic for Alarm Setting

Le Sun 02 February 2014

The code below successfully determines which alarm is due and when it should adjust the weekend alarms. This logic has been carried across to the Arduino program here to test that it still works in the Arduino. The logic is robust so that a step of 50 seconds say will also yield the same result. Whether the longer steps between cycles interferes with the Alarm operation will need to be tested. We can test this by using the example sketch in the TimeAlarms Library on the main Arduino website and changing the Alarm.delay to something like 7000 milliseconds that the cycle time will possibly screw up the 15 second alarm.

An earlier version of the Python program used the len function to measure the length of the array. As the Arduino does not have this function the work around was to use the NoF (Number of Fields) variable that was initially read off the SD Card to set up the array and would still be floating around and available for use in the final Arduino Program.

Related Project Pages

The code below can be downloaded from here: Timer_Alarm_Logic_Development.py

# 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.
# The arduino does not have an easy array length function as python has so the NoF or
# Number of fields variable that exists in the rest of the Arduino sketch to pick up the
# Array values will have to be used.

times = [1386347400,1386563400,1387548000,1389155400,1389967200,1390278600,1397743200,1398141000,1398348000,1398659400,1401458400,1401769800,1414159200,1414470600]
NowTime=1386347000    # This is the current time in Unix Time format. This will come from the RTC
StatusFlag = False  #Is the HWC enabled or not?
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
YellowLED=False #Yellow LED indicator to show that the HWC element is enabled.
GreenLED=False  #Green LED indicater to show that the system is working.
NoF = 14        #This is the number of fields in the array. It is found in the Arduino sketch.

AlarmDateOn=0 #This is the variable to simulate the alarm function.  It will be used to call a subroutine if the time matches the AlarmDate.  This coding will not be needed for the Arduino.
AlarmDateOff=0

# Catchup function
def Catchup (a,b):
    while a<NowTime:
        a=a+604800    # Add a week's worth of seconds
        b=b+604800
    return [a,b]




# Setup Catchup - this jumps the Weekend alarms forward a week at a time until they are in the
# future.
print (NowTime)
print (times[0],times[1])
if times[0]< NowTime:
    WkEnds = Catchup(times[0],times[1])  # Call the catchup function
    times[0]=WkEnds[0]
    times[1]=WkEnds[1]

# Step through the times[] array components to find the next START of the holiday period.
InDexer = 2
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

print (times[0],times[1])
print (RedLED)
print (InDexer)
print ('Holiday Alarm', times[InDexer],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.
# The Weekends will be set to skip forward if it turns out that a holiday period is
# coming up next.
# The alarms willbe reset ONLY after the end alarm date has passed.

while NowTime < 1414900000: #Start the timing loop.
    dd = AlarmDateOn    #This is justa holder to help with debugging
    ee = AlarmDateOff    #This is another holder to help with debugging
    if ((times[0]> times[InDexer]) and (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]
    elif ((times[0]>times[InDexer]) and (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]:
        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]:
        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
    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
        print (AlarmDateOn) #Whenever a date is triggered.
    if ee != AlarmDateOff:
        print (AlarmDateOff)
    NowTime = NowTime + 50   #The design of the logic is robust and can cope with 50second steps or more.
                            #How this will impact on the operation of the alarms has yet to be seen.
                            # A trial with the Arduino will be required.

Output from this script.

The script essentially speeds through time to calculate the alarms that are going to be required next. There are two alarms used, one for enabling the hot water cylinder elements and one for disabling the elements.

1389966200
(1386347400, 1386563400)
(1389976200, 1390192200)
False
4
('Holiday Alarm', 1389967200, 1390278600)
1389967200
1390278600
1390581000
1390797000
1391185800
1391401800
1391790600
1392006600
1392395400
1392611400
1393000200
1393216200
1393605000
1393821000
1394209800
1394425800
1394814600
1395030600
1395419400
1395635400
1396024200
1396240200
1396629000
1396845000
1397233800
1397449800
1397743200
1398141000
1398348000
1398659400
1399048200
1399264200
1399653000
1399869000
1400257800
1400473800
1400862600
1401078600
1401458400
1401769800
1402072200
1402288200
1402677000
1402893000
1403281800
1403497800
1403886600
1404102600
1404491400
1404707400
1405096200
1405312200
1405701000
1405917000
1406305800
1406521800
1406910600
1407126600
1407515400
1407731400
1408120200
1408336200
1408725000
1408941000
1409329800
1409545800
1409934600
1410150600
1410539400
1410755400
1411144200
1411360200
1411749000
1411965000
1412353800
1412569800
1412958600
1413174600
1413563400
1413779400
1414159200
1414470600
1414773000
1414989000

Or when interpreted

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   Easter
1398141000  Tuesday, 22 April 2014 04:30    Easter
1398348000  Thursday, 24 April 2014 14:00   Anzac
1398659400  Monday, 28 April 2014 04:30 Anzac
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   Queens Birthday
1401769800  Tuesday, 3 June 2014 04:30  Queens Birthday
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   Labour Day
1414470600  Tuesday, 28 October 2014 04:30  Labour Day
1414773000  Friday, 31 October 2014 16:30   
1414989000  Monday, 3 November 2014 04:30

Par Hamish Trolove, Cat├ęgorie : Tech Projects

Tags : Arduino / Python /