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

counter

Jeena99Jeena99 Posts: 14
edited 2009-11-17 11:07 in BASIC Stamp
Hello Everybody

I want to display the number that a push button is pressed
I wrot the following code, but I do not know why is it always showing 1

' {$STAMP BS2}
' {$PBASIC 2.5}
x· VAR Byte
y VAR Byte
x=0
check:
y=x+1
DO
PAUSE 200
LOOP UNTIL IN3=0
DEBUG DEC y
GOTO check

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-16 16:14
    It always shows 1 because you wrote "y = x + 1" and x is always 0.
  • Jeena99Jeena99 Posts: 14
    edited 2009-11-16 16:18
    Thank u 4 replying

    what should I do to count who many times the push button was pressed?
  • Jeena99Jeena99 Posts: 14
    edited 2009-11-16 16:32
    Oh
    I`ve found the answer
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    x VAR Byte
    y VAR Byte


    DO
    y=y+1
    DO
    PAUSE 200
    LOOP UNTIL IN3=0

    DEBUG DEC y
    LOOP
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-16 16:38
    You have a nice program, but it won't do what you say you want. Try running it without pushing the button.
  • Jeena99Jeena99 Posts: 14
    edited 2009-11-16 17:47
    yeh u r right it does not display 0
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-16 18:12
    How do you think you might change what you have to
    1) Detect that the button is pushed
    2) Increment the counter
    3) Wait for the button to be "not pushed"

    You can also change the order of the above to #3, #1, #2 or #1, #3, #2
  • Jeena99Jeena99 Posts: 14
    edited 2009-11-16 19:14
    What if I want to display each bit of the counter .
    And I only need the counter to count from 0-15 so I defined the counter as Nib.
    I have tried the following but it did not work
    Mike What do u suggest?
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    y VAR Nib
    zerothBit VAR Bit
    firstBit VAR Bit
    secondBit VAR Bit
    thirdBit VAR Bit

    zerothBit=y.BIT0
    firstBit=y.BIT1
    secondBit=y.BIT2
    thirdBit=y.BIT3


    DO
    y=y+1
    DO
    PAUSE 100
    LOOP UNTIL IN3=0

    DEBUG DEC ? zerothBit
    DEBUG DEC ? firstBit
    DEBUG DEC ? secondBit
    DEBUG DEC ? thirdBit

    LOOP
  • Jeena99Jeena99 Posts: 14
    edited 2009-11-16 19:28
    Mike you also said in your first reply :

    "It always shows 1 because you wrote "y = x + 1" and x is always 0."
    I agree with u but I donot know why did not work? why it was not changed?
  • Jeena99Jeena99 Posts: 14
    edited 2009-11-16 19:48
    To display each bit I have done the following and it worked
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    counter VAR Nib
    pzero VAR Nib
    pone VAR Nib
    ptwo VAR Nib
    pthree VAR Nib
    DO
    counter=counter+1
    DO
    PAUSE 100
    LOOP UNTIL IN3=0
    pzero=counter & %0001
    pone=counter & %0010
    ptwo=counter & %0100
    pthree=counter& %1000
    DEBUG DEC ? counter
    DEBUG BIN ? pzero
    DEBUG BIN ? pone
    DEBUG BIN ? ptwo
    DEBUG BIN ? pthree

    LOOP
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-16 19:57
    Two things ...

    1) You can include expressions in your DEBUG statements so you don't need the extra variables like:

    DEBUG "count ",DEC counter, " %",BIN1 counter.bit3, " ", BIN1 counter.bit2, " ", BIN1 counter.bit1, " ", BIN1 counter.bit0

    or

    DEBUG "count ",DEC counter, " %", BIN4 counter

    2) I think you're confused between the computer notion of "executing a statement" and the math concept of a formula stating a relationship between two expressions. When you say "y = x + 1", on a computer it means "compute the value x + 1 and set the variable y to that value and do this all once right now". In the math concept, you're expressing a relationship between y and x that will hold true indefinitely.
  • Jeena99Jeena99 Posts: 14
    edited 2009-11-16 20:43
    Aha Thank u very Much Mike for your explanation.
    I still want to do another thing:
    I want 4 LEDs to display the value of the counter in binary.
    Do u have any suggessions?
    I just could not do it right
    Can u help me plz?
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-16 21:12
    If you choose a set of I/O pins that's on a boundary of 4 (0-3, 4-7, 8-11, or 12-15) it's easy. The output register has names for groups of 4 and you can assign a value to that group (like OUTB = counter). Look at pages 81-84 of the BASIC Stamp Syntax and Reference Manual. The "What's a Microcontroller?" tutorial manual shows how to hook up LEDs to Stamp I/O pins. Remember to set the corresponding direction register bits properly.
  • Jeena99Jeena99 Posts: 14
    edited 2009-11-17 11:07
    Yep but I have to use the pins: 0-1-4-5
    So I cannot use OUTA or the rest of the registers.
Sign In or Register to comment.