Shop OBEX P1 Docs P2 Docs Learn Events
Measuring series of pulses with basic stamp, possible? — Parallax Forums

Measuring series of pulses with basic stamp, possible?

CuriousOneCuriousOne Posts: 931
edited 2012-10-23 23:47 in BASIC Stamp
Here's the input:

pulse with duration of 1ms, pause 3ms, 2nd pulse with duration 1-5ms. This is the input. it should be measured, and with beginning of the 2nd pulse, output pulse should be sent.

There's command PULSIN for that, but it has two states, when it measures high or low pulse, but how to switch it on the fly, in sequence?

Here's the code, is it correct?
STATE=1
DURATION=0
DO
PULSIN 1,STATE,DURATION ' READ THE FIRST PULSE
IF DURATION> 450 AND DURATION<550 THEN STATE=0 ' FIRST PULSE CAUGHT, LET'S MEASURE PAUSE
PULSIN 1,STATE,DURATION ' READ THE PAUSE BETWEEN PULSES
IF DURATION>1450 AND DURATION<1550 THEN STATE=1: PULSOUT 2,1000 ' PAUSE MEASURED, DO OUTPUT
LOOP

The syntax might be not exactly of basic stamp, but this is just demonstartion of the algorithm, will it work?

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-10-06 07:30
    PULSIN 1 is for measuring time HI when the previous state is LO: established state = LO, input goes HI then back LO. To do a PULSIN 1, the input must be LO to begin with.
    PULSIN 0 is for measuring time LO when the previous state is HI: established state = HI, input goes LO then back HI. To do a PULSIN 0, the input must be HI to begin with.
  • CuriousOneCuriousOne Posts: 931
    edited 2012-10-06 07:33
    Yes I know, and this is the reason why I asked here, unsure, how to measure both high and low pulse duration on single pin. Maybe put two pins in paralel ?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-10-06 07:35
    It's not possible.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-10-06 07:39
    Is it necessary to know the time_low between the pulse-1 and pulse-2?
  • CuriousOneCuriousOne Posts: 931
    edited 2012-10-06 08:02
    Yes, it is necessary, that is used to prevent false outputs.
  • CuriousOneCuriousOne Posts: 931
    edited 2012-10-06 08:28
    Dual input idea:

    we have two inputs, one "waits" for high, another for low signal
    "first" pulsin waits for high signal, as signal of specific duration arrives and ends, the "second" pulsin is started, which measures the duration of the low signal, if that low signal completes within required timeframe, and we already have the "proper" high pulse measured, we can fire output

    right?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-10-06 09:36
    right?
    No.

    The Stamp does one thing at a time.

    It doesn't make a difference if it's branched to two input pins.
    What matters is the PULSIN pin's state when the instruction is initiated.

    So, to start, the initial PULSIN 1 is completed (satisfied) when the pin goes LO again.
    The next thing to do is a PULSIN 0, but the trouble is that that pin is LO already.
    See?

    If it is LO already then it will have to wait for the pin to go HI and then on the following falling-edge it will begin the timing (provided that occurs before it times out.)
  • CuriousOneCuriousOne Posts: 931
    edited 2012-10-06 09:42
    Ok, what if do it in the next way?

    After first pulse completes, counter is launched, which increments untill the 2nd pulse begins, so we analyze timing of counter and use it to determine whenever the pulse between the impulses was of the correct duration.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-10-06 09:49
    Counter? What counter?

    Sounds like it's -- Propeller Time!
  • CuriousOneCuriousOne Posts: 931
    edited 2012-10-06 10:05
    aren't there a system timer available in basic stamp?

    No propeller :D Have stamp - have to use it, hobby budget is not stretchable :)

    And counter can be simple FOR-NEXT with adequate number of digits to count.
    Another solution will be to loop the signal from built-in oscillator to input and use COUNT command ? :)
  • CuriousOneCuriousOne Posts: 931
    edited 2012-10-06 10:09
    Ah I forgot, nothing happens while one command is executing. What if use polled interrupt ?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-10-06 10:14
    No, there isn't a "system timer".
    No interrupts either.
  • PublisonPublison Posts: 12,366
    edited 2012-10-06 10:14
    CuriousOne wrote: »
    aren't there a system timer available in basic stamp?

    No propeller :D Have stamp - have to use it, hobby budget is not stretchable :)

    I have to agree with PJ. It's Propeller time.

    Propeller at $7.99 not affordable?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-10-06 10:22
    He'd need a PropPlug, too.
    And shipping.
    Start-up costs, man.

    Or get a QuickStart.
    http://www.parallax.com/Store/Microcontrollers/PropellerDevelopmentBoards/tabid/514/ProductID/748/List/0/Default.aspx?SortField=ProductName,ProductName
    Nothing else (much) to buy.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-10-06 11:04
    PJ Allen wrote: »
    Or get a QuickStart.

    I second PJ's suggestion.

    If you don't have other things you want to purchase from Parallax, it might be less expensive to buy a QuickStart from PropellerPowered or GadgetGangster. They both have really low shipping costs (IIRC about $4). If you get if from PropellerPowered, you might want to consider also getting one of the inexpensive ultrasound sensors at the same time (for a fun reason to measure pulses).
  • PublisonPublison Posts: 12,366
    edited 2012-10-06 11:15
    Quick Start board can also be purchased at a some Radio Shacks.

    Jim
  • CuriousOneCuriousOne Posts: 931
    edited 2012-10-06 11:32
    The expense is not only value of $$ needed to spend, the time, that is needed to learn and deal with propeller, also counts. I think it'll be better if I do a small introduction to myself, why I've choosen basic stamp and not propeller, arduino, avr studio or any other.

    In late 80s, I was doing a lot of coding on Sinclair ZX Spectrum, using various BASIC language dialects and assembler as well. So, in general I have idea what is the machine code, structure of MCU and so on. Some of my code was so effective, that a scrolling marque generator, built with my Z80 code, was used by local tv studio for two decades, untill fire occured and it burned down :)

    So, now I decided to go back to programming, but as hobby. I started looking around, for various possibilities market offers these days. I've checked a lot of possibilities, picaxe, arduino, microbasic, avr studio, mikroe, propeller, basic stamp, etc. Amongst these, basic stamp looked more appealing than others, so I decided to stick with it. The main reason why I dislike propeller, is it's syntax and code appearance, looking like C, which I hate most.

    Regarding the current issue, what if do it in a such way?
    X=0: Y=0: Z=0: A=0: B=0: C=0: D=0
    DO
    IF INPUT(1)=1 THEN X=X+1 'READ THE FIRST PULSE, USE X VALUE AS LENGTH MEASUREMENT
    IF X>450 THEN Y=1 'IF LENGTH IS LONG ENOUGH TO SUSPECT CORRECT PULSE, LETS RECORD IT
    Z=Z+Y 'INCREMENT OF VARIABLE WHERE CORRECT PULSE LENGTH IS STORED
    IF Z>100 THEN Z=0 'IF INPUT PULSE LENGTH EXCEEDED THE CORRECT VALUE, THEN RESET CORRECT PULSE VARIABLE
    IF Z>50 AND Z<80 AND INPUT(1)=0 THEN A=1 'IF LENGTH OF FIRST PULSE WAS LONG ENOUGH, AND NOW NO PULSE, LET'S COUNT "SILENCE"
    B=B+A 'INCREMENT THE "SILENCE" VARIABLE
    IF B>1450 THEN C=1 'IF LENGTH OF "SILENCE" IS LONG ENOUGH TO SUSPECT CORRECT PULSE, LETS RECORD IT
    D=D+C  'INCREMENT OF VARIABLE WHERE CORRECT "SILENCE" LENGTH IS STORED
    IF D>300 THEN D=0 'IF INPUT "SILENCE" LENGTH EXCEEDED THE CORRECT VALUE, THEN RESET CORRECT "SILENCE" VARIABLE
    IF D>250 AND D<280 AND INPUT(1)=1 THEN OUT (2)=1: X=Y=Z=A=B=C=D=0 'IF "SILENCE" WAS ENOUGH AND WE SEE 2ND PULSE COMING, DO THE JOB
    LOOP
    

    The syntax is still weird, but doable, what do you think?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-10-06 12:25
    Before you dismiss the Prop completely, I'd like to encourage you to read some of Phil's excellent remarks about how the Prop can be easier to use than the BS2.
  • CuriousOneCuriousOne Posts: 931
    edited 2012-10-06 12:52
    Yes, I'll have a look, but my case is a bit different.

    1. I already studied a lot in basic stamp.
    2. When I look at basic stamp code at first time, I easilly can "see" what this code does. This never happened with Propeller or C, or some others.
    3. The main reason why I'm doing this is to push things (and myself) to the edge. I mean, to do "undoable" things on simple hardware. This is much more exciting than just including readily available libraries and busting out ready code.
    4. To be even more specific, all I'm trying to do, is to build a custom slave flash, which will only trigger after specific flash series received from the main flash. The factory-made unit which has all these functions, costs $39.99 on amazon, so instead of buying propeller, I can go and buy that flash directly, but I don't think this will be a fair move.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2012-10-06 14:21
    CuriousOne wrote: »
    ...
    3. The main reason why I'm doing this is to push things (and myself) to the edge. I mean, to do "undoable" things on simple hardware. This is much more exciting than just including readily available libraries and busting out ready code.

    I can appreciate that motivation!
    After first pulse completes, counter is launched, which increments untill the 2nd pulse begins, so we analyze timing of counter and use it to determine whenever the pulse between the impulses was of the correct duration.

    Look at the RCTIME command. It sort of provides the timer you are looking for. Your program might execute a PULSIN to capture the initial 1ms, then execute an RCTIME to find how long it takes to the next edge. The difficulty with that the timer is not launched right at the end of the previous PULSIN, rather, it takes some time for the interpreter to fetch and execute the RCTIME command. Not to mention the math that tests the length of the first pulse. You would have to add those fixed times to the result returned. How much? The times you are looking at are at the edge of the BASIC Stamp universe. Leave out the math for the moment. The RCTIME command itself eats up about 220µs. So the 3ms rising edge should happen with a count value of between 1340 and 1540. The math can eat up a lot more time, about 500µs for one IF THEN statement, and more when there are multiple conditions. What is more, the time for an IF THEN is different depending on whether the conditions are true or false. There is a table of approximate times for different Stamp instructions posted here. Note that the BASIC Stamp p or px would be quite a bit faster.

    If all the conditions are met, how quickly does the slave flash have to fire? It is challenging, but don't let that stand in your way! If a delay of a few milliseconds is okay, then all the math could be done after the rising edge of the second pulse.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-10-06 14:57
    If a delay of a few milliseconds is okay, then all the math could be done after the rising edge of the second pulse.
    CuriousOne wrote:
    "...pulse with duration of 1ms, pause 3ms, 2nd pulse with duration 1-5ms."
    -- oo -- oo --
    CuriousOne wrote:
    "I mean, to do "undoable" things on simple hardware. This is much more exciting than just including readily available libraries and busting out ready code."
    vs.
    CuriousOne wrote:
    "Regarding the current issue, what if do it in a such way?"

    Why not try that and see how it goes?
  • PublisonPublison Posts: 12,366
    edited 2012-10-06 16:39
    Duane Degn wrote: »
    Before you dismiss the Prop completely, I'd like to encourage you to read some of Phil's excellent remarks about how the Prop can be easier to use than the BS2.

    While I haven't read Phil's "excellent remarks" thread, I thought I would throw this out.

    When I was starting with the Propeller, I was drawn to the BS2 Funtions Object.

    It allowed me to make the transition from BS2 to Propeller very smoothly and quickly.

    I learned a lot from that Object.

    Give it a look see.
  • CuriousOneCuriousOne Posts: 931
    edited 2012-10-06 21:38
    I can't try now, since currently I'm away from home and hardware, so only can "work" in theory.
  • vaclav_salvaclav_sal Posts: 451
    edited 2012-10-09 09:31
    I wrote a BS2e program which reads WWVB timming pulses of three different length ( 200 to 800 ms) every second. I used simple loop to monitor just change of single pin and than I process the resulting state. I had to do lots of tweaking to measure the pulses with reasonable tollerance. It probably would not work for "short" 1 ms pulse.

    In the process I found little use for PULSIN for such task.
  • CuriousOneCuriousOne Posts: 931
    edited 2012-10-12 10:12
    When I bought the basic stamp kit, I also bought mikrobasic pro kit + easy pic 7 dev board from mikroe.com. But I was not using it, since I don't like to spread around different platforms. Now as I see the basic stamp is not enough for me, I decided to juice up that system from mikroe. Boy it stinks! absolutelly buggy and poorly documented compiler. You can check some text here, it may be used as micro review for mikrobasic pro from mikroe.
    http://www.mikroe.com/forum/viewtopic.php?f=97&t=50923&p=198310#p198255
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2012-10-12 12:23
    Spin is different, yes, but not too hard to read, and the support here is excellent. In a nutshell, your program core in Spin would go something like this, assuming an 80MHz operating frequency:
    [SIZE=1][FONT=courier new]waitpeq(1,0,0)          ' wait for pin p0 to go high
    xt := cnt               ' capture the system count
    waitpeq(0,0,0)          ' wait for p0 to go low
    yt := cnt               ' at time
    pulse1 := (yt - xt)/80  ' duration of pulse in microseconds
    waitpeq(1,0,0)          ' wait for p0 high again
    zt := cnt               ' at time
    space1 := (zt - yt)/80  ' duration of low in microseconds
    if pulse1>900 and pulse1<1100 and space1>2900 and space1<3100
      outa[2] := 1           ' the output flash
      waitcnt(160000 + cnt)  ' 2 millisecond pulse
      outa[2] := 0[/FONT][/SIZE]
    

    There will be time lags for the interpreter to do its work, but the delay in the propeller will be 10s of microseconds as opposed to 100s of microseconds as it is on the Stamp. And there are easy ways to tighten it up on the Prop. Is that too unreadable, CuriousOne?
  • CuriousOneCuriousOne Posts: 931
    edited 2012-10-12 12:57
    It's relatively easy to read, when it has description next to each line. But, by meaning "see the code", I mean that I can see what this particular code does, even if it does not have description next to it.

    Below I'll try to illustrate, how above code (without description) will look for me (line by line):

    Declare array of variables
    xt variable equals cnt variable
    declare another array of variables
    pulse variable equals (yt-xt)/80
    zt variable equals cnt variable
    space1 variable equals (zt-yt)/80

    below code seems to be what it actually does (understandable)
  • CuriousOneCuriousOne Posts: 931
    edited 2012-10-22 06:57
    Finally, get back to hardware.

    The above code was implemented (with corrections, of course), and it seems to be working - tested with pushbutton as input pulse generator. In nearest days, actual hardware will be connected to try and measure real signals.
  • HumanoidoHumanoido Posts: 5,770
    edited 2012-10-23 23:47
    I use both Propeller chips and BASIC Stamps. As a matter of fact, some projects for the Propeller chip are being moved back to the BASIC Stamp. It has happened - what is developed in 5 minutes on the BASIC Stamp takes 5 days on the Propeller. This is in part, due to the great availability of documentation and resources for the Stamp. When the Stamp has high performance within the range of timing and function suited for the app, and rapid development time is a factor, it becomes the usable choice. When the app fits, the cost of a Stamp over a Propeller equates to shorter development time.
Sign In or Register to comment.