none
Delaying read RRS feed

  • Question

  • Hey,

    I'm writing a times table test and I want to only allow 10 seconds for each question but you have an infinite amount of time to enter the variable. Is there a way to over ride this process?

    Also how do you use the ESC key in small basic?

    Thank you


    Thursday, January 29, 2015 6:02 PM

Answers

  • Here is a way to do it! (This doesn't check for a correct answer. It just checks for taking to long to answer)

    Timer.Interval = 5
    Timer.Tick = Check_Answer
    TimeSinceAsked = Clock.ElapsedMilliseconds
    
    While 1 = 1
    TextWindow.WriteLine("What is "+Math.GetRandomNumber(5)+" * "+Math.GetRandomNumber(5)+"? ")
    TimeSinceAsked = Clock.ElapsedMilliseconds
    Answer = TextWindow.ReadNumber()
    Endwhile
    
    
    Sub Check_Answer
      If Clock.ElapsedMilliseconds - TimeSinceAsked > 5000 Then
        TextWindow.WriteLine("You took too long!")
        Timer.Pause()
        Program.Delay(2000)
        Program.End()
      EndIf
      EndSub


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11



    Friday, January 30, 2015 8:13 PM
    Answerer
  • Hi Ezra94

    I have made the program for same question and posted on other thread by 13rydelljoshua . Where Timer.Interval set to 5 seconds and used Timer.Tick event with Timer.Resume method. If during these 5 seconds there is no input from user, subroutine assigned to Timer.Tick event will automatically generate next question. If user enters answer Timer.Resumes and re-start counting for next 5 seconds

    Hope it may also help you


    Sunday, February 8, 2015 6:32 AM

All replies

  • In the graphics window you can use the KeyDown event to detect a key press then use LastKey to find out which key was pressed. This lets you get around the infinite wait problem but you will need to construct the value entered from the individual key presses.
    Friday, January 30, 2015 7:59 AM
  • Here is a way to do it! (This doesn't check for a correct answer. It just checks for taking to long to answer)

    Timer.Interval = 5
    Timer.Tick = Check_Answer
    TimeSinceAsked = Clock.ElapsedMilliseconds
    
    While 1 = 1
    TextWindow.WriteLine("What is "+Math.GetRandomNumber(5)+" * "+Math.GetRandomNumber(5)+"? ")
    TimeSinceAsked = Clock.ElapsedMilliseconds
    Answer = TextWindow.ReadNumber()
    Endwhile
    
    
    Sub Check_Answer
      If Clock.ElapsedMilliseconds - TimeSinceAsked > 5000 Then
        TextWindow.WriteLine("You took too long!")
        Timer.Pause()
        Program.Delay(2000)
        Program.End()
      EndIf
      EndSub


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11



    Friday, January 30, 2015 8:13 PM
    Answerer
  • Hello Zock,

    I found this topic interesting enough that I thought about taking this challenge. I usually don't prefer looking at other posts/answers until having a try at it myself. That way, I could get a better understanding of the problem. However, upon contemplating a solution, I realized that I couldn't come up with anything at all. Seeking guidance, I studied your example and it worked just as expected, but I only ended up more confused. With any program, the computer reads each line sequentially and repeatedly one at a time. In the main loop, all the code nested inside is constantly reread and no other part of the program. Keeping these points in mind, I deducted that (in the main loop) the question is generated, roughly a millisecond is passed and stored(?), and finally user input is awaited by the program. But, in the text window, read() and readNumber() usually pauses the program until there is user input. That means, the code in the main loop can't loop if its waiting for user input? How can the program track the time in this situation? Even putting aside read(), how can it read Check_Answer() if it isn't the main loop? Sorry If I explained any concepts incorrectly or misread it. I am not too familiar with the Timer. Any explanation is welcomed. 

    Saturday, January 31, 2015 4:41 AM
  • Ezra you're thinking alone the right lines.

    The Timer object creates an event that runs independently of the main program. An asynchronous event.

    The main program runs in the UI thread and events in SB are async and are handled in their own thread, independent of the UI thread.

    http://social.technet.microsoft.com/wiki/contents/articles/15060.small-basic-event-basics.aspx

    Sunday, February 1, 2015 8:45 AM
    Moderator
  • Here's a way to use Escape:

    ESC = Text.GetCharacter(27)
    
    While "True"
      TextWindow.Write("Press ESC to close program: ")
      keyPressed = TextWindow.ReadKey()
      
      If keyPressed = ESC Then
        TextWindow.WriteLine("You pressed ESC, program closing!")
        Program.Delay(1000)
        Program.End()
      Else
        TextWindow.WriteLine("You didn't press ESC.")
      EndIf
      
    EndWhile

    Sunday, February 1, 2015 8:57 AM
    Moderator
  • What you are saying is that the UI thread functions independently in program? Besides the Timer, are there any other keywords that run in the UI event?

    By the way, I didn't realize that readKey() event can be used in the text window. I didn't it find anywhere in the wiki or the reference documentation.

    Sunday, February 1, 2015 5:53 PM
  • Nah, what I'm saying is:

    Events happen independently to your program. Your program can't click a mouse, type on keyboard or control your pc's timer.

    You wire/register an Event and assign it a name.

    When your hardware is subjected to a registered event your program is notified by your OS that an event happened.

    That wired event is fired and is then handled independently by your program. It does this using another thread.

    Here's a sample that runs 2 infinite loops. AFAIK (for SB) you can't process 2 statements simultaneously in 1 thread. Each thread processes 1 statement at a time.

    'RUN 2 INIFINITE LOOPS IN PARALLEL
    GraphicsWindow.Show()
    
    myClock[1] = Shapes.AddText("one")
    myClock[2] = Shapes.AddText("two")
    
    Shapes.Move(myClock[2], 0, 50)
    
    'Register a Timer Event
    Timer.Tick = OnTick
    'This event will be fired in 200ms from now
    Timer.Interval = 200 
    
    While 0=0
      Shapes.SetText(myClock[1], Clock.Time)  
      Program.Delay(100) 
    EndWhile
    
    Sub OnTick  'handles your Tick Event
      Timer.Pause() 'stop Tick from firing again
      While 0=0
        Shapes.SetText(myClock[2], Clock.Time)
        Program.Delay(100)
      EndWhile
    EndSub


    Monday, February 2, 2015 7:04 PM
    Moderator
  • I think I understand it now. I am going to keep studying the info provided and do some experimentation. If I have anymore questions I'll let you know. Thank you for being patient and taking the time to explain.

    PS: Looking at my previous post I realize that I didn't make any sense at all. I think I had my concepts backwards. 

    • Edited by Ezra94 Tuesday, February 3, 2015 1:35 AM
    Tuesday, February 3, 2015 12:41 AM
  • Hi Ezra94

    I have made the program for same question and posted on other thread by 13rydelljoshua . Where Timer.Interval set to 5 seconds and used Timer.Tick event with Timer.Resume method. If during these 5 seconds there is no input from user, subroutine assigned to Timer.Tick event will automatically generate next question. If user enters answer Timer.Resumes and re-start counting for next 5 seconds

    Hope it may also help you


    Sunday, February 8, 2015 6:32 AM