K8055 programming a loop for digital channels with VBA

Hi,

currently I’m making first steps on programming applications with VBA for the K8055 board.

There is one thing which doesn’t work at the moment:

I want to have a commandbutton within excel or an userform which activates a small routine an loops until it is pressed again.

Example:

Button_Click()

Do

SetDigitalChannel (1)
sleep 5
ClearDigitalChannel (1)
sleep 5

loop until i = true


Everytime I try to run this as a test, excel freezes after a few seconds.

Is it not possible to write loops for the digital channels?

Hope to hearing from you guys soon.

Best regards!

[quote]Is it not possible to write loops for the digital channels?[/quote] No, it is not possible, the loop always freezes the Excel.
You may use timer instead.
Please see the Excel example in the latest software package for the K8055 “Complete SDK Pack (Rev 4.0)”:
velleman.eu/support/download … 8055&type=

Hi,

thank you so much for supporting me.

Could you please be so kind as to explain what causes the excel freeze?
Is it related to dll limitations or is it a hardware based limitation?

is there a possibility to get loops enabled in the near future?

OK. Now it works.
The trick was to put “DoEvents” to the loop.
Here is the full working code.
There are now two buttons.
First button opens the card.
The second button starts LD1 to flash.
Next press stops flashing.

Option Explicit Private Declare Function OpenDevice Lib "k8055d.dll" (ByVal CardAddress As Long) As Long Private Declare Sub CloseDevice Lib "k8055d.dll" () Private Declare Sub SetDigitalChannel Lib "k8055d.dll" (ByVal Channel As Long) Private Declare Sub ClearDigitalChannel Lib "k8055d.dll" (ByVal Channel As Long) Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Dim i As Long Sub Button_Open_Click() Dim h As Long h = OpenDevice(0) If h = 0 Then ActiveSheet.Cells(1, 2) = "Card 0 opened" Else ActiveSheet.Cells(1, 2) = "Card 0 not found" End If i = 0 End Sub Sub Button2_Click() i = i Xor 1 ActiveSheet.Cells(1, 1) = "Wait" Do SetDigitalChannel (1) Sleep 100 ClearDigitalChannel (1) Sleep 100 DoEvents Loop Until i = 0 ActiveSheet.Cells(1, 1) = "OK" End Sub

Hi,

that works just fine!!!

Thank you so much for support.

Best regards
Heiko

If you mean MS Excel, then your loop will basically continue looping and hogging CPU usage. This will mean that excel cannot redraw / refresh the window and handle user input because it is unable to process any event handlers internally.

Any code you write either needs to execute and finish quickly or make appropriate calls to handle the normal background events itself. This principle applies to any programming language and is the reason why most program loops have a small sleep / delay in them so that the system can continue to execute other background tasks.