Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Simplistic reaction timer

Status
Not open for further replies.
this dosent work but ging to ponder on i and get a fresh start tomrrow
Code (text):
WHILE True
delayms(2000) // insert a random # here
mS = 0
green = 0 //turn on green until button is pressed

Timer.Start //start timing reaction time
if button = 0 and green = 1 //green is not enabled but button is pressed
then red = 0 //turn on red led
delayms(2000)
timer.stop
end if

REPEAT
UNTIL
button = 0 // button pressed then turn off green, turn on blue
green = 1 //off
blue = 0 //on


Timer.Stop //stop timer

' WriteAt(1,1,"reaction=",DecToStr(mS),"ms",4)
s = DecToStr(ms/1000) + "." + DecToStr((ms MOD 1000), 3) + "secs"
WriteAt(1,1,"reaction=",s,2)
DELAYMS(3000)
blue = 1 //led is off
red = 1
DELAYMS(1000)

WEND
 
Code:
WHILE True
    delayms(2000) // insert a random # here
    mS = 0
    green = 0                    //turn on green until button is pressed
   
    Timer.Start                  //start timing reaction time
         if button = 0 and green = 1 //green is not enabled but button is pressed
         then red = 0                //turn on red led
         delayms(2000)
         timer.stop
         end if
         
            REPEAT
            UNTIL 
            button = 0                   // button pressed then turn off green, turn on blue
            green = 1                    //off
            blue = 0                     //on
           
           
            Timer.Stop                   //stop timer
             
   ' WriteAt(1,1,"reaction=",DecToStr(mS),"ms",4)
    s = DecToStr(ms/1000) + "." + DecToStr((ms MOD 1000), 3) + "secs"
WriteAt(1,1,"reaction=",s,2)
    DELAYMS(3000)
    blue = 1     //led is off
    red = 1
    DELAYMS(1000)
   
WEND
 
Code:
WHILE True
    delayms(2000) // insert a random # here

    // make sure button is not pressed before we begin
    while (button = 0)
        red = 0                   // turn on red
    wend
    red = 1                     // just in case it's on

    // start
    green = 0                    //turn on green until button is pressed
    mS = 0
    Timer.Start                  //start timing reaction time
    REPEAT
    UNTIL button = 0             // button pressed...
    Timer.Stop                   //stop timer

    green = 1                    // turn off green, turn on blue
    blue = 0
         
   ' WriteAt(1,1,"reaction=",DecToStr(mS),"ms",4)
    s = DecToStr(ms/1000) + "." + DecToStr((ms MOD 1000), 3) + "secs"
    WriteAt(1,1,"reaction=",s,2)
    DELAYMS(3000)

    blue = 1     //all leds off
    red = 1
    green = 1
    DELAYMS(1000)
WEND
 
Thanks will give it a try.
I looked up MOD but very little info. What exactly does it do. NEVER seen it used before
 
When you divide integers ( whole numbers ) modulus is the remainder.
So 20 / 6 should be 3.333.. But we lose the real part so we get 3.

If you do 20 mod 6 you get the remainder ie 2..
Divide... 20/6 = 3
Modulus.... 20 MOD 6 = 2
 
MOD is the modulus operator. It gives the remainder of the division of one number by another.

edit: what Ian said.
 
WTF!!! Did i just witness code tags????? :O
 
very interesting. Thanks for the explanation as I have never seen MOD used in any code.
 
very interesting. Thanks for the explanation as I have never seen MOD used in any code.
They normally use it near the bits you ignore :D.
 
Am I correct that calling an INTERRUPT is supposed to stop code flow, jump to the INTERRUPT routine then continue with rest of code.
I am calling the INTERRUPT but the code dosen't do the INTERRUPT routine? here is the INTERRUPT
code basic
CONST
ipLow = 1,
ipHigh = 2
INTERRUPT pin(iplow)
for x = 0 to 5
red4 = 1
DELAYMS(1000)
TOGGLE (red4)
DELAYMS(1000)
next
END INTERRUPT

And here I am calling the INTERRUPT
code basic

Yellow1 = 1
if button1 = 0
then
debounce()
end if

DELAYMS(1000)
TOGGLE(Yellow1)
delayms(1000)
here is the sub routine Debounce
sub Debounce ()
delayms(100)
enable(pin)

disable (pin)
end sub

This is supposed to stop if the green led is not enabled yet (pressing the button1 before the green led indicates to press.
 

Attachments

  • isrbegin code.bas
    4.3 KB · Views: 291
Why you using a interrupt? Dont try and over complicate things. Often you can simply use while loops to stop flow, so things like

while something is like this, stay put. then when it changes and the condition becomes True/untrue it moves on, thats kind of the whole while 1 thing, it is basically saying while true (1= true 0 = untrue) do this code. In C you often (in old mplab anyway and C18) used to see in start up code a oscillator section, it would look for a OSC flag that would flag as true once the OSC had become stable, those of a picky nature (like me), would often put in code that wouldnt let the init bit proceed until the internal clock was stable.

So we put

while XXxx (forgotten the reg now) = X xxxx

This made the code stand still until that OSC bit tripped to show the internal oscillator was stable, in reality you were talking fractions of a second, but there you go. Interrupts are used when you absolutely must get something done on a certain condition. say you had a machine safety guard, the beam or safety switch on the guard hinge would goto a interrupt on change routine, so if some idiot opened the guard while the machine ran, then the code jumps directly to a routine that stops the machine.

I dont think you need interrupts for this, but basic isnt my thing yet.
 
I tried using a while wend loop
dim PinIsHigh as PORTB.Booleans(0)

while PinIsHigh

wend



then the code following wend is not executed until PORTB.0 becomes equal to 0.
I changed to portb.4
I have tried many different ideas from the SF help file.
Tried using an INTERRUPT but it too dosen't seem to work as desired.
IF the player presses button1 too soon before the green led is enabled then the red4 led is enabled. Tried different debounce senerios since it appears to work if you hold down the button1.
 
Odd you tried code and it didnt work, not sure what to say, it unusual for code not to work for you, must be a bug or something in the help file.
 
What do you think happens if a switch is pressed during any of the dozens of DelayMS statements in the code?

Hint: nothing at all.
 
What do you think happens if a switch is pressed during any of the dozens of DelayMS statements in the code?

Hint: nothing at all.
Well to be fair, if he buys cheap ass enough switches, they should in theory still be bouncing by Christmas..... Otherwise its possible while the micro is busy counting for no particular reason, it might miss a press unless they stab it multiple times. Does sword fish have a on change interrupt?
 
Yes, Swordfish does. I don't know what happens when you are paused in the ISR and another interrupt occurs.

I am not going to take time to decipher MrDEB's mis-mash of code. I simply am incapable of following his logic and the rewards of attempting to do so are zilch, zip, zero.
 
I tried eliminating the DELAYMS statements and used FOR NEXT LOOP thinking it might work.
Will keep trying, was/am hoping for suggestions what to try or perhaps the INTERRUPT is configured wrong.
Going to try the FOR NEXT LOOP again but slightly different by adding a COUNT sequence then maybe detect the switch closing by using an IF THEN statement.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top