Shop OBEX P1 Docs P2 Docs Learn Events
TIme Based Events with the Spinneret RTC question — Parallax Forums

TIme Based Events with the Spinneret RTC question

Brian CarpenterBrian Carpenter Posts: 728
edited 2011-07-23 10:20 in Accessories
I have done several project that have logged data with time. This would involve waiting for a condition on a pin to change states and then capturing the state change and time and logging it to an SD card.
What i have not ever done is Time based such as a sprinkler timer.
I know that i can use the RTC object that has been posted to get the time. If i were to have an ON time of 0600 and an OFF time of 1900.
Would i create an object that just constantly compares the RTC time to the desired set times and change the state of my output?

Comments

  • Mike GMike G Posts: 2,702
    edited 2011-07-17 06:06
    Brian, the S35390A RTC has two alarms. The alarms might be easier to deal with than parsing the date.

    I'm using Kye's RTC object. The code snippet below sets the alarm on INT1 to fire when the minutes matches 46. INT1AlarmCheck returns true if the alarm has fired.

    INT1Alarm(week, hour, minute)
    OBJ
    
      rtc           : "S35390A_RTCEngine.spin"
      pst           : "Parallax Serial Terminal"
      ...
        pst.str(string(13, "INT1Alarm: ")) 
        pst.dec(rtc.INT1Alarm(-1, -1, 46))
        pst.char(13)
      ...
        pst.str(string(13, "INT1AlarmCheck: ")) 
        pst.dec(rtc.INT1AlarmCheck)
        pst.char(13)
    

    There is also user defined output on pins INT1/INT2 which I have not tried. I suppose the output could drive external logic.
    S35390A Manual

    I would roll the alarm logic into a COG using the cognew command right in the object that processes the alarm.
  • JeffaJeffa Posts: 80
    edited 2011-07-17 10:45
    The alarm might be the best way to go, I'm researching it a bit for my own Spinneret project. This object http://obex.parallax.com/objects/45/ might give you some food for thought as well.
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2011-07-17 17:38
    thank you for the replies.
    I am actually making it more complicated than this in that i have 6 zones that could potentially each have their own start and stop times as well as a condition per zone, where ,when the zone is on an off state, and a condition is met on a pin, the zone will turn back on for 1 hour from that moment.

    This is why it looks like i will not be able to us the internal alarms and will need to create a 'parser' to handle this. We will see how it goes.
  • Mike GMike G Posts: 2,702
    edited 2011-07-17 18:20
    Brian, I don't see the 6 zones as a being a hurtle to using the alarm. An alarm fires and is reset. The next alarm can be contingent on the prior alarm event. I'd image all you'd need is a table of alarm events in RAM/EEPROM. From there just a matter of managing state. What do you think, would that work?
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2011-07-17 21:42
    Yes, that may work. I know that what i am doing now does not.
    I am pulling my hair out right now.
    I am using the SNTP demo code where the time get set by an SNTP server.
    It sets the RTC time. All of this is working correctly and i see the correct time in my debug window. (parallax terminal)
    then i try to do some comparisons.
    I have created a DAT section that has the on and off times. I have put them in the following format
    Zone1OnTime           long    0,5,00,1,5,00,2,5,00,3,5,00,4,5,00,5,7,00,6,7,00     '7 days a week in the format of DAY, Hour(24 hr) and minute. Day 1 is Monday
    Zone1OffTime          long    0,5,00,1,22,30,2,22,30,3,22,30,4,18,00,5,18,00,6,18,00
    
    The objects from the RTC are as follows.
    RTC.getday
    RTC.gethour
    RTC.getminutes
    

    I am trying to compare the one against the other to create some logic. This is not working as i had planned.
             REPEAT
                IF(RTC.getday) == 6 ' it is sunday
                  PlxST.str(string("Ive made it past the day decission"))
                  IF (RTC.gethour) == long[@Zone1Ontime][1]
                    PlxST.str(string("Ive made it past the hour decission"))
                    IF(RTC.getminutes) == long[@Zone1Ontime][2]
                      PlxST.str(string("Ive made it past the minute decission"))
                      PlxST.char(13)
                      PlxST.char(13)
                      PlxST.char(13)
                      PlxST.char(13)
                      PlxST.char(13)
                      PlxST.char(13)
                      PlxST.char(13)
                      PlxST.str(string("  We are now ON   "))
                   IF (RTC.gethour) == long[@Zone1Offtime][1]
                     IF (RTC.getminutes) == long[@Zone1Offtime][2]
                       PlxST.str(string("  We are now Off  ")) 
    
    I have tried many different ways. I changed the DAT to longs because that is what is being sent back from the RTC object. But still no joy.
    What am i doing wrong??
  • Mike GMike G Posts: 2,702
    edited 2011-07-18 04:01
    This might make it a little easier. It allows you to index into the table by day and retrieve the OnTime and Duration.
    CON
      _clkmode = xtal1 + pll16x     
      _xinfreq = 5_000_000
    
    
    
    DAT
      OnTable               byte   5,00, 5,00, 5,00, 5,00, 5,00, 7,00, 7,00
      Duration              byte   10,   20,   10,   20,   10,   20,   10
      OnTimer               long   @OnTable, @OnTable+2, @OnTable+4, @OnTable+6, @OnTable+8, @OnTable+10, @OnTable+12
    
    
    OBJ
      pst           : "Parallax Serial Terminal"
    
    PUB Main | i
      pst.Start(115_200)
      Pause(1000)
    
      repeat i from 0 to 6
        pst.str(string("Timer: "))
        pst.str(string("Day "))
        pst.dec(i)
        pst.str(string(" On Time "))
        pst.dec(byte[@@OnTimer[i]])
        pst.str(string(":"))
        pst.dec(byte[@@OnTimer[i]+1])
        pst.str(string("  Duration "))
        pst.dec(byte[@Duration[i]])
        pst.char(13)
    
    
    PRI Pause(ms)  
      waitcnt(((clkfreq / 1_000 * Duration - 3932) #> 381) + cnt)
      return
    

    By the way, I built a count down timer a while back. You're welcome to it not sure if it fits your needs though. It runs in a COG and can handle 4 timers with a 1/512 resolution and 2330 hour max. A pin goes high when the timer starts then low when the timer reaches zero.
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2011-07-18 07:42
    MikeG,
    This is what i currently have working.
      Zone1OnTime           LONG    500,500,500,500,500,700,700
      Zone1OffTime          LONG    730,2230,2230,2230,1800,1800,1800
      Zone2OnTime           long    500,500,500,500,500,700,700
      Zone2OffTime          long    730,2230,2230,2230,1800,1800,1800
      Zone3OnTime           long    500,500,500,500,500,700,700
      Zone3OffTime          long    730,2230,2230,2230,1800,1800,1800
      Zone4OnTime           long    500,500,500,500,500,700,700
      Zone4OffTime          long    800,2230,2230,2230,1800,1800,1800
      Zone5OnTime           long    500,500,500,500,500,700,700
      Zone5OffTime          long    800,2230,2230,2230,1800,1800,1800
      Zone6OnTime           long    500,500,500,500,500,700,700'  
      Zone6OffTime          long    800,2230,2230,2230,1800,1800,1800
    
    and here is the code that is working. Now i need to gust include state changes on the conditions
    PUB CheckTimeByZone 
    
      
        Today := (RTC.getdayofweek)
        
        currentTime := (RTC.gethour) * 100
        currentTime := currentTime + (RTC.getminutes)
        PlxST.dec(currentTime)
        PlxST.char(13)
        PlxST.char(13)
        PlxST.char(13)
                
        ontime := Zone1Ontime[Today]
        offtime:= Zone1Offtime[Today]
        PlxST.char(13)
        PlxST.char(13)
        PlxST.char(13)
        PlxST.dec(ontime)
        PlxST.str(string("---"))
        PlxST.dec(offtime)
        PlxST.str(string("---"))
        
        IF currentTime > ontime AND currentTime < offtime
          PlxST.str(string("Zone 1 On  "))
        ELSE
          PlxST.str(string("ZOne 1 Off "))
    
        ontime := Zone2Ontime[Today]
        offtime:= Zone2Offtime[Today]
        PlxST.char(13)
        PlxST.dec(ontime)
        PlxST.str(string("---"))
        PlxST.dec(offtime)
        PlxST.str(string("---"))
        
        IF currentTime > ontime AND currentTime < offtime
          PlxST.str(string("Zone 2 On  "))
        ELSE
          PlxST.str(string("ZOne 2 Off "))
    
        ontime := Zone3Ontime[Today]
        offtime:= Zone3Offtime[Today]
        PlxST.char(13)
        PlxST.dec(ontime)
        PlxST.str(string("---"))
        PlxST.dec(offtime)
        PlxST.str(string("---"))
        
        IF currentTime > ontime AND currentTime < offtime
          PlxST.str(string("Zone 3 On  "))
        ELSE
          PlxST.str(string("ZOne 3 Off "))
    
        ontime := Zone4Ontime[Today]
        offtime:= Zone4Offtime[Today]
        PlxST.char(13)
        PlxST.dec(ontime)
        PlxST.str(string("---"))
        PlxST.dec(offtime)
        PlxST.str(string("---"))
        
        IF currentTime > ontime AND currentTime < offtime
          PlxST.str(string("Zone 4 On  "))
        ELSE
          PlxST.str(string("ZOne 4 Off "))
    
        ontime := Zone5Ontime[Today]
        offtime:= Zone5Offtime[Today]
        PlxST.char(13)
        PlxST.dec(ontime)
        PlxST.str(string("---"))
        PlxST.dec(offtime)
        PlxST.str(string("---"))
        
        IF currentTime > ontime AND currentTime < offtime
          PlxST.str(string("Zone 5 On  "))
        ELSE
          PlxST.str(string("ZOne 5 Off "))
    
        ontime := Zone6Ontime[Today]
        offtime:= Zone6Offtime[Today]
        PlxST.char(13)
        PlxST.dec(ontime)
        PlxST.str(string("---"))
        PlxST.dec(offtime)
        PlxST.str(string("---"))
        
        IF currentTime > ontime AND currentTime < offtime
          PlxST.str(string("Zone 6 On  "))
        ELSE
          PlxST.str(string("ZOne 6 Off "))
    
  • Mike GMike G Posts: 2,702
    edited 2011-07-18 08:25
    Glad you got it working!
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2011-07-23 10:20
    i thought i should share the current working code for time based events.

    I changed the way i store the day and times.
      Zone1OnTime           LONG    500,500,500,500,500,700,700
      Zone1OffTime          LONG    730,2230,2230,2230,600,1800,1800
      Zone2OnTime           long    500,500,500,500,500,700,700
      Zone2OffTime          long    730,2230,2230,2230,1800,1800,1800
      Zone3OnTime           long    500,500,500,500,500,700,700
      Zone3OffTime          long    730,2230,2230,2230,1800,1800,1800
      Zone4OnTime           long    500,500,500,500,500,700,700
      Zone4OffTime          long    2230,2230,2230,2230,1800,1800,1800
      Zone5OnTime           long    500,500,500,500,500,700,700
      Zone5OffTime          long    2230,2230,2230,2230,1800,1800,1800
      Zone6OnTime           long    500,500,500,500,500,700,700
      Zone6OffTime          long    2400,2230,2230,2230,1800,1800,1800
    

    PUB CheckTimeByZone 
    
        ZoneTimerFlag := %00000000
        Today := (RTC.getdayofweek)
        
        currentTime := (RTC.gethour) * 100
        currentTime := currentTime + (RTC.getminutes)
        PlxST.dec(currentTime)
        PlxST.char(13)
        ontime := Zone1Ontime[Today]
        offtime:= Zone1Offtime[Today]
        PlxST.char(13)
        PlxST.dec(ontime)
        PlxST.str(string("---"))
        PlxST.dec(offtime)
        PlxST.str(string("---"))
        
        IF currentTime > ontime AND currentTime < offtime
          PlxST.str(string("Zone 1 On  "))
          ZoneTimerflag := ZoneTimerFlag | %0000_0001
        ELSE
          PlxST.str(string("ZOne 1 Off "))
          'ZoneTimerflag[0] := %0
    
        ontime := Zone2Ontime[Today]
        offtime:= Zone2Offtime[Today]
        PlxST.char(13)
        PlxST.dec(ontime)
        PlxST.str(string("---"))
        PlxST.dec(offtime)
        PlxST.str(string("---"))
        
        IF currentTime > ontime AND currentTime < offtime
          PlxST.str(string("Zone 2 On  "))
          ZoneTimerFlag := ZoneTimerFlag | %0000_0010
        ELSE
          PlxST.str(string("ZOne 2 Off "))
          'ZoneTimerFlag[1] := %0
    
        ontime := Zone3Ontime[Today]
        offtime:= Zone3Offtime[Today]
        PlxST.char(13)
        PlxST.dec(ontime)
        PlxST.str(string("---"))
        PlxST.dec(offtime)
        PlxST.str(string("---"))
        
        IF currentTime > ontime AND currentTime < offtime
          PlxST.str(string("Zone 3 On  "))
          ZoneTimerFlag := ZoneTimerFlag | %0000_0100
        ELSE
          PlxST.str(string("ZOne 3 Off "))
          'ZoneTimerFlag[2] := %0
    
        ontime := Zone4Ontime[Today]
        offtime:= Zone4Offtime[Today]
        PlxST.char(13)
        PlxST.dec(ontime)
        PlxST.str(string("---"))
        PlxST.dec(offtime)
        PlxST.str(string("---"))
        
        IF currentTime > ontime AND currentTime < offtime
          PlxST.str(string("Zone 4 On  "))
          ZoneTimerFlag := ZoneTimerFlag | %0000_1000
        ELSE
          PlxST.str(string("ZOne 4 Off "))
          'ZoneTimerFlag[3] := %0
    
        ontime := Zone5Ontime[Today]
        offtime:= Zone5Offtime[Today]
        PlxST.char(13)
        PlxST.dec(ontime)
        PlxST.str(string("---"))
        PlxST.dec(offtime)
        PlxST.str(string("---"))
        
        IF currentTime > ontime AND currentTime < offtime
          PlxST.str(string("Zone 5 On  "))
          ZoneTimerFlag := ZoneTimerFlag | %0001_0000
        ELSE
          PlxST.str(string("ZOne 5 Off "))
          'ZoneTimerFlag[4] := %0
    
        ontime := Zone6Ontime[Today]
        offtime:= Zone6Offtime[Today]
        PlxST.char(13)
        PlxST.dec(ontime)
        PlxST.str(string("---"))
        PlxST.dec(offtime)
        PlxST.str(string("---"))
        
        IF currentTime > ontime AND currentTime < offtime
          PlxST.str(string("Zone 6 On  "))
          ZoneTimerFlag := ZoneTimerFlag | %0010_0000
        ELSE
          PlxST.str(string("ZOne 6 Off "))
          'ZoneTimerFlag[5] := %0
    
        PlxST.str(string("ZoneTimerFlags:  ")) 
        PlxST.bin(ZoneTimerFlag,8)
    
    
Sign In or Register to comment.