I had some difficulties to understand the Liberty Basic code.
Anyhow I think I know what you want it to do.
I made an example code using the Visual Basic in Excel.
Here is the screenshot of the result:
Pressing the buttons are used to emulate the phototransistor operation.
This is the “core” of the lap time counter procedure:
Dim data As Long
Dim diff As Long
Dim i As Long
timecounter = timecounter + 1
data = ReadAllDigital()
If data > old_data Then ' check if any one of the digital inputs is activated (pulled down by the phototransistor or switch)
diff = data - old_data
For i = 0 To 4
If (diff And 2 ^ i) > 0 Then ' find the input who changed state
lap(i) = lap(i) + 1 ' increment lap counter
ActiveSheet.Cells(lap(i) + 1, i + 2) = 0.01 * (timecounter - time(i)) ' print the lap time in seconds on sheet
time(i) = timecounter ' save the time counter value for this channel
End If
Next i
End If
old_data = data
This routine is inside a 10ms timer event handler.
The digital input of the K8055 is read.
The input value is compared to the previous input value.
If any one of the input channels has changed state from 0 to 1 then the input is checked more detailed.
The time counter value assigned to the input(s) who changed state is loaded with the global time counter value.
The lap time is printed on the Excel sheet.
Here is the complete script:
[code]Option Explicit
Private Declare Function OpenDevice Lib “k8055d.dll” (ByVal CardAddress As Long) As Long
Private Declare Sub CloseDevice Lib “k8055d.dll” ()
Private Declare Function ReadAllDigital Lib “k8055d.dll” () As Long
Private Declare Function SetTimer Lib “user32” ( _
ByVal HWnd As Long, ByVal nIDEvent As Long, _
ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib “user32” ( _
ByVal HWnd As Long, ByVal nIDEvent As Long) As Long
Dim TimerID As Long
Dim TimerSeconds As Single
Dim time(0 To 4) As Long
Dim lap(0 To 4) As Long
Dim old_data As Long
Dim timecounter As Long
Sub Button1_Click()
Dim h As Long
Dim i As Long
timecounter = 0
old_data = 0
For i = 0 To 4
time(i) = 0
lap(i) = 0
Next i
h = OpenDevice(0)
If h = 0 Then
ActiveSheet.Cells(1, 7) = “Card 0 Connected”
TimerSeconds = 0.01 ’ the timer interval is now 0.01 sec.
TimerID = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf TimerProc)
Else
ActiveSheet.Cells(1, 7) = “Card 0 Not Found”
End If
End Sub
Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, ByVal nIDEvent As Long, ByVal dwTimer As Long)
On Error Resume Next
Dim data As Long
Dim diff As Long
Dim i As Long
timecounter = timecounter + 1
data = ReadAllDigital()
If data > old_data Then ’ check if any one of the digital inpus is activated (pulled down by the phototransistor or switch)
diff = data - old_data
For i = 0 To 4
If (diff And 2 ^ i) > 0 Then ’ find the input who changed state
lap(i) = lap(i) + 1 ’ increment lap counter
ActiveSheet.Cells(lap(i) + 1, i + 2) = 0.01 * (timecounter - time(i)) ’ print the lap time in seconds on sheet
time(i) = timecounter ’ save the time counter value for this channel
End If
Next i
End If
old_data = data
End Sub
Sub Button2_Click()
KillTimer 0&, TimerID
CloseDevice
ActiveSheet.Cells(1, 7) = “Card 0 Closed”
End Sub[/code]
Here is the link to download the Excel workbook including this VBA macro:
box.net/shared/gakgqb7dx6ltnfqbnfqk