Shop OBEX P1 Docs P2 Docs Learn Events
How to do this simple task — Parallax Forums

How to do this simple task

PVJohnPVJohn Posts: 60
edited 2005-11-12 07:06 in BASIC Stamp
I'm trying to TOGGLE PIN 1 after 3 counts at PIN 0, but I'm having a trouble with PBASIC. Please help.

' {$STAMP BS2px}
' {$PBASIC 2.5}

SpeedIN········ PIN···· 0········· ' Input on P0
SpeedOUT····· PIN···· 1········· ' OUTput on P1

DIR1 = 1····························· ' pin 1 in output mode
SpeedOUT = 0····················· ' pin 1 output driver low

OneSec······ CON···· $37B··········· ' / 0.287
Capture······CON···· 1000··········· ' 1 second
cycles········· VAR···· Word··········· ' counted cycles

Main:
DO
··· COUNT SpeedIN, (Capture */ OneSec), cycles
····· IF SpeedIN = 3 THEN
······· TOGGLE 1
······· SpeedIN = 0
····· ELSE
········ IF SpeedIN < 3 THEN Main
····· END IF
LOOP
END



799 x 597 - 67K

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-10 13:23
    The problem is that you might get more than three inputs in the one second period.· Maybe this will work:

    testIn·· VAR·· Bit
    oldIn····VAR·· Bit

    Main:
    · DO
    ··· testIn = SpeedIn ^ oldIn & SpeedIn
    ··· oldIn =·testIn
    ··· cycles = cycles + testIn
    ··· IF (cycles = 3) THEN
    ····· TOGGLE SpeedOut
    ····· cycles =·0
    ··· ENDIF
    · LOOP

    The first line scans the input and watches for a 0-to-1 change on the input, setting testIn to 1 when (and only when) that happens.· The rest is pretty straightforward.·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • PVJohnPVJohn Posts: 60
    edited 2005-11-12 07:06
    Thanks John, that helped.

    PVJohn
Sign In or Register to comment.