none
counter in small basic RRS feed

  • Question

  • Hello,

    i'm new in programing (just taked the challenge of the small basic), and i would like to do a counter that counts from 0 to 100 in 100 seconds but i didnt found anything that could help me to do that.

    Can anyone reply with an example or anything that could help me?

    i would apreciate.
    greets
    d_ethos
    Friday, March 6, 2009 1:52 PM

Answers

  •  I believe that the Program.Delay() function is event driven and not just a counting loop hidden from the user. This means that, within reason, the time should be unaffected by the loading of the PC. The trouble with Program.Delay() is that you can't do anything else while you're waiting.
    In the thread "How would I show the time?" Vijaye raised the question of adding a timer:


    "As for the timer, there was some thought about adding a timer to Small Basic - something along the lines of:

     Timer.Interval = 1000
     Timer.Tick = OnTimerTick

     Sub OnTimerTick
         DrawClockHands()
     EndSub

    I'd like to get some feedback about the utitlity vs. complexity of such a feature."


    I think this is a good place to raise the subject. What does everyone else think?



    Tuesday, March 10, 2009 6:27 PM

All replies

  • Here is 1 way to count 100 seconds by using the clock function and only using the second part of the function.  The same thing could be accomplished by using the minute and second calls of the clock function.

    '100 Second Counter

    startsecond=Clock.second
    TextWindow.WriteLine("Start Second is: "+startsecond)
    While Clock.Second =startsecond
      'Loop past startsecond
    EndWhile
    TextWindow.WriteLine("Past Start")
    TextWindow.WriteLine("Looping for 1 Minute")
    While Clock.Second <>startsecond
      'Loop first minute
    EndWhile
    TextWindow.WriteLine("Past 1 Minute")
    finish=startsecond+40 'add 40 seconds to startsecond
    If finish > 60 Then
      EndingSecond=finish-60  'actual ending second
    endif
    TextWindow.WriteLine("Ending Second is: "+EndingSecond)
      While Clock.Second <> EndingSecond
        'Loop for last 40 Seconds
      endwhile
      TextWindow.WriteLine("100 Seconds has Passed "+Clock.second)

     JR
    Friday, March 6, 2009 3:53 PM
    Answerer
  • If your counter doesnt need to be synced with the system clock and you dont mind pausing the program.

    For counter = 0 To 100
      Program.Delay(1000) '1000 milliseconds = 1 second
      TextWindow.Write(counter)
      TextWindow.WriteLine(" seconds have passed.")
    EndFor
    Friday, March 6, 2009 4:23 PM
  • I was thinking the same as SYS64738, but when I saw Jricestk's solution I thought it would be better because the system clock would give a true 100 seconds, where the Program.Delay could be affected by your computer's performance at the time it is ran.
    Obviously the system performance would not be affected by this program, it would have to be other programs running that would slow your computer.

    So I think that a true 100 seconds would be obtained by using the Clock function.
    ...and a in-program 100 seconds would be like...  Let's say 100 seconds before the bomb blows up and you are running away from the bomb, but the game lags and your character isn't getting away as quickly but if the game is checking the system clock then your character could be only 20 seconds away from the bomb when the system clock says 100 seconds and BOOM!  The bomb goes off.

    Again, not that this program would have that problem, but just a thought. :-)
    Tuesday, March 10, 2009 4:58 PM
  •  I believe that the Program.Delay() function is event driven and not just a counting loop hidden from the user. This means that, within reason, the time should be unaffected by the loading of the PC. The trouble with Program.Delay() is that you can't do anything else while you're waiting.
    In the thread "How would I show the time?" Vijaye raised the question of adding a timer:


    "As for the timer, there was some thought about adding a timer to Small Basic - something along the lines of:

     Timer.Interval = 1000
     Timer.Tick = OnTimerTick

     Sub OnTimerTick
         DrawClockHands()
     EndSub

    I'd like to get some feedback about the utitlity vs. complexity of such a feature."


    I think this is a good place to raise the subject. What does everyone else think?



    Tuesday, March 10, 2009 6:27 PM
  • Thank you very much for all your answers, they were very helpful and with then i reach the goal that i was suposed to!

    i finaly did my first program.

    greets
    d_ethos
    Wednesday, March 11, 2009 3:34 AM
  • There's already a way to have timers in Small Basic through my extension.

    The code you'll need :

        SmallBasic.SetInterval("X", 1000)
        
        Sub X
            ' Each second (or 1000ms)
        EndSub

    If you want to run your function only one time :

        SmallBasic.SetTimeout("X", 1000)

    If you want to be able to end-up the timer at a time :

        Timer = SmallBasic.SetInterval("X", 1000)
            ' At a time
            SmallBasic.ClearInterval(Timer) ' I'm unsure of the syntax, here, but I know I implemented it


    Fremy VB & C#
    Wednesday, March 11, 2009 5:06 PM