Shop OBEX P1 Docs P2 Docs Learn Events
n00b question - best way to see if circuit is complete — Parallax Forums

n00b question - best way to see if circuit is complete

tbaretbare Posts: 13
edited 2011-05-24 11:32 in BASIC Stamp
First post - be nice

Also, first microcontroller programming - be real nice. ;)

I'm working on a simple project that takes a signal from a piece of machinery, and captures it in a VB .net app.

basically, the machine closes a circuit when i want to capture the counter.

I've got everything working, but want to know if there's a better way to capture a simple "circuit closed" event.

here's what i've got:
' {$STAMP BS2}
' {$PBASIC 2.5}

btnwrk  VAR Byte
Btn     PIN 3
counter VAR Byte

Main:
BUTTON Btn, 1, 255, 20, btnwrk, 0, No_Press : counter=counter+1 'Add One to variable counter

DEBUG DEC counter ' Display positive count in the debug window

No_Press:
GOTO Main 'Start back at the beginning
Is there a better way than using "BUTTON" to do what i want? (i was using a button for my testing, but now i'm literally just touching wires, and it's working...)

Thanks!

Comments

  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-05-23 14:59
    Welcome to the Parallax Fourm

    Try this code and see if this works for

    One thing nice about this code routine is that if the input dose not stay HIGH for the hole count then the routine is not run

     
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
    chg PIN 1                                [COLOR=seagreen] '  To Input Pin[/COLOR]
     
    cntr VAR Byte                           [COLOR=seagreen]'  Loop Counter [/COLOR]
     
                                                    [COLOR=seagreen]'  The table is IF Chg = 1 Then Loop = 100 If Chg = 0 THEN Loops = 0[/COLOR]
     
    [COLOR=black]DO[/COLOR]
    cntr = cntr + 1 * Chg
    IF (cntr = 100) THEN EXIT
    LOOP
     
    

    OR

    One thing nice about this code routine is that if the input dose not stay LOW for the hole count then the routine is not run
     
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
    chg PIN 1                                [COLOR=seagreen] ' To Input Pin[/COLOR]
     
    cntr VAR Byte                          [COLOR=seagreen] ' Loop Counter [/COLOR]
     
    [COLOR=seagreen]                                          '  The table is IF Chg = 0 Then Loop = 100 If Chg = 1 THEN Loops = 0[/COLOR]
     
    [COLOR=black]DO[/COLOR]
     
     cntr = cntr + 1 * (1 - Chg)
        IF (cntr = 100) THEN EXIT
     
    LOOP
     
    

    OR

    This might work for you as far as what BOLD type
    '
    ' ===========================================================================================
    '
    '   File...... State Machine Logic For InPut Pin
    '   Purpose... To Count Input Pulses From Flow Meter
    '   Author.... Tracy Allen
    '   E-mail.... [URL="http://www.emesystems.com/"]www.emesystems.com[/URL]
    '   Started... 5/16/09
    '   Updated...
    '
    '   {$STAMP BS2}
    '   {$PBASIC 2.5}
    '
    ' I want to Thank Tracy For sharing this Routine for a Project that I am working on
    ' ===========================================================================================
    '
    ' -----[ Program Description ]---------------------------------------------
    '
    ' There will be 78.4 pulses per gallon and you are looking at a flow rate
    ' of about 30 gallons per minute.
    ' That will be 2352 pulses per minute, OR about 40 per second.
    ' That gives about 25 milliseconds from pulse TO pulse FOR the Stamp TO DO processing.
    ' That is really quite a Bit of time IF the Stamp does NOT have much ELSE TO DO WHILE
    ' counting.
    ' This is state machine logic.
    ' I figure that loop will execute in about 3 milliseconds,
    ' so that is plenty of time within the 25 ms allowed.
    ' It actually has TO catch both the opening AND the closing of the switch,
    ' within the 25 millisecond period.
    '
    [B]' The line bellow is explained Here ........>>>>>>>[/B]
    [B]'[/B]
    [B]'                   xx = xbit ^ x0 & x0[/B]
    [B]'[/B]
    [B]' ........>>>> Here[/B]
    [B]' The value of xx will be either 1 or 0, depending on the current value of the input (xbit)[/B]
    [B]' ANd the prior value of the INPUt (x0).[/B]
    [B]' xx will be 1 only IF there is a change from prior time TO now (xbit ^ x0 = 1) AND the[/B]
    [B]' prior time it was HIGH ( & x0). So xx will be 1 only when the INPUT changes from 1 TO 0.[/B]
    [B]' Note that the statement x0 = xbit subsequently updates the prior value.[/B]
    [B]' The DO:LOOP can execute many many times with xx=0.[/B]
    [B]' It is only during one time around that it becomes xx=1, when the INPUT transitions from 1-->0.[/B]
    '
    '
    '
    '============================================================================================
    '
    ' -----[ I/O Definitions ]-------------------------------------------------
    '
    valve     PIN    7
    control   PIN    8
    '
    ' -----[ Constants ]-------------------------------------------------------
    '
    ' -----[ Variables ]-------------------------------------------------------
    xbit      VAR     Bit       ' current state of input pin
    x0        VAR     Bit       ' previous state
    xx        VAR     Bit       ' change of state from high to low
    N         VAR     Word      ' number of pulses
     
    ' -----[ Program Code ]----------------------------------------------------
     
    N=0
    DO
    HIGH control
    IF n > 0 THEN LOW valve   ' This line is to Demo Test this Code Routine
    control = IN0
      xbit = IN0
      xx = xbit ^ x0 & x0
      x0 = xbit
      N = N + xx
     DEBUG HOME, DEC ? xbit,
                 DEC ? xx,
                 DEC ? x0,
                 DEC ? N
     
                             ' pulses Need
    LOOP UNTIL N = 20
    HIGH control
    HIGH valve
     
    ' -----[ Subroutines ]-----------------------------------------------------
    
    I hope this help
  • tbaretbare Posts: 13
    edited 2011-05-24 06:30
    Thanks for the tips...

    unfortunately, I'm not sure how long the circuit stays closed, so putting a timer in there is a potential issue (unless i'm missing something...)

    admittedly, i'm not an electrician, and this is my first bout with bread boards, resistors, etc... :) gotta learn sometime, though... may as well get paid for it ;)
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-05-24 11:32
    The timer is there if there is noise on your input you can have some time to see if your input stays high or low depending which routine you use I use this routine all the time for inputs
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
    chg PIN 1                                '  To Input Pin
     
    cntr VAR Byte                           '  Loop Counter 
     
                                                    '  The table is IF Chg = 1 Then Loop = 100 If Chg = 0 THEN Loops = 0
     
    DO
    cntr = cntr + 1 * Chg
    IF (cntr = 100) THEN EXIT
    LOOP
    

    But alot of the time I use

    IF (cntr = 50) THEN EXIT <.........or less 10 maybe

    One thing you can use is an optic sensor like a 4N25 and then you may not need this routine at all but I still you the routine any way my self
Sign In or Register to comment.