Shop OBEX P1 Docs P2 Docs Learn Events
Need Help on — Parallax Forums

Need Help on

Zach CriderZach Crider Posts: 3
edited 2009-03-30 00:08 in Learn with BlocklyProp
I'm new to this and trying to learn on my own, I'm not sure I fully understand trying to code on my own. The IF... THEN...ELSE, Psuedo code Item 3 in "revising the design" has me stuck. I'm not sure how to code it to make it work. Heres what I have not really sure how to fix it:

' What is a Microcontroller - ReactionTimer.bs2
' Test reaction time with a pushbutton and a bi-color LED.

' {$STAMP BS2}
' {$PBASIC 2.5}

timecounter VAR Word ' Declare variable to store time.
value VAR Byte
value = 23

DEBUG "Press and hold pushbutton.", CR, ' Display reation instructions.
"to make light turn red.", CR, CR,
"When light turns green, let", CR,
"go as fast as you can.", CR, CR


DO ' Begin main loop.

DO ' Nested loop repeats...
LOOP UNTIL IN3 = 1 ' until pushbutton press.

LOW 14 ' Bi-color LED red.
HIGH 15

RANDOM value
DEBUG "Delay time ", ? 1000 + value, CR

PAUSE 1000 + value ' Delay 1 second plus value of random.

HIGH 14 ' Bi-color LED green.
LOW 15

timecounter = 0 ' Set timecounter to zero.


DO ' Nested loop, count time...

IF timecounter = 1 THEN
DEBUG "Wait for green ", CR,
"Try Again!", CR, CR

LOW 14

ELSEIF timecounter > 1 THEN
PAUSE 1
timecounter = timecounter + 1
ENDIF
LOOP UNTIL IN3 = 0 ' until pushbutton is released.

LOW 14 ' Bi-color LED off.

timecounter = timecounter * 2
DEBUG "Your time was ", DEC timecounter, ' Display time measurement.
" ms.", CR, CR,
"To play again, hold the ", CR, ' Play again instructions.
"button down again.", CR, CR



LOOP ' Back to "Begin main loop".

Comments

  • SRLMSRLM Posts: 5,045
    edited 2009-03-29 04:38
    Why do you turn the led on 14 off?

    Also, a better loop will look something like this:

    
    /*...*/
      DEBUG "Wait for green ", CR,
        "Try Again!", CR, CR
    
      DO ' Nested loop, count time...
        timecounter = timecounter + 1
      LOOP UNTIL IN3 = 0 ' until pushbutton is released.
    
      LOW 14 ' Bi-color LED off.
    /*...*/
    
    



    I'm guessing here on what you want done, so see if this works for you, if not then a complete description will help... As a side note, your code doesn't take into account what happens when the user releases the button during the 1s + delay. As it is, it just says that you have a 1 unit of time reaction.
  • Zach CriderZach Crider Posts: 3
    edited 2009-03-29 15:22
    I'm trying to display the error message if the pushbutton is released before the light turns green. I know my code is wrong it should have something to do with delay value. the original code allows the user to release the pushbutton before the light turns green and it shows a short reaction time ( 1 or 2 ms).

    Here is that line of code
    /*...*/
    DO ' nested loop count time
    Pause 1
    timcounter = timecounter + 1

    LOOP UNTIL IN3 = 0 ' until pushbutton is released

    LOW 14 ' Bi-color LED off

    /*...*/

    Like you said it doesn't take into account what happens in the random time delay. That is the IF..THEN..ELSE.. coding I cant figure out. Its part of "What is a microcontroller?"
    page 96. I bought the kit to teach myself but this is one concept that im stuck on.
  • SRLMSRLM Posts: 5,045
    edited 2009-03-29 16:10
    Okay, according to the text I believe that the author meant that test to be outside of the loop. So, use the loop I provided and move the test (IF-ELSE) outside and after the loop.

    By the way, I didn't include the pause statement in the loop since the time to execute that code will put a delay in there. By leaving out the pause, you can get a higher resolution on your measurement.
  • Zach CriderZach Crider Posts: 3
    edited 2009-03-29 17:35
    Thank you so much This is what I adjusted the code to and it worked the right way! I guess it just takes and outside view sometimes to figure out the solution to a problem. Thanks again!


    DO ' Nested loop, count time...
    timecounter = timecounter + 1

    LOOP UNTIL IN3 = 0 ' until pushbutton is released.

    LOW 14 ' Bi-color LED off.

    IF timecounter = 1 THEN
    DEBUG "Try again ", CR,
    "Wait for Green!", CR, CR
    ELSEIF timecounter > 1 THEN

    timecounter = timecounter * 2
    DEBUG "Your time was ", DEC timecounter, ' Display time measurement.
    " ms.", CR, CR,
    "To play again, hold the ", CR, ' Play again instructions.
    "button down again.", CR, CR

    ENDIF

    LOOP ' Back to "Begin main loop".
  • SRLMSRLM Posts: 5,045
    edited 2009-03-30 00:08
    Zach Crider said...
    I guess it just takes and outside view sometimes to figure out the solution to a problem.

    There's a philosophy in professional programming environments called "extreme programming". Basically, it says that each computer has two people at time: one typing out code, the other just watching and commenting. Apparently (I've never tried it) it is very effective in catching errors before they're made.
Sign In or Register to comment.