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

Counting

Joe SchisslerJoe Schissler Posts: 22
edited 2012-04-20 19:04 in BASIC Stamp
Here is my program. how can I make it faster and can I label the debug sereen: People in total, People out, people in store.
' {$STAMP BS2}
' {$PBASIC 2.5}
'Two counters one for people entering and one for leaving.
'The difference "C" is the number of people in the store
CounterA VAR Word 'People entering store total
CounterB VAR Word 'People leaving store
C VAR Word 'People in store
DO
DEBUG HOME
DEBUG CLS
DEBUG DEC CounterA, CR
DEBUG DEC COUNTERB, CR
DEBUG DEC C, CR
IF (IN3 = 1) THEN
CounterA = CounterA + 1 'People entering store total count
PAUSE 500
ENDIF
IF (IN4 = 1) THEN
CounterB = CounterB + 1 'People leaving store total count
PAUSE 500
ENDIF
C = CounterA - CounterB 'People in the store
PAUSE 1000

LOOP

END

Comments

  • FranklinFranklin Posts: 4,747
    edited 2012-04-16 07:33
    You could make it faster by removing the pauses.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-16 07:39
    You can include string constants in the DEBUG statements to label the debug display. Look at the description of the DEBUG statement in the Basic Stamp Syntax and Reference Manual or the Stamp Editor's help files.
  • Joe SchisslerJoe Schissler Posts: 22
    edited 2012-04-16 15:31
    Without the pauses the debug screen just flutters the numbers
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-16 15:56
    If you leave out the CLS and use the fixed format DEC prefix, the debug display won't flicker so much and you can eliminate or reduce the PAUSEs

    DEBUG HOME, DEC5 CounterA, CR
    DEBUG DEC5 CounterB, CR
    DEBUG DEC5 C, CR

    You could also put the labels on the line above by having an initialization line before the DO

    DEBUG HOME, CLS, "Enter Leave Total"

    Then the display stuff would be

    DEBUG HOME, LF, DEC5 CounterA, " ", DEC5 CounterB, " ", DEC5 C
  • Joe SchisslerJoe Schissler Posts: 22
    edited 2012-04-17 09:35
    Works good but i'm getting a double count sometimes. Is there a one shot setup or the button setup if you can use it on more than one input.
    Thanks joe
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-17 10:08
    You can use the BUTTON statement to do debouncing if your inputs are switches. If they're not switches, you could do edge detection where you keep track of the previous state of each input and count if the previous state was 0 and the current state is 1 like this

    newIn3 = in3
    if (oldIn3 = 0) and (newIn3 = 1) then
    countA = countA + 1
    endif
    oldIn3 = newIn3

    Both oldIn3 and newIn3 are defined as bit variables and oldIn3 is initialized to zero.
  • Joe SchisslerJoe Schissler Posts: 22
    edited 2012-04-17 10:20
    How do i get the button statement to work on two inputs. I could only get it to work on one. Sorry to be such a pain!
    Joe
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-17 10:44
    You need one BUTTON statement for each input. They work somewhat like the IF THEN statements. There's one label they goto when the button is pushed and falls through otherwise. Sort of like this

    Display:
    DEBUG HOME, LF, DEC5 CounterA, " ", DEC5 CounterB, " ", DEC5 C
    CheckButton:
    BUTTON 3, 1, 255, 0, Work3, 1, Pushed3 ' Check buttons
    BUTTON 4, 1, 255, 0, Work4, 1, Pushed4
    GOTO CheckButton ' No need to display if nothing changed
    Pushed3:
    CountA = CountA + 1
    C = C + 1
    GOTO Display ' No need to check IN4 this cycle ... will be checked after display update
    Pushed4:
    CountB = CountB + 1
    C = C + 1
    GOTO Display

    I think I got the BUTTON parameters right. Check the Stamp Manual description. We're debouncing only, no auto-repeat. Work3 and Work4 are byte variables.
  • Joe SchisslerJoe Schissler Posts: 22
    edited 2012-04-18 00:09
    This is whatI have nd it dosen't work:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    Btn1 PIN 3
    Btn2 PIN 4
    btnWrk1 VAR Byte
    btnwrk2 VAR Byte

    DO
    PAUSE 5
    BUTTON Btn1, 1, 255, 255, btnWrk1, 0, No_Press
    DEBUG "*"
    BUTTON Btn2, 1, 255, 255, btnWrk2, 0, No_Press
    DEBUG "#"
    No_Press:
    LOOP
  • Joe SchisslerJoe Schissler Posts: 22
    edited 2012-04-20 19:04
    HERE IS MY FINAL PROGRAM WORKS GREAT THANKS FOR ALL YOUR HELP:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    'Two counters one for people entering one door, and one for
    'People leaving through the other door
    'The difference "C" is the number of people in the store
    CounterA VAR Word 'People entering store total
    CounterB VAR Word 'People leaving store
    C VAR Word 'People in store
    DO
    DEBUG HOME, CR
    DEBUG DEC3 CounterA,"-ENTERED STORE - Daily Total",CR
    DEBUG DEC3 COUNTERB,"-LEFT STORE", CR
    DEBUG DEC3 C,"-ARE IN STORE", CR
    IF (IN3 = 1) THEN
    CounterA = CounterA + 1 'People entering store total count
    PAUSE 500
    ENDIF
    IF (IN4 = 1) THEN
    CounterB = CounterB + 1 'People leaving store total count
    PAUSE 500
    ENDIF
    C = CounterA - CounterB 'People in the store
    PAUSE 20
    LOOP:
    END
Sign In or Register to comment.