• 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

MrDEB

Well-Known Member
During this pandemic one needs to find a way to pass the time and not waste watching the boob tube so I have a very simple swordfish code that seems to work very well AFTER I used the GOTO statement.
This is a simplistic drag race simulator/reaction timer. Plan to use a 4x20 lcd and making it a two player game.
Thinking of adding a 4 pole coded rotary switch to adjust the Christmas tree countdown (there are several different ones used on legal drag strips. The timing between each bulb) and adding a shift light to indicate when to "shift gears". A missed shift could cost you the race.
As an example, a pro-stock car shifts at about 10,000 rpm.
anyway here is my basic code if anyone wants something to pass the time away. A schematic is kind-a self explanatory.
Code:
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"
Dim s As String
Dim mS As Word
Dim GO As PORTC.2
Dim green As PORTE.0
Dim red As PORTE.1
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 Reset As PORTC.0
Dim x As Byte


Sub Int_Timer()
        Timer.Initialize(1)
        Timer.Items(0).interval=1  //ms
        Timer.Items(0)[email protected]  //timer event handler
    //enable timers
        Timer.Items(0).enabled=true
        If GO=0 Then   //insert this into all the sub sequences but only last key in sequence
        Timer.Stop       //need to insert GETKEY
        End If
End Sub
 //RED LIGHT FOUL
 Sub Foul()
    If GO=0 And green = 0 Then
    red=1
    WriteAt(1,1,"FOUL" )
    DelayMS(3000)
        blue1=0 //led is off
        blue2=0
        yellow1=0
        yellow2=0
        YELLOW3=0
        green=0
        red=0
    Cls
EndIf
End Sub

// OnTimer event, flash LED...
Sub OnTimer()
    Inc(mS)
End Sub

Sub ET_time() //dis[;ay results
        Timer.Stop //stop timer
        Cls
        s=DecToStr(mS/1000)+"."+DecToStr((mS Mod 1000))
        WriteAt(1,1,"reaction     ")            //display players score
        WriteAt(2,1,"seconds   ",s,"     ")
         DelayMS(5000)
         Cls
End Sub

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

Output (blue1)
Output(blue2)
Output(yellow1)
Output(yellow2)
Output(YELLOW3)
Output (green)
Output(red)
Input(GO)     //button press
GO = 1    //input
blue1=0   //led
blue2=0   //led
green = 0 //led
mS=0      //timer set to zero

// initialise the timer module...
Timer.Initialize(1)
// initialise timer.items(0)
Timer.Items(0).Interval = 1 // 1mS
Timer.Items(0).OnTimer = @OnTimer // timer event handler
// enable the timers...
Timer.Items(0).Enabled = true

SetAllDigital

//START OF GAME
While True
'mS = 0
blue1=1
If GO=0 and green=0
goto skipcode

    DelayMS(1000)
    blue2=1
    If GO=0 and green=0
    goto skipcode
    
    
        DelayMS(1000)
        yellow1=1
        If GO=0 and green=0
        goto skipcode
        
            DelayMS(500)
            yellow2=1
            If GO=0 and green=0                   
            goto skipcode
        
               DelayMS(500)
                YELLOW3=1
                If GO=0 and green=0
                goto skipcode
                
                   DelayMS(500)
                    If GO=0 And green = 0 //Then  //green = 0
                    goto skipcode
                    delayms(200)
    
    green=1
    timer.start
    repeat
    Until
    go = 0
    ET_time()
    ms=0
    skipcode:
     foul()
     end_of_game()
                    
 Wend
 

MrDEB

Well-Known Member
Here is the swordfish code still under construction and a 1 player schematic
Code:
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2020 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 10/23/2020                                                     *
*  Version : 1.0                                                            *
*  Notes   : NEED TO ADD A SECOND PLAYER AND ROTARY SWITCH FOR SETTING
THE CHRISTMAS TREE TIMING.
A "SHIFT LIGHT" WOULD BE A NICE ADDITION. aDD TO MS/RT FOR MISSED SHIFTS USING A FOR NEXT LOOP?                                                               *
* BASICALLY MEASURE THE PLAYER REACTION TIME 5X AND ADD ALL TOGETHER         :                                                                *
*****************************************************************************
}
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 As PORTC.2
Dim green As PORTE.0
Dim red As PORTE.1
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 GREEN
Dim RT_2 As LongWord   //REACTION TIME 1-2 SHIFT
Dim RT_3 As LongWord   //REACTION TIME  2-3
Dim RT_4 As LongWord   //REACTION TIME  3-4
Dim RT_5 As LongWord   //REACTION TIME  4-5 SHIFT
Dim Totals As LongWord


Sub Int_Timer()
        Timer.Initialize(1)
        Timer.Items(0).interval=1  //ms
        Timer.Items(0)[email protected]  //timer event handler
    //enable timers
        Timer.Items(0).enabled=true
        If GO=0 Then   //insert this into all the sub sequences but only last key in sequence
        Timer.Stop       //need to insert GETKEY
        End If
End Sub
 //RED LIGHT FOUL
 Sub Foul()
    If GO=0 And green = 0 Then
    red=1
    WriteAt(1,1,"FOUL" )
    DelayMS(3000)
        blue1=0 //led is off
        blue2=0
        yellow1=0
        yellow2=0
        YELLOW3=0
        green=0
        red=0
    Cls
EndIf
End Sub

// OnTimer event, flash LED...
Sub OnTimer()
    Inc(mS)
End Sub

Sub ET_time() //dis[;ay results
        Timer.Stop //stop timer
        Cls
        s=DecToStr(mS/1000)+"."+DecToStr((mS Mod 1000))
        WriteAt(1,1,"reaction     ")            //display players score
        WriteAt(2,1,"seconds   ",s,"     ")
        EE.Write(6,RT_1)
         DelayMS(5000)
         Cls
End Sub

Sub Total_ET()
  EE.Read(11,Totals)
End Sub

Sub End_of_game()
    blue1 = 0 //led is off
    blue2=0
    yellow1=0
    yellow2=0
    YELLOW3=0
    green=0
    red=0
DelayMS(2000)
End Sub
Output(Shift_led)
Output (blue1)
Output(blue2)
Output(yellow1)
Output(yellow2)
Output(YELLOW3)
Output (green)
Output(red)
Input(GO)     //button press
Input(S_press)//shift button
GO = 1    //input
blue1=0   //led
blue2=0   //led
green = 0 //led
mS=0      //timer set to zero
Shift_led=1  //this is wired wrong



// initialise the timer module...
Timer.Initialize(1)
// initialise timer.items(0)
Timer.Items(0).Interval = 1 // 1mS
Timer.Items(0).OnTimer = @OnTimer // timer event handler
// enable the timers...
Timer.Items(0).Enabled = true

SetAllDigital

//START OF GAME
While True
Shift_led=0
DelayMS(1000)
Shift_led=1
DelayMS(1000)
'mS = 0
blue1=1
If GO=0 And green=0
GoTo skipcode

    DelayMS(1000)
    blue2=1
    If GO=0 And green=0
    GoTo skipcode
    
    
        DelayMS(1000)
        yellow1=1
        If GO=0 And green=0
        GoTo skipcode
        
            DelayMS(500)
            yellow2=1
            If GO=0 And green=0                   
            GoTo skipcode
        
               DelayMS(500)
                YELLOW3=1
                If GO=0 And green=0
                GoTo skipcode
                
                   DelayMS(500)
                    If GO=0 And green = 0 //Then  //green = 0
                    GoTo skipcode
                    DelayMS(200)
    
    green=1
    Timer.Start
    Repeat
    Until
    GO = 0
    ET_time()
    
    DelayMS(1500)
    Shift_led=0   //LED ON
    Timer.Start     //timing of 1st shift
    
    If S_press=0 Then   //shift button
    
    Shift_led=1 //SHIFT LED OFF HAVE THIS WIRED WRONG ON PERFBOARD
    DelayMS(1500)            //1.5 SECONDS
    Toggle(Shift_led)  //MEASURE THE REACTION TIME RT_1
    EndIf
    WriteAt(1,1,"RT   ",s,"     ")
    WriteAt(2,1,"     ",DecToStr(RT_2/1000)+"."+DecToStr((RT_2 Mod 1000)))
    DelayMS(5000)
//INSERT SHIFT LED HERE ADDING TO TIME FOR MISSED SHIFTS (IN A FOR NEXT LOOP )
    //ET_TIME()
    EE.Write(6,RT_1) //WRITE ET OF GREEN TO 1ST SHIFT
    
    mS=0
    skipcode:
     Foul()
     End_of_game()
                    
 Wend
 

Attachments

Pommie

Well-Known Member
Most Helpful Member
Do you see anything wrong with these two lines?
Code:
Input(GO)     //button press
GO = 1    //input
Mike.
 

Visitor

Active Member
Do you see anything wrong with these two lines?
Code:
Input(GO)     //button press
GO = 1    //input
Mike.
It has been pointed out dozens of times that you can't set the value of an input – the level of an input is whatever is connected to that input.

But it's been ignored n + 1 times now.
 

MrDEB

Well-Known Member
I know but old habits are hard to break.
QUESTION how many timers can I use in one program. I recall from the DIGITAL DIY site a timer with 20+ timers?
am contemplating using a FOR NEXT loop and stopping when a button is pressed and extracting the value of X but how does this compare to using the actual timer function that I am using for reaction time?
After it warms up a little I plan to compare the two methods. It's cold in my back porch.
 

Visitor

Active Member
You might look in the Swordfish help file to search about timers. It's a click away on the Swordfish menu bar.

Rather than using a bunch of timers, the more traditional way would be to use one timer to increment a "master time" variable, and to note beginning and end times of each event.

You seem not to understand why you can't assign a value to an input port pin. You might want to take time to understand why that is.
 

tumbleweed

Active Member
ok, here's how you can handle multiple elapsed times using a single timer.

First, try a single time...
Code:
dim mS as word = 0          // this is the timer count value. initialize it to 0 on startup
dim StartTime, EndTime as word    // these are for the first elapsed time

// OnTimer event
Sub OnTimer()
    Inc(mS)
End Sub

// display time
Sub ET_time(msecs as word)
    dim s as string
    Cls
    s = DecToStr(msecs/1000) + "." + DecToStr((msecs Mod 1000))
    WriteAt(1,1,"reaction     ")
    WriteAt(2,1,"seconds   ", s ,"     ")
End Sub


// initialise the timer module...
Timer.Initialize(1)
// initialise timer.items(0)
Timer.Items(0).Interval = 1 // 1mS
Timer.Items(0).OnTimer = @OnTimer // timer event handler
// enable the timers...
Timer.Items(0).Enabled = true
Now, to time something add:
Code:
// record starting time (current mS value)
StartTime = mS

// do whatever....

// record end time (current mS value)
EndTime = mS 

// display elapsed time
ET_time(EndTime - StartTime)
Don't reset mS anywhere, and don't stop the timer.

If you want to have multiple "timers" then just define more start and end time variables...
Code:
dim StartTime, EndTime as word      // these are for the first elapsed time
dim StartTime2, EndTime2 as word      // these are for the second elapsed time
dim StartTime3, EndTime3 as word      // these are for the third elapsed time

// to record the starting times, just set the start = current mS...
StartTime = mS
StartTime2 = mS
StartTime3 = mS

// and then later when you want to stop recording time set endtime = current mS
EndTime = mS
EndTime2 = mS
EndTime3 = mS

// you can then display them when you want
ET_time(EndTime - StartTime)
ET_time(EndTime2 - StartTime2)
ET_time(EndTime3 - StartTime3)
Declaring all the "time" variables as a word will let you count up to 65535 msecs (65.535 secs)
If you want a longer time then change all the "word" declarations to "longword"
 

MrDEB

Well-Known Member
will give a try.
Am attempting to get my wife's computer to set up a printer but computer won't communicate w/ computer?
Will try code suggestions maybe tonight or first thing tomorrow morning. My head hurts with wife's printer issues.
 

Nigel Goodwin

Super Moderator
Most Helpful Member
will give a try.
Am attempting to get my wife's computer to set up a printer but computer won't communicate w/ computer?
Will try code suggestions maybe tonight or first thing tomorrow morning. My head hurts with wife's printer issues.
Are you connecting via a cable or WiFi?.
 

MrDEB

Well-Known Member
a USB cable. Tried downloading the drivers but none for a hp laserjet p110w
the printer has capabilities to use wireless but that doesn't connect either. Have shut the computer OFF and going to restart. Did the RESTART before (just restart) and keeps coming up with a hp disk drive?
 

Nigel Goodwin

Super Moderator
Most Helpful Member
a USB cable. Tried downloading the drivers but none for a hp laserjet p110w
the printer has capabilities to use wireless but that doesn't connect either. Have shut the computer OFF and going to restart. Did the RESTART before (just restart) and keeps coming up with a hp disk drive?
Don't know why you would think that?.


There's also a video showing how to install it.

For a start unplug the printer, delete any relevent entries in 'Printers', uninstall any attempts you've already made - and then follow EXACTLY the instructions from HP. Don't connect the USB lead until it tells you to.
 

MrDEB

Well-Known Member
printer still comes up as a cd player
tried the suggestions made by tumbleeweeds but keep getting errors.
attempting to correct or edit but not getting much accomplished.
will keep pluging away.
I fail to see anywhere where the timer is started? ONTIMER
 

Pommie

Well-Known Member
Most Helpful Member
He's still tossing those lines out and getting some bites. Note, he doesn't answer my posts anymore (but I may be on his ignore list, here's hoping.). CD player indeedy! :D

Mike.
 

Visitor

Active Member
I fail to see anywhere where the timer is started? ONTIMER
"Now, Dasher! now, Dancer! now Prancer and Vixen!
On, Comet! on, Cupid! on, Donner and Blitzen!"


About those help files..... OnTimer has nothing to do with starting a timer.

Code:
// OnTimer event
Sub OnTimer()
    Inc(mS)
End Sub
Tumbleweed has spelled it out as clearly as possible. Use his code exactly as written. OnTimer is an event that happens when the timer reaches its specified count. The interval specified is 1 mS – when the timer reaches 1 mS, it does what's specified in the OnTimer Event, which in this case is to increment the variable mS.

Read the help files.
 

tumbleweed

Active Member
I fail to see anywhere where the timer is started?
My bad... I assumed you had a Timer.Start statement in there somewhere.

Here's the initialization spelled out...
Code:
// initialise the timer module...
Timer.Initialize(1)
// initialise timer.items(0)
Timer.Items(0).Interval = 1 // 1mS
Timer.Items(0).OnTimer = @OnTimer // timer event handler
// enable the timer
Timer.Items(0).Enabled = true
// start interrupt processing
mS = 0
Timer.Start()
If you're using an older version of Swordfish then this might cause you problems...
Code:
dim mS as word = 0
Initialization is a fairly new feature, so you might have to drop the '= 0' part
I added setting mS = 0 in the code above just in case, but for elapsed time it really doesn't matter if you initialize mS or not
(although the compiler might complain if it doesn't see it initialized anywhere).
 

Latest threads

EE World Online Articles

Loading
Top