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

Pause Counter

NeoneuronNeoneuron Posts: 9
edited 2009-04-19 02:00 in BASIC Stamp
Greetings!

I have a theory that the FOR counter = (first number) not only starts counting at that number, but is also the amount of delay. Is this correct? Because when I started counting using a WORD for room, and starting the counter = 800 to 900 the light flashed slower to begin with. Is this correct?
Also it seemed that the Bs2 stamp always adds 1ms to the delay for each time the program is run though FOR..NEXT is this correct?

Here is a copy of the program I ran with notes. Please correct me!

Thanks for any Help,

Neo

---

' ($Stamp BS2)
' {$PBASIC 2.5}

counter VAR Nib ' Equals number of counts Nib=15 Byte=255

' DEBUG "Program Running!"

'DO
FOR counter = 13 TO 50 ' Example: 13 is where you start
'counting. Also represents 13ms delay with 1 ms added
' In this case it will stop at nibble 15 (and reset itself
' to zero when it reaches its goal and goes to next?
DEBUG ? counter ' This was added to be able to
'watch the counts on the screen.

HIGH 15 ' red
LOW 14
PAUSE counter

LOW 15 ' Green
HIGH 14
PAUSE counter

LOW 15
LOW 14
PAUSE counter

NEXT
'LOOP

Comments

  • FranklinFranklin Posts: 4,747
    edited 2009-04-18 14:51
    If your counter is a nib it can hold a number from 0 to 15. For counter = 13 to 50 is invalid code. it may run but not as you expect.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • NeoneuronNeoneuron Posts: 9
    edited 2009-04-18 15:00
    True! It only went to 15 and then reset to zero. It answered one of my questions of not going beyond 15, (no matter how high the number). I wish I had answers to the other questions I had asked [noparse]:([/noparse]
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-04-18 16:08
    Many factors have a small influence on execution times. For example, a word variable as a loop counter may be marginally faster than a nib, because the interpreter has to use fewer bits to store the address of a nib. But I don't understand your theory (ies). Could you elaborate what you mean by, "when I started counting using a WORD for room, and starting the counter = 800 to 900 the light flashed slower to begin with. Is this correct?"

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • NeoneuronNeoneuron Posts: 9
    edited 2009-04-18 16:30
    Thank you for helping! I needed the Word size to be able to count higher. (You can't count past 255 with a Byte) In this case I tried starting the count from 800. The 900 number was just used to keep the count short so I could test it quickly. The thing is, I notice the bulb flashed slower from the start when I did this. So, is it true that not only am I'm (in this case) starting the count from 800. And 800 seems to flash the bulb slower from the start? Or am I wrong?

    Again Thanks!
  • NeoneuronNeoneuron Posts: 9
    edited 2009-04-18 16:35
    Or is there something else going on in processing that I am not aware of?? But it seems that the higher the number, the slower the bulb flashes from the very start.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-04-18 20:46
    >I have a theory that the FOR counter = (first number) not only starts counting at that number, but is also the amount of delay. Is this correct?
    That is correct, because that is the way you programmed it. Your program code would sure enough flash slower starting with counter=800 than with counter=80, because of the "PAUSE counter" command.

    In the snppet like this, the led flash will slow down:
    counter VAR Word
    FOR counter=100 to 1000
    TOGGLE led
    PAUSE counter ' <--- the pause and the rate of flashing depend on the current value of counter
    NEXT

    There are much much smaller effects having to do with the size of the variables. I should have said "the interpreter has to use fewer bits to store the address of a word than a nib, because it only takes 4 bits to address one out of 16 word locations in ram, versus 1 out of 64 nib locations. But the difference there is only a few microseconds.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • NeoneuronNeoneuron Posts: 9
    edited 2009-04-18 22:00
    Thank you so much Tracy! So is it correct to say that entering a larger number after the "For counter = " takes more time to process and hence the slower flashing to begin with?
  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-18 22:22
    The FOR statement takes roughly the same time to execute regardless of the size of the number. You're using the value of the FOR variable as the delay for the PAUSE statement and that is what is taking the extra time. For example,
    counter VAR nib
    FOR counter = 1 to 10
    TOGGLE led
    PAUSE counter * 100
    NEXT
    


    This behaves the same as the program that Tracy wrote, but it only uses a nibble for the counter.
  • NeoneuronNeoneuron Posts: 9
    edited 2009-04-18 22:49
    Thanks Mike! So when I type something like, FOR counter = 800 to 1000 The 800 is 800 milliseconds? Or For Counter = 10 to 50 The 10 is 10 milliseconds?
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-04-19 02:00
    How about this approach:··

    Room VAR Word

    For room = 800 to 900

    ··· .....

    ··· pause 100

    next

    Now you have· FOR loop with "fixed" delay of 100 ms.

    When you use variable room as value to your pause it will naturally vary with the actual loop index.

    Or better yet - do not use variable name·"counter"· if you really doing "delay".

    Cheers

    Vaclav
Sign In or Register to comment.