Need Help on
Zach Crider
Posts: 3
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".
' 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
Also, a better loop will look something like this:
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.
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.
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.
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".
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.