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.

pass the TIME away

Status
Not open for further replies.
What i mean is that you don't have to have two players...
You could do the same with a single player game and just telling player 1 to remember his score.
Having a second player GO button and display is for convenience.

Just trying to figure out what you're after here
 
A video will demonstrate this better than words.

Basically, the top bunch of lights are a countdown to the green go light. If you press the button before the light is green, it's a foul and you're disqualified.

The time between the light turning green is your reaction time. The shorter that interval, the better.


A bar in a bowling alley I frequent (because they have thick deep-fryed bacon on their awesome cheese burgers) has one of these things. I'm not sure it's ever worked....
 
Last edited:
To be fair, I have seen professional programmers fall into the trap of believing that getting an output, any output, means their code is working properly.

I wonder if MrDEB (no, I don't actually wonder because I know the answer) has noticed that player 1 always wins, or at worst it's a tie? It can be nothing else with his copy&paste code.
 
yes player 1 wins when using a WHILE WEND loop
that's one reason why I am considering 2 timers. 1 timer for each player.
how to get 2 timers to play together is my issue. The help file doesn't help much. It mentions two timers but??
 
Stick with one timer.

How about something like this:
Code:
    dim buttons as byte
    dim timer1, timer2 as word
    
    //initialize timer
    Timer.Initialize(1)
    Timer.Items(0).interval=1  //ms
    Timer.Items(0).OnTimer=@OnTimer  //timer event handler
    Timer.Items(0).enabled=true

    // add your while loop and countdown code here...
    
    // start race
    green=1
    mS = 0
    timer1 = 0
    timer2 = 0
    Timer.Start()
    
    buttons = 0
    repeat
        if (GO_1 = 0) and (timer1 = 0) then        // player1 button press...
            timer1 = mS
            buttons = buttons or 1
        endif
        if (GO_2 = 0) and (timer2 = 0) then        // player2 button press...
            timer2 = mS
            buttons = buttons or 2
        endif
    until (buttons = 3)
    ET_time()      //display timer1 and timer2
    Timer.Stop()
 
will run Tumbleweeds code suggestion,
QUESTION?? Maybe instead of a polled time I should be using an event timer?
have two EVENTS two button presses?
 
No. There's no simple way to hookup a button press to a timer event.
It would just make things much more complicated.
 
ran code suggestion but doesn't work.
only displays one time and the beginning sequence doesn't even run.
I see you added a "counter" I tried that as well. Maybe I did it wrong?
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2020 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 11/11/2020                                                     *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
Device = 18F43k22
Clock = 8
//Config MCLRE = off
// some LCD options...
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
// import LCD library...
Include "SetDigitalIO.bas"
Include "IntOSC.bas"
Include "LCD.bas"
Include "utils.bas"
'INCLUDE "Keypad16pullup.bas"
Include "Convert.bas"
Include "isrtimer.bas"
Include "EEPROM.bas"
Dim s As String
Dim mS As Word
Dim GO_1 As PORTC.0     //goes LOW when player presses switch
Dim GO_2 As PORTC.2     //goes LOW when player presses switch
Dim green As PORTE.0
Dim red_1 As PORTE.1
Dim red_2 As PORTE.3
Dim Shift_led As PORTA.0  //SHIFT LED
Dim blue1 As PORTA.1
Dim blue2 As PORTA.2
Dim yellow1 As PORTA.3
Dim yellow2 As PORTA.4
Dim YELLOW3 As PORTA.5
//Dim S_press As PORTC.0   //SHIFT BUTTON
Dim x As Byte        //USED FOR SHIFT POINTS
Dim RT_1 As LongWord   //REACTION TIME player 1
Dim RT_2 As LongWord   //REACTION TIME player 2
Dim ET_1 As LongWord   //elasped time player 1
Dim ET_2 As LongWord   //elasped time player 2
Dim Start_Time_1 As Word
Dim Start_Time_2 As Word
Dim End_Time_1 As Word
Dim End_Time_2 As Word
Dim timer1 As Word
Dim timer2 As Word
Dim buttons As Byte

Sub OnTimer()
    Inc(mS)
End Sub


Sub Display()
     //dispay results
        Timer.Stop //stop timer
        Cls
        s=DecToStr(timer1/1000)+"."+DecToStr((timer1 Mod 1000))
        WriteAt(1,1,"timer1    ")            //display players score
        WriteAt(2,1,"seconds   ",s,"     ")
        s=DecToStr(timer2/1000)+"."+DecToStr((timer2 Mod 1000))
        WriteAt(3,1,"timer2     ")            //display players score
        WriteAt(4,1,"seconds   ",s,"     ")
       // EE.Write(6,RT_1)
       // EE.Write(6,RT_1)
         DelayMS(5000)
         Cls
End Sub

Sub Foul()
    If GO_1=0 And green = 0 Then
    red_1=1
    WriteAt(1,1,"FOUL" )
    DelayMS(3000)
        blue1=0 //led is off
        blue2=0
        yellow1=0
        yellow2=0
        YELLOW3=0
        green=0
        red_1=0
    Cls
EndIf
End Sub
Sub End_of_game()
    blue1=0 //led is off
    blue2=0
    yellow1=0
    yellow2=0
    YELLOW3=0
    green=0
    red_1=0
DelayMS(2000)
End Sub


    
    //initialize timer
    Timer.Initialize(1)
    Timer.Items(0).interval=1  //ms
    Timer.Items(0).OnTimer=@OnTimer  //timer event handler
    Timer.Items(0).enabled=true
 mS=0
 'timer1=mS
 'timer2=mS
Start_Time_1=mS
Start_Time_2=mS
End_Time_1=mS
End_Time_2=mS
Output(Shift_led)
Output (blue1)
Output(blue2)
Output(yellow1)
Output(yellow2)
Output(YELLOW3)
Output (green)
Output(red_1)
Input(GO_1)     //button press
Input(GO_2)     //button press
//Input(S_press)//shift button
//GO_1 = 1    //input
blue1=0   //led
blue2=0   //led
green = 0 //led
mS=0      //timer set to zero


SetAllDigital   
//initialize timer
Timer.Initialize(2)
Timer.Items(1).interval=1  //ms
Timer.Items(1).OnTimer=@OnTimer  //timer event handler
Timer.Items(2).interval=1  //ms
Timer.Items(2).OnTimer=@OnTimer  //timer event handler
//enable timers
Timer.Items(1).enabled=true
'Timer.Items(2).enabled=true
'mS = 0
'Timer.Start()   

    // add your while loop and countdown code here...
  While True
DelayMS(1000)
blue1=1        //first stage light
        If GO_1=0 And green=0
        GoTo skipcode         //skipcode is a foul start
        DelayMS(1000)
        blue2=1
             If GO_1=0 And green=0
             GoTo skipcode
             DelayMS(1000)
                 yellow1=1
                 If GO_1=0 And green=0
                 GoTo skipcode
                 DelayMS(500)
                     yellow2=1
                     If GO_1=0 And green=0                   
                     GoTo skipcode
                     DelayMS(500)
                         YELLOW3=1
                         If GO_1=0 And green=0
                         GoTo skipcode
                             DelayMS(500)
                             If GO_1=0 And green = 0 //Then  //green = 0
                             GoTo skipcode
                             DelayMS(200)
    // start race
    green=1
    mS = 0
    timer1 = 0
    timer2 = 0
    Timer.Start()
    
    buttons = 0
    Repeat
        If (GO_1 = 0) And (timer1 = 0) Then        // player1 button press...
            timer1 = mS
            buttons = buttons Or 1
        EndIf
        If (GO_2 = 0) And (timer2 = 0) Then        // player2 button press...
            timer2 = mS
            buttons = buttons Or 2
        EndIf
    Until (buttons = 3)
    Display()      //display timer1 and timer2
    Timer.Stop()
    skipcode:
     Foul()
     End_of_game()
                    
    Wend
 
A little tweaking of code and it appears to work
the elapsed times are close but not sure if accurate?
Code:
 green=1        //start race
    timer1=0
    timer2=0
     Repeat   
    Timer.Start
    Repeat
        If (GO_1 = 0) And (timer1 = 0) Then        // player1 button press...
            timer1 = mS
            counter = counter + 1
        EndIf
        If (GO_2 = 0) And (timer2 = 0) Then        // player2 button press...
            timer2 = mS
           counter = counter +1
        EndIf
    Until (counter >= 2)
    Display()      //display timer1 and timer2
    Timer.Stop()
    Until (counter = 3)
 
I didn't add a counter, I added a flag to keep track of which buttons have been pressed.
Each bit in 'buttons' means that button was pressed.
It probably didn't work because you were enabling multiple timers.

I integrated your code... try this
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2020 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 11/11/2020                                                     *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
Device = 18F43k22
Clock = 8

//Config MCLRE = off
// some LCD options...
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
// import LCD library...
Include "SetDigitalIO.bas"
Include "IntOSC.bas"
Include "LCD.bas"
Include "utils.bas"
Include "Convert.bas"
Include "isrtimer.bas"
Include "EEPROM.bas"

Dim GO_1 As PORTC.0     //goes LOW when player presses switch
Dim GO_2 As PORTC.2     //goes LOW when player presses switch
Dim green As PORTE.0
Dim red_1 As PORTE.1
Dim red_2 As PORTE.3
Dim Shift_led As PORTA.0  //SHIFT LED
Dim blue1 As PORTA.1
Dim blue2 As PORTA.2
Dim yellow1 As PORTA.3
Dim yellow2 As PORTA.4
Dim yellow3 As PORTA.5
//Dim S_press As PORTC.0   //SHIFT BUTTON

Dim mS As Word
Dim timer1 As Word
Dim timer2 As Word
Dim buttons As Byte


Sub OnTimer()
    Inc(mS)
End Sub

// display results
Sub Display()
    Dim s As String
    Cls
    s=DecToStr(timer1/1000)+"."+DecToStr((timer1 Mod 1000))
    WriteAt(1,1,"timer1    ")            //display players score
    WriteAt(2,1,"seconds   ",s,"     ")
    s=DecToStr(timer2/1000)+"."+DecToStr((timer2 Mod 1000))
    WriteAt(3,1,"timer2     ")            //display players score
    WriteAt(4,1,"seconds   ",s,"     ")
    DelayMS(5000)
    Cls
End Sub

Sub Foul()
    If green = 0 Then
        red_1=1
        WriteAt(1,1,"FOUL" )
        DelayMS(3000)
        blue1=0 //led is off
        blue2=0
        yellow1=0
        yellow2=0
        yellow3=0
        green=0
        red_1=0
        Cls
    EndIf
End Sub

Sub End_of_game()
    blue1=0 //led is off
    blue2=0
    yellow1=0
    yellow2=0
    yellow3=0
    green=0
    red_1=0
    DelayMS(2000)
End Sub

SetAllDigital  
low(Shift_led)      // make led outputs, set 0
low(blue1)
low(blue2)
low(yellow1)
low(yellow2)
low(yellow3)
low(green)
low(red_1)

Input(GO_1)     //button press
Input(GO_2)     //button press
//Input(S_press)//shift button

//initialize timer
Timer.Initialize(1)
Timer.Items(0).interval=1  //ms
Timer.Items(0).OnTimer=@OnTimer  //timer event handler
Timer.Items(0).enabled=true

// add your while loop and countdown code here...
While True
    DelayMS(1000)
    blue1=1        //first stage light
    If (GO_1 = 0) or (GO_2 = 0) GoTo skipcode         //skipcode is a foul start

    DelayMS(1000)
    blue2=1
    If (GO_1 = 0) or (GO_2 = 0) GoTo skipcode         //skipcode is a foul start

    DelayMS(1000)
    yellow1=1
    If (GO_1 = 0) or (GO_2 = 0) GoTo skipcode         //skipcode is a foul start

    DelayMS(500)
    yellow2=1
    If (GO_1 = 0) or (GO_2 = 0) GoTo skipcode         //skipcode is a foul start

    DelayMS(500)
    yellow3=1
    If (GO_1 = 0) or (GO_2 = 0) GoTo skipcode         //skipcode is a foul start

    DelayMS(500)
    If (GO_1 = 0) or (GO_2 = 0) GoTo skipcode         //skipcode is a foul start
   
    DelayMS(200)
   
    // start race
    mS = 0
    timer1 = 0
    timer2 = 0
    green=1
    Timer.Start()
   
    buttons = 0
    Repeat
        If (GO_1 = 0) And (timer1 = 0) Then        // player1 button press...
            timer1 = mS
            buttons = buttons Or 1
        EndIf
        If (GO_2 = 0) And (timer2 = 0) Then        // player2 button press...
            timer2 = mS
            buttons = buttons Or 2
        EndIf
    Until (buttons = 3)
    Display()      //display timer1 and timer2
    Timer.Stop()

skipcode:
     Foul()
     End_of_game()
Wend
 
Sorry, I can't help but say this.

What a #$%/_ waste of time. You get advice from people who know what they are talking about, and they explicitly say "don't do it that way" AND EXPLAIN why what you're trying to do won't work, and you completely ignore it. At best (worst???), you take a couple statements and add them to that mess you call code, and are somehow surprised when it doesn't work.
 
Thanks Tumbleweed, the suggestion works great.
Now I am curious about the flag that you mentioned.
I ran my code and discover that it only displays correct time on the first pass then just 0.0
then the OR statement?
my code
Code:
  //start race                           
    mS=0
    green=1       
    timer1=0
    timer2=0
     Repeat   
    Timer.Start
    'Repeat
        If (GO_1 = 0) And (timer1 = 0) Then        // player1 button press...
            timer1 = mS
            counter = counter + 1
        EndIf
        If (GO_2 = 0) And (timer2 = 0) Then        // player2 button press...
            timer2 = mS
           counter = counter +2
        EndIf
    Until (counter = 3)
  
    Timer.Stop()
    Display()      //display timer1 and timer2' Until (counter = 3)

then your suggested code

Code:
   // start race
    mS = 0
    timer1 = 0
    timer2 = 0
    green=1
    Timer.Start()
  
    buttons = 0
    Repeat
        If (GO_1 = 0) And (timer1 = 0) Then        // player1 button press...
            timer1 = mS
            buttons = buttons Or 1
        EndIf
        If (GO_2 = 0) And (timer2 = 0) Then        // player2 button press...
            timer2 = mS
            buttons = buttons Or 2
        EndIf
    Until (buttons = 3)
    Display()      //display timer1 and timer2
    Timer.Stop()

after reviewing this post I see where I failed to reset COUNTER as you did with counter
 
I inserted a line to reset the COUNTER to 0 and it seems to run same as Tumbleweeds code.
Am I to assume using a FLAG as Tumbleweed did (buttons) performs just like a counter as in my example?
 
I don't think I used multiple timers but?
You did when you copy-pasted code from the help file.
You ended up with multiple timers, all incrementing the same 'ms' variable'

Am I to assume using a FLAG as Tumbleweed did (buttons) performs just like a counter as in my example?
In this particular case they end up being the same.
However, to me calling it a 'counter' and using addition (+1, +2) makes it more difficult to see how it's being used.

Maybe this syntax makes it clearer?
Code:
    buttons = 0
    Repeat
        If (GO_1 = 0) And (timer1 = 0) Then        // player1 button press...
            timer1 = mS
            buttons.bits(0) = 1
        EndIf
        If (GO_2 = 0) And (timer2 = 0) Then        // player2 button press...
            timer2 = mS
            buttons.bits(1) = 1
        EndIf
    Until (buttons = 3)        // 3 is buttons.0=1 and buttons.1=1   (1 + 2)
 
Tumbleweed, I like the way you used the flag. Very neat way of testing for a series of conditions!

My usual approach is to set up boolean variables and then use "if this AND that AND theother, then...." statements. It's kind of a tradeoff between very descriptive variable names and simple conditional tests.

Proving there are many paths to a good solution ;)
 
You can always create aliases to the bit definitions for more descriptive names
(not that these are, but you get the gist)...
Code:
dim buttons as byte
dim but1 as buttons.bits(0),
    but2 as buttons.bits(1),
    but3 as buttons.bits(2),
    but4 as buttons.bits(3)
    
buttons = 1
but1 = 1
but2 = 0

if (but1 = 1) or (but2 = 0) then...
if (buttons = %1010) then...
 
curious bug?
I run this snippet of code and I inserted a green led and red led to indicate the code progression BUT
the red led comes on first AFTER the first section is complete.
Then the second section progresses and when complete the green led lights
THEN the red led lights up again but it shouldn't because that section is completed. WHY? and how to prevent the code from progressing? this is SWORDFISH basic
Code:
Sub Stage()
//#1 
Repeat
        If (Go_1 = 0) And (blue1 = 0) Then        // player1 button press...
            blue1=1
            blue2=0
            Buttons = Buttons Or 1
        EndIf
        If (Go_2 = 0) And (blue_1= 0) Then        // player2 button press...
            blue_1=1
            blue_2=0
            Buttons = Buttons Or 2
        EndIf
    Until (Buttons = 3) //3
       red=1
       delayms(500)
       red=0
       delayms(500)
                      
      
    buttons=0
Repeat
        If (Go_1 = 0) And (blue2 = 0)Then        // player1 button press...
            blue1=1
            blue2=1
            Buttons = Buttons Or 1
        EndIf
        If (Go_2 = 0) And (blue_2= 0) Then        // player2 button press...
            blue_1=1
            blue_2=1
            Buttons = Buttons Or 2
        EndIf
    Until (Buttons = 3) //3
         green=1
       delayms(500)
       green=0
       delayms(500)
                      
End Sub
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top