Shop OBEX P1 Docs P2 Docs Learn Events
timeCounter — Parallax Forums

timeCounter

j.m.krellj.m.krell Posts: 14
edited 2007-02-06 03:03 in BASIC Stamp
Hi,

I'm working on a project (drowning prevention system) and was wondering if there is a way to have more than one timeCounter or a way to keep time (more than one) on how long a pin is high or low.· If this isn't detailed enough post back an I'll delve into more details.· Thank you very much, I greatly appreciate it.

Jordan

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-01-23 16:04
    Jordan,

    Without an external counter you wouldn’t have accuracy, but internally you could run two passive counters within the loop that count while the pin is in the target state and stop when it changes and you can get a relative value. Of course, this really depends on the length of time and resolution required. Do you have any more details?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • j.m.krellj.m.krell Posts: 14
    edited 2007-01-23 17:10
    I'm looking at 30 seconds for a maximum and the program runs as a if IN0 = 0 then add one, and if it IN0 = 1 then timeCounter = 0 (or something similar) In my drowning prevention system each headband would need two input pins (one for a 15 sec. and another for a 30 sec.) I would like to add additional headbands as I can currently do one headband with a single timeCounter, is there a way I can have more at least two independent timeCounters. On any solutions would it be possible to show where I could find an example. Thank you very much, I greatly appreciate it.

    Jordan M. Krell
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-01-23 20:27
    Jordan,

    It sounds like you will need an external timer or RTC such as the DS1302. When reset to 00:00:00 you can use it as a counter. You can have multiple counters in variables based on the contents read from the RTC. They don’t even need to be relative to 00:00:00 if you convert to raw and it will be easier to compare values. You limit will be 65535 seconds minus any offset from 00:00:00. I could go into a little more detail if you need. I hope this helps. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-01-24 00:37
    Hi Jordan,

    I have trouble understanding what you want to do or how this system would work. My guess is that these timers do not have to have subsecond accuracy and that you can probably get more than one timer running at the same time in software. Here is a little program that counts up by seconds on two independent counters. It counts up if the associated pin p0 is at zero and resets to zero if the pin is 1.

    x0 VAR Bit
    x1 VAR Bit
    cnt0 VAR Word
    cnt1 VAR Word
    
    DO
      x0 = ~ in0    ' complement of input 0
      x1 = ~ in1   ' ditto input 1
      cnt0 = cnt0 + x0 * x0   ' channel 0 increment when p0=0, reset when p0=1
      cnt1 = cnt1 + x1 * x1   ' ditto, channel 1
      IF cnt0 = 30 THEN DEBUG CR,"Alarm0!"
      IF cnt1 = 15 THEN DEBUG CR,"Alarm1!"
      PAUSE 995   ' to make it 1 second ticks, hold here 995 milliseconds
    LOOP
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-01-24 11:40
    Jordan -

    Just out of curiosity, what kind of sensors or devices are attached to the two input pins to provide these signals? Depending on your answer, there may be a potential problem with the way you presently have it designed. It's by NO means an insurmountable problem, however.

    Many years ago I designed a pool use alarm, that's why I ask. Once SET, it would detect anyone who might have fallen or jumped into the pool. It really prevented unauthorized use, and was NOT a pool safety device as yours seems to be.

    regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • j.m.krellj.m.krell Posts: 14
    edited 2007-01-29 15:41
    I'm using a rf transmitter and receiver and when the child is underwater for so long an alarm would go off.· I'm looking at a small goggle mount module with the rf inside of it.· When the receiver is receiving a signal there is no voltage being sent out and when there is no signal the voltage output is at 3V.· I'm running the output of the receiver into the input of the microcontroller
  • j.m.krellj.m.krell Posts: 14
    edited 2007-01-29 15:45
    With that program is it possible to count up if pin0 is at 1 and resets the counter to 0 when pin0 is at 0.· Also is it possible to do this with say up to 8 counters?· Thank you very much.·

    Jordan
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-01-29 17:21
    http://www.rhombus-tek.com/co-processors.html

    See "Simple Multi-Tasking" on that page. The co-processor (a $12, 8-pin processor) would enable you to set a 10 mSec 'tick'. In your BS2 code, you can then use that 'tick' to implement multiple counters. This will get you a much more accurate timer than the BS2 alone.

    And if 10 mSec doesn't give you enough time, you can implement a 20 mSec or 50 mSec 'tick' instead.
    I've used this, and it's really liberating to have a 'hard' real-time tick source for the BS2.

    Post Edited (allanlane5) : 1/29/2007 6:41:45 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-01-29 17:24
    Jordan,

    · This could be done, however you should know that the more tasks you add inside your loop the more it will affect the number of counts within the same period of time. Also keep in mind that 8 counters using Word variables will consume 16 of your 26 variables. A highly simplified sample of how this could be done is listed below. This shows 4 pins/counters but you could add more at the cost of variable space and speed. Bear in mind this doesn’t cover reading the variables at any given time to see how high they go before being reset. I presume this is something you have already thought out. I hope this helps. Take care.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    counter0   VAR    Word
    counter1   VAR    Word
    counter2   VAR    Word
    counter3   VAR    Word
    do
      IF IN0 = 0 THEN
        counter0 = counter0 + 1
      else
        counter0 = 0
      endif
      IF IN1 = 1 THEN
        counter1 = counter1 + 1
      ELSE
        counter1 = 0
      ENDIF
      IF IN2 = 1 THEN
        counter2 = counter2 + 1
      ELSE
        counter2 = 0
      ENDIF
      IF IN3 = 1 THEN
        counter3 = counter3 + 1
      ELSE
        counter3 = 0
      ENDIF
    loop
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-01-29 17:26
    Jordan,
    The program counts up if pin p0 is at 1 and resets the counter to 0 when pin0 is at 0. Same for pin p1. Here is a program that does it for 8 pins, p0 to p7:

    x VAR Byte
    cnt0 VAR Byte
    cnt1 VAR Byte
    cnt2 VAR Byte
    cnt3 VAR Byte
    cnt4 VAR Byte
    cnt5 VAR Byte
    cnt6 VAR Byte
    cnt7 VAR Byte
    
    DO
      x = ~ inL    ' complement of 8 inputs p0 to p7
      cnt0 = cnt0 + x.bit0 * x.bit0   ' channel 0 increment when p0=0, reset when p0=1
      cnt1 = cnt1 + x.bit1 * x.bit1   ' ditto, channel 1
      cnt2 = cnt2 + x.bit2 * x.bit2   ' ditto, channel 2
      cnt3 = cnt3 + x.bit3 * x.bit3   ' ditto, channel 3
      cnt4 = cnt4 + x.bit4 * x.bit4   ' ditto, channel 4
      cnt5 = cnt5 + x.bit5 * x.bit5   ' ditto, channel 5
      cnt6 = cnt6 + x.bit6 * x.bit6   ' ditto, channel 6
      cnt7 = cnt7 + x.bit7 * x.bit7   ' ditto, channel 7
      IF x THEN GOSUB alarms  ' some pin is low
      PAUSE 995   ' to make it 1 second ticks, hold here 995 milliseconds
    LOOP
    
    alarms:
      IF cnt0 = 30 THEN DEBUG CR,"Alarm0!"
      IF cnt1 = 15 THEN DEBUG CR,"Alarm1!"
      ' and so on
      RETURN
    



    Are your radios going to work underwater?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • j.m.krellj.m.krell Posts: 14
    edited 2007-01-29 17:57
    The radio won't transmit through the water as the rf frequency is 2.4 Ghz and water absorbs waves at 2.4 Ghz
  • j.m.krellj.m.krell Posts: 14
    edited 2007-02-05 18:17
    With this program is there a way to make it so it counts up when the P0 = 1 and resets when P0 = 0, I ran it and it signaled an alarm when P0 = 0 and reset when P0 = 1

    Thank you very much
    Jordan

    x·VAR·Byte
    cnt0·VAR·Byte
    cnt1·VAR·Byte
    cnt2·VAR·Byte
    cnt3·VAR·Byte
    cnt4·VAR·Byte
    cnt5·VAR·Byte
    cnt6·VAR·Byte
    cnt7·VAR·Byte
    DO
    ··x·=·~·inL····'·complement·of·8·inputs·p0·to·p7
    ··cnt0·=·cnt0·+·x.bit0·*·x.bit0···'·channel·0·increment·when·p0=0,·reset·when·p0=1
    ··cnt1·=·cnt1·+·x.bit1·*·x.bit1···'·ditto,·channel·1
    ··cnt2·=·cnt2·+·x.bit2·*·x.bit2···'·ditto,·channel·2
    ··cnt3·=·cnt3·+·x.bit3·*·x.bit3···'·ditto,·channel·3
    ··cnt4·=·cnt4·+·x.bit4·*·x.bit4···'·ditto,·channel·4
    ··cnt5·=·cnt5·+·x.bit5·*·x.bit5···'·ditto,·channel·5
    ··cnt6·=·cnt6·+·x.bit6·*·x.bit6···'·ditto,·channel·6
    ··cnt7·=·cnt7·+·x.bit7·*·x.bit7···'·ditto,·channel·7
    ··IF·x·THEN·GOSUB·alarms··'·some·pin·is·low
    ··PAUSE·995···'·to·make·it·1·second·ticks,·hold·here·995·milliseconds
    LOOP
    alarms:
    ··IF·cnt0·=·30·THEN·DEBUG·CR,"Alarm0!"
    ··IF·cnt1·=·15·THEN·DEBUG·CR,"Alarm1!"
    ··'·and·so·on
    ··RETURN
    Are your radios going to work underwater?

    Tracy Allen
    www.emesystems.com
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-02-06 03:03
    In the DO loop use
    x = inL ' state of 8 inputs p0 to p7
    instead of
    x = ~ inL ' complement of 8 inputs p0 to p7

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.