Shop OBEX P1 Docs P2 Docs Learn Events
Phototransmitter Counter — Parallax Forums

Phototransmitter Counter

I'm trying to write a pBasic code to Count stuff using a Phototransmitter.
The problem is, it's not working the way I want it to.
' {$STAMP BS2}
' {$PBASIC 2.5}

' Only works if Bright Light touches it

' Phototransister
' 0 = Ambient Fluorescent Light
' 1 = Bright Light

Counter VAR Word
Total VAR Word

Total = 0

DO
PAUSE 1000
DEBUG CLS

Counter = IN5
IF Counter = 1 THEN Total = Total + 1

DEBUG ? Counter
DEBUG ? Total
LOOP

I'm trying to make it so even if the Light continues to shine on the phototransmitter (1), it doesn't keep counting. How do I fix this?

Comments

  • kwinnkwinn Posts: 8,697
    You need to wait for the counter to return to 0 immediately after DEBUG ? Total.
  • XiaWarriorsXiaWarriors Posts: 4
    edited 2016-09-14 16:46
    If I was suppose to add Counter = 0 after debug ? total, then it didn't work for me.
    DO
    PAUSE 1000
    DEBUG CLS

    Counter = IN5
    IF Counter = 1 THEN Total = Total + 1 ' Make it so even if the Light continues to shine on the phototransmitter (1), it doesn't count
    DEBUG ? Counter
    DEBUG ? Total
    Counter = 0
    LOOP
  • kwinn wrote: »
    You need to wait for the counter to return to 0 immediately after DEBUG ? Total.

    Yea, I thought so, but..
    How do I do that?
    How is the code suppose to be written?
  • kwinnkwinn Posts: 8,697
    edited 2016-09-14 17:00
    Keep in mind that I do not program in Basic regularly so the syntax may not be correct for this version of basic, but in essence you add the three lines as shown below.

    DO
    PAUSE 1000
    DEBUG CLS

    Counter = IN5
    IF Counter = 1 THEN Total = Total + 1

    DEBUG ? Counter
    DEBUG ? Total

    ' add these three lines
    DO
    Counter = IN5
    LOOP UNTIL (Counter = 0)

    LOOP
  • Thanks
    I didn't realize you can add another Do/Loop inside another Do/Loop
Sign In or Register to comment.