counter
Jeena99
Posts: 14
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
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
what should I do to count who many times the push button was pressed?
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
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
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
"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?
' {$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
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.
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?
So I cannot use OUTA or the rest of the registers.