Tuesday, May 13, 2014

Audio (Waveform Peak) to LED Display with PLC and Visual Basic




This application use visual basic for convert audio (waveform Peak) into digital output. And digital output send to PLC and then force on/off to LED Display. This application can also are used to control the lighting for a music concert, control the lighting for a music studio, control the lighting for a music festival, and other applications.
Video about Audio Waveform Peak to LED Display

Hardware and software that is needed at the PLC to Computer
I use This Hardware for audio to LED Display with PLC,LED and PLC Cable.
Hardware for audio to LED Display with PLC,LED and PLC Cable

Additional hardware options:
1. LED (Light Emitting Diode) for LED Display / LED Bar Display or other
2. Lamps for lighting music concert or other

Wiring Diagram of LED to PLC Output:
Wiring Diagram of LED to PLC Output

Wiring Diagram of Lamps to PLC Output:
Wiring Diagram of Lamps to PLC Output

Example: Audio and PLC control the Lighting for a Music Concert
lamps for lighting music concert

Visual Basic Code of Audio Waveform(Peak) :
1. Use DLL: CoreAudioApi
2. Download Project: click here
3. Change: Serial Port Number
Change Serial Port Number

4. Main Menu
menu serial number error

menu serial number ok
Remarks of main menu:
1. Serial port number is not connected
2. PLC output Setting:
2.1. Auto: PLC Outputs following the peak value
2.2. All OFF: All PLC Outputs are OFF
2.2. All ON: All PLC Outputs are ON
3. Serial port number is connected
4. Adjustment the peak value
5. Adjustment the peak value minimum for PLC Output is ON
6.PLC Output Display

5. Project Code of Visual Basic 2010:
Imports CoreAudioApi
Imports System.Threading

Public Class Form1
    Friend Device As MMDevice
    Friend Audio_Devices_Enumerator As New MMDeviceEnumerator()
    Dim Q0val, Q1val As Byte

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.PortName = "COM4"  'Serial Port Number Setting
        Try
            SerialPort1.Open()
        Catch ex As Exception
            Me.Text = ex.Message
        End Try

        Device = Audio_Devices_Enumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
        Q0val = 255
        Q1val = 255
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ''MasterPeak
        TextBox1.Text = CStr(Device.AudioMeterInformation.MasterPeakValue * 10 * (1 + (0.1 * TrackBar1.Value)))
        ''LeftPeak
        TextBox2.Text = CStr(Device.AudioMeterInformation.PeakValues(0) * 10 * (1 + (0.1 * TrackBar1.Value)))
        ''RightPeak
        TextBox3.Text = CStr(Device.AudioMeterInformation.PeakValues(1) * 10 * (1 + (0.1 * TrackBar1.Value)))
        ''progress bar
        If CInt(TextBox1.Text) > 100 Then ProgressBar1.Value = 100 Else ProgressBar1.Value = CInt(TextBox1.Text)
        If CInt(TextBox2.Text) > 100 Then ProgressBar2.Value = 100 Else ProgressBar2.Value = CInt(TextBox2.Text)
        If CInt(TextBox3.Text) > 100 Then ProgressBar3.Value = 100 Else ProgressBar3.Value = CInt(TextBox3.Text)


    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        If autoset.Checked = True Then
            If set00.Value <= ProgressBar2.Value And set00.Value <> 0 Then out00.Checked = True Else out00.Checked = False
            If set01.Value <= ProgressBar2.Value And set01.Value <> 0 Then out01.Checked = True Else out01.Checked = False
            If set02.Value <= ProgressBar2.Value And set02.Value <> 0 Then out02.Checked = True Else out02.Checked = False
            If set03.Value <= ProgressBar2.Value And set03.Value <> 0 Then out03.Checked = True Else out03.Checked = False
            If set04.Value <= ProgressBar2.Value And set04.Value <> 0 Then out04.Checked = True Else out04.Checked = False
            If set05.Value <= ProgressBar2.Value And set05.Value <> 0 Then out05.Checked = True Else out05.Checked = False
            If set06.Value <= ProgressBar2.Value And set06.Value <> 0 Then out06.Checked = True Else out06.Checked = False
            If set07.Value <= ProgressBar2.Value And set07.Value <> 0 Then out07.Checked = True Else out07.Checked = False

            If set10.Value <= ProgressBar2.Value And set10.Value <> 0 Then out10.Checked = True Else out10.Checked = False
            If set11.Value <= ProgressBar2.Value And set11.Value <> 0 Then out11.Checked = True Else out11.Checked = False
            If set12.Value <= ProgressBar2.Value And set12.Value <> 0 Then out12.Checked = True Else out12.Checked = False
            If set13.Value <= ProgressBar2.Value And set13.Value <> 0 Then out13.Checked = True Else out13.Checked = False
            If set14.Value <= ProgressBar2.Value And set14.Value <> 0 Then out14.Checked = True Else out14.Checked = False
            If set15.Value <= ProgressBar2.Value And set15.Value <> 0 Then out15.Checked = True Else out15.Checked = False
            If set16.Value <= ProgressBar2.Value And set16.Value <> 0 Then out16.Checked = True Else out16.Checked = False
            If set17.Value <= ProgressBar2.Value And set17.Value <> 0 Then out17.Checked = True Else out17.Checked = False
        ElseIf alloffset.Checked = True Then
            out00.Checked = False
            out01.Checked = False
            out02.Checked = False
            out03.Checked = False
            out04.Checked = False
            out05.Checked = False
            out06.Checked = False
            out07.Checked = False

            out10.Checked = False
            out11.Checked = False
            out12.Checked = False
            out13.Checked = False
            out14.Checked = False
            out15.Checked = False
            out16.Checked = False
            out17.Checked = False
        ElseIf allonset.Checked = True Then
            out00.Checked = True
            out01.Checked = True
            out02.Checked = True
            out03.Checked = True
            out04.Checked = True
            out05.Checked = True
            out06.Checked = True
            out07.Checked = True

            out10.Checked = True
            out11.Checked = True
            out12.Checked = True
            out13.Checked = True
            out14.Checked = True
            out15.Checked = True
            out16.Checked = True
            out17.Checked = True
        End If

        Dim leftval, rightval As Byte
        leftval = (Int(out00.Checked) * 1) + (Int(out01.Checked) * 2) + (Int(out02.Checked) * 4) + (Int(out03.Checked) * 8) + (Int(out04.Checked) * 16) + (Int(out05.Checked) * 32) + (Int(out06.Checked) * 64) + (Int(out07.Checked) * 128)
        rightval = (Int(out10.Checked) * 1) + (Int(out11.Checked) * 2) + (Int(out12.Checked) * 4) + (Int(out13.Checked) * 8) + (Int(out14.Checked) * 16) + (Int(out15.Checked) * 32) + (Int(out16.Checked) * 64) + (Int(out17.Checked) * 128)

        If Q0val = leftval And Q1val = rightval Then Return

        Dim str_write(0 To 38) As Byte
        Dim Temp_FCS As VariantType
        Dim i As Long

        If SerialPort1.IsOpen = False Then Return
        Timer2.Enabled = False

        str_write(0) = &H68
        str_write(1) = 33 'count=2+31
        str_write(2) = 33 'count=2+31
        str_write(24) = &H2 'count=2
        str_write(16) = &H6 'count+4
        str_write(33) = (2 * 8) \ 256 'count*8/256
        str_write(34) = (2 * 8) Mod 256 'count*8 mod 256

        str_write(29) = &H0 'Q start from Q0.0 to Q1.7
        str_write(30) = 0 * 8 'Q start from Q0.0 to Q1.7

        str_write(3) = &H68
        str_write(4) = &H2
        str_write(5) = &H0
        str_write(6) = &H7C
        str_write(7) = &H32
        str_write(8) = &H1
        str_write(9) = &H0
        str_write(10) = &H0
        str_write(11) = &H43
        str_write(12) = &H1
        str_write(13) = &H0
        str_write(14) = &HE
        str_write(15) = &H0

        str_write(17) = &H5
        str_write(18) = &H1
        str_write(19) = &H12
        str_write(20) = &HA
        str_write(21) = &H10
        str_write(22) = &H2 'len = 2
        str_write(23) = &H0

        str_write(25) = &H0
        str_write(26) = &H0  ' type q
        str_write(27) = &H82  'type q
        str_write(28) = &H0

        str_write(31) = &H0
        str_write(32) = &H4

        str_write(35) = leftval 'Q value0 = Q0.0 to Q0.7
        str_write(36) = rightval 'Q value1 = Q1.0 to Q1.7

        For i = 4 To 36
            Temp_FCS = Temp_FCS + str_write(i)
        Next
        str_write(37) = Temp_FCS Mod 256
        str_write(38) = &H16
        If (SerialPort1.IsOpen = True) Then
            ' Send the binary data out the port
            SerialPort1.Write(str_write, 0, str_write.Length)

            Thread.Sleep(100) 'wait 100ms

            End_comm() 'end Communication
            Q0val = leftval
            Q1val = rightval
        End If

        Thread.Sleep(100) 'wait 100ms
        If (SerialPort1.IsOpen = True) Then SerialPort1.ReadExisting()
        Timer2.Enabled = True
    End Sub

    Private Sub End_comm()
        'End Communication
        Dim str_val(0 To 5) As Byte

        SerialPort1.ReadExisting()

        str_val(0) = &H10
        str_val(1) = &H2
        str_val(2) = &H0
        str_val(3) = &H5C
        str_val(4) = &H5E
        str_val(5) = &H16
        If (SerialPort1.IsOpen = True) Then
            SerialPort1.Write(str_val, 0, str_val.Length)
        End If
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        If SerialPort1.IsOpen = True Then SerialPort1.Dispose()
    End Sub
End Class





Labels:






Newer Post Older Post Home

You may also like these ebook:

Get Free PLC eBook directly sent to your email,
and email subscription to program-plc.blogspot.com




We hate SPAM. Your information is never sold or shared with anyone.

Your Email Will Be 100% Secured !