Control 8 LEDs using parallel port in vb-VBForums
Results 1 to 4 of 4

Thread: Control 8 LEDs using parallel port in vb

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    1

    Control 8 LEDs using parallel port in vb

    I need to control 8 LEDs. each led have an On button and an Off button.

    I used this code:

    to turn on led 1
    Private Sub Command1_Click()
    Port1 = 888
    Out Port1, 1
    End Sub

    to turn on led 2
    Private Sub Command2_Click()
    Port1 = 888
    Out Port1, 2
    End Sub

    to turn on led 3
    Private Sub Command3_Click()
    Port1 = 888
    Out Port1, 4
    End Sub

    to turn on all leds
    Private Sub Command4_Click()
    Port1 = 888
    Out Port1, 255
    End Sub

    to turn off all leds
    Private Sub Command5_Click()
    Port1 = 888
    Out Port1, 0
    End Sub

    When I run the code and want to turn on 2 or 3 or 4 the LEDs at once, I can only turn on one LED at a time. Each time I click the next ON button, the LED chosed before turns off and the new chosen LED turns on.

    And what's the code to off each LED?

  2. #2
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,452

    Re: Control 8 LEDs using parallel port in vb

    Reading through this thread might help.

    http://www.vbforums.com/showpost.php...0&postcount=27
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  3. #3
    Addicted Member
    Join Date
    Sep 2009
    Posts
    190

    Re: Control 8 LEDs using parallel port in vb

    @mohdh..., hello. It is strongly recommended for you to teach something about using of VB6's "control array" and then you can make more efficient program like this with just few lines of code.
    Which led's will be turned on or off depends about logical (and so electrical) state of bits in byte which you send to port.
    If 255 turns on all led's in then 254 turns first led off, 253 second, 252 first and second and so on following the logic of say binary counter.
    That mean that for turning leds you just need to toggle state of certain bit in byte you send. For this it would be useful to read port too, not just write.
    Unfortunately, VB6 havent native functions for binary conversions but you can easily make you routines for such a job or find ready ones on the net.
    I find something useful for start at fast.

    Code:
    Public Function BinToDec(BinNum As String) As String
       Dim i As Integer
       Dim DecNum As Long
       
       On Error GoTo ErrorHandler
       
    '  Loop thru BinString
       For i = Len(BinNum) To 1 Step -1
    '     Check the string for invalid characters
          If Asc(Mid(BinNum, i, 1)) < 48 Or _
             Asc(Mid(BinNum, i, 1)) > 49 Then
             DecNum = ""
             Err.Raise 1002, "BinToDec", "Invalid Input"
          End If
    '     If bit is 1 then raise 2^LoopCount and add it to DecNum
          If Mid(BinNum, i, 1) And 1 Then
             DecNum = DecNum + 2 ^ (Len(BinNum) - i)
          End If
       Next i
    '  Return DecNum as a String
       BinToDec = DecNum
    ErrorHandler:
    End Function
    
    Public Function DecToBin(DecNum As String) As String
       Dim BinNum As String
       Dim lDecNum As Long
       Dim i As Integer
       
       On Error GoTo ErrorHandler
       
    '  Check the string for invalid characters
       For i = 1 To Len(DecNum)
          If Asc(Mid(DecNum, i, 1)) < 48 Or _
             Asc(Mid(DecNum, i, 1)) > 57 Then
             BinNum = ""
             Err.Raise 1010, "DecToBin", "Invalid Input"
          End If
       Next i
       
       i = 0
       lDecNum = Val(DecNum)
       
       Do
          If lDecNum And 2 ^ i Then
             BinNum = "1" & BinNum
          Else
             BinNum = "0" & BinNum
          End If
          i = i + 1
       Loop Until 2 ^ i > lDecNum
    '  Return BinNum as a String
       DecToBin = BinNum
    ErrorHandler:
    End Function
    Simply use this functions like:

    Code:
    OUT Port1,  BinToDec("10110101")
    or whatewer you need for knowing what you send to port. Where is "1" led will be turned on and opposite (or inverted, depends...).

  4. #4
    Addicted Member
    Join Date
    Feb 2009
    Posts
    145

    Re: Control 8 LEDs using parallel port in vb

    Hi, you need to add the binary numbers!

    so a value of 1 turns on LED one
    2 turns on LED 2
    4 turns on LED 3

    BUT

    If you want led 1 AND 2, the value should be 3 (value 1 plus 2)
    if you want LED 1 AND 2 AND 3, then you need a value of 7 (1, plus 2, plus 4)

    does that make sense? sorry, bit hungover this morrning, if not, let me know and i'll explain further!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.