Hello @dn
here are some snips of my code (Excel 2007, VBA).
[color=#0000BF]Code part on Close:[/color]
CloseDevices
[color=#0000FF]Code part on Open:[/color]
CloseDevices
'Open channel to HW
hwAdr = OpenDevice()
If hwAdr < 0 Then
MsgBox ("Kanal zur USB I/O-Karte konnte nicht geöffnet werden!")
End
End If
[color=#0000FF]Called every 5 secs:[/color]
Sub RefreshRelais()
Dim i As Integer
Dim b As Boolean
'refresh relais outputs
Range("RelOutFirstBit").Select
For i = 0 To 31
If ActiveCell.Value <> 0 Then
b = False
Else
b = True
End If
sendBit (b)
ActiveCell.Offset(0, -1).Select
Next i
'Now takeover all 32 bits in Ginnes Hardware (Shift register behind Velleman HW)
nixnutz = ClearDigitalChannel(hwAdr, 3)
nixnutz = SetDigitalChannel(hwAdr, 3)
'Wait a bit to get stable analog data
Application.Wait (Now + TimeValue("0:00:01"))
End Sub
[color=#008000]Rem: …to bring digital data into a shift register behind the Velleman HW[/color]
Sub sendBit(setting As Boolean)
Dim b As Boolean
If setting = True Then
nixnutz = ClearDigitalChannel(hwAdr, 1)
Else
nixnutz = SetDigitalChannel(hwAdr, 1)
End If
'generate the clockpulse
nixnutz = ClearDigitalChannel(hwAdr, 2)
nixnutz = SetDigitalChannel(hwAdr, 2)
End Sub
[color=#0000FF]This is called every 15 minutes:
3 Levels each 7 roos. I always read 10 times, kick away the lowest and the highest value (followed by an averaging of the other 8 measurements)[/color]
Sub ReadAdChannels_averaging()
Dim i As Integer
Dim k As Integer
Dim l As Integer
Dim n As Integer 'pointer to minimum
Dim x As Integer 'pointer to maximum
Dim minVal As Integer 'to store curr. minimum
Dim maxVal As Integer 'to store curr. maximum
For i = 0 To 2
'Do for every level
SelectLevel (i)
Range("SingleRawBase").Select
ActiveCell.Offset(0, i * 7).Select
For k = 0 To 6
'Do for every room
minVal = 2000 'reset
maxVal = 0 'reset
For l = 0 To 9
ActiveCell.Offset(l, 0).Value = ReadAdChannel(k)
If ActiveCell.Offset(l, 0).Value < minVal Then
minVal = ActiveCell.Offset(l, 0).Value
n = l ' set pointer
End If
If ActiveCell.Offset(l, 0).Value > maxVal Then
maxVal = ActiveCell.Offset(l, 0).Value
x = l ' set pointer
End If
Next l
'now do averaging for this channel
ActiveCell.Offset(n, 0).Value = 0 'don´t use the minimum value
ActiveCell.Offset(x, 0).Value = 0 'don´t use the maximum value
'Special case: 10 times the same value
If n = x Then
ActiveCell.Offset(1, 0).Value = 0 'clear any second value!
End If
ActiveCell.Offset(0, 1).Select
Next k
Next i
UnSelectLevel
End Sub
[color=#0000FF]Commonly used:[/color]
Function ReadAdChannel(i As Integer) As Integer
Dim locvar As Long
Dim locvarErg As Long
locvar = i + 1
locvarErg = ReadAnalogChannel(hwAdr, locvar)
If locvarErg > 1023 Then
locvarErg = locvarErg - 1 'just for Breakpoint in case of THIS PROBLEM HAPPENS
End If
ReadAdChannel = locvarErg
End Function