PDA

View Full Version : counter



Jeena99
11-16-2009, 11:09 PM
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

Mike Green
11-16-2009, 11:14 PM
It always shows 1 because you wrote "y = x + 1" and x is always 0.

Jeena99
11-16-2009, 11:18 PM
Thank u 4 replying

what should I do to count who many times the push button was pressed?

Jeena99
11-16-2009, 11:32 PM
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 Green
11-16-2009, 11:38 PM
You have a nice program, but it won't do what you say you want. Try running it without pushing the button.

Jeena99
11-17-2009, 12:47 AM
yeh u r right it does not display 0

Mike Green
11-17-2009, 01:12 AM
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

Jeena99
11-17-2009, 02:14 AM
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

Jeena99
11-17-2009, 02:28 AM
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?

Jeena99
11-17-2009, 02:48 AM
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 Green
11-17-2009, 02:57 AM
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.

Jeena99
11-17-2009, 03:43 AM
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 Green
11-17-2009, 04:12 AM
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.

Jeena99
11-17-2009, 06:07 PM
Yep but I have to use the pins: 0-1-4-5
So I cannot use OUTA or the rest of the registers.