Results 1 to 3 of 3

Thread: Pls Help The Poor Me!!!!!!!!!!!!!!

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Posts
    4

    Pls Help The Poor Me!!!!!!!!!!!!!!

    Below is a following code for my previous question. on how to count like from 0000 when we add it will be to 0001 then 0010 then 0011 so on and so forth i would like to know if i had four led
    naming it Led1, Led2, Led3, Led4.. and when i add this time round from 0000 all would be turned off. then when i add Led1, Led2, Led3 will be off but Led4 will be turned on. can someone teach me how to add on to my code below thanks for those who helped me previously. and the code below has a bug when i subtract till all values is 0000 and i sybtract again it will turn to 0001 then i press subtract again it will turn into 0000 it keeps on changing from 0000 to 0001 and then 0000 again ...... but the main aim is about the led anyone can help.


    Option Explicit
    Dim iValue As Integer


    Private Sub CmdAdd_Click()
    If iValue = 15 Then iValue = 0 Else iValue = iValue + 1
    Text1.Text = Format(dec2bin(iValue), "0000")
    End Sub

    Private Sub CmdQuit_Click()
    End
    End Sub

    Private Sub CmdSub_Click()
    If iValue = 15 Then iValue = 0 Else iValue = iValue - 1
    Text1.Text = Format(dec2bin(iValue), "0000")
    End Sub

    Private Sub Form_Load()
    Text1.Text = "0000"
    End Sub

    Public Function dec2bin(mynum As Variant) As String
    Dim loopcounter As Integer
    If mynum >= 2 ^ 31 Then
    dec2bin = "Too big"
    Exit Function
    End If
    Do
    If (mynum And 2 ^ loopcounter) = 2 ^ loopcounter Then
    dec2bin = "1" & dec2bin
    Else
    dec2bin = "0" & dec2bin
    End If

    loopcounter = loopcounter + 1
    Loop Until 2 ^ loopcounter > mynum
    End Function

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    How about something like this?
    VB Code:
    1. Function Dec2Bin(ByVal lDecimal As Variant, Optional ByVal lBits As Long = 0) As String
    2.     Dim lBit As Long
    3.     Dim sBin As String
    4.    
    5.     lBits = IIf(lBits = 0, 31, lBits)
    6.    
    7.     If lDecimal > (2 ^ lBits) Then
    8.         sBin = String(lBits - 1, "1")
    9.         Exit Function
    10.     End If
    11.    
    12.     For lBit = (lBits - 1) To 0 Step -1
    13.         If 2 ^ lBit <= lDecimal Then
    14.             sBin = sBin & "1"
    15.             lDecimal = lDecimal - (2 ^ lBit)
    16.         Else
    17.             sBin = sBin & "0"
    18.         End If
    19.     Next
    20.     Dec2Bin = sBin
    21. End Function
    Example Usage:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim lIndex As Long
    3.     Dim tTimer As Single
    4.    
    5.     For lIndex = 0 To (2 ^ 4) - 1
    6.         Caption = Dec2Bin(lIndex, 4)
    7.         tTimer = Timer
    8.         Do While (Timer - tTimer) < 0.2
    9.             DoEvents
    10.         Loop
    11.     Next
    12. End Sub

  3. #3
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    just a very minor addition for sqapg. Prob best not to use the IIF function and to replace with IF. Dont know if it still does but i remember that IIF caused probs in earlier VB versions and also required adding an extra reference.
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

Posting Permissions

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



Click Here to Expand Forum to Full Width