Arduino + Visual Studio (LDR and LED)

PaulS

Thank you very much for your reply. You are right, VS is the Software where I develop my application which communicates with the Arduino. I am sorry that I did not explain myself correctly.

Here is the code of my App in Visual Studio.

Imports System.IO.Ports
Imports System.IO
Imports System.Threading


Public Class Form3

    Shared _continue As SerialPort
    Shared _serialPort As Boolean

    Private Sub GetSerialPortNames()
        Timer1.Enabled = False
        For Each sp As String In My.Computer.Ports.SerialPortNames
            cmbPort.Items.Add(sp)
        Next
    End Sub


    Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim str As String = SerialPort1.ReadExisting()
    End Sub

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim BaudRates() As String = {"300", "1200", "2400", "4800", "9600", "14400", "19200", "28800", "38400", "57600", "115200"}
        cmbBaud.Items.AddRange(BaudRates)
        cmbBaud.SelectedIndex = 4
        Try
            GetSerialPortNames()
            cmbPort.SelectedIndex = 0
        Catch
            MessageBox.Show("ARDUINO NOT CONECTED, PLEASE CLOSE AND TRY AGAIN")
        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Form1.Show()
        Me.Hide()
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        Timer1.Enabled = True
        Try
            SerialPort1.BaudRate = cmbBaud.SelectedItem.ToString
            SerialPort1.PortName = cmbPort.SelectedItem.ToString
            SerialPort1.Open()
            If SerialPort1.IsOpen Then
                Button7.Visible = True
                cmbPort.Enabled = False
                cmbBaud.Enabled = False
                Button1.Visible = True
                SerialPort1.DtrEnable = True
                MessageBox.Show("CONECTED")
            End If
        Catch
            SerialPort1.Close()
            MessageBox.Show("THERE IS NOT ANY COM")
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            SerialPort1.Close()
            Button7.Visible = True
            Button1.Visible = True
            cmbPort.Enabled = True
            cmbBaud.Enabled = True
            SerialPort1.DtrEnable = False
            MessageBox.Show("DISCONECTED")
            Exit Sub
        Catch
            MessageBox.Show("Error, Try again")
        End Try
    End Sub

    Private Sub Form3_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If SerialPort1.IsOpen() Then
            MessageBox.Show("Disconect before closing")
            e.Cancel = True
        End If
    End Sub

End Class

In VS, Form 3 is in charge of connections, baudrate, COM and disconnections.
(Attached you will find a picture of the Form3)

in VS, Form 4 is in charge of LED ON / OFF.

Public Class Form4

    Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Form1.Show()
        Me.Hide()
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        Form3.SerialPort1.Write("a")
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Form3.SerialPort1.Write("b")
    End Sub
End Class

As you can see, I use the SerialPort declared in Form3 in the Form4 to turn the LED on and off.
(Attached you will find a picture of Form4)

And in Arduino I use this code to get the Data from the Application made in VS to turn LED ON and OFF.

void loop()
{
  digital();
}

void digital()           //This is the part where the value from Visual Studio App is received.
{
  if (Serial.available())
  {
    valor2 = Serial.read();
    if (valor2 == 'a')            //When I pressed a button in Visual Studio, it sends a data whit the value 'a', which is received in the arduino. This is for ON
    {
     digitalWrite(led, HIGH);
    }
    if (valor2 == 'b')            //When I pressed a button in Visual Studio, it sends a data whit the value 'b', which is received in the arduino. This is for OFF
    {
     digitalWrite(led, LOW);
    }
  }
}

Once again, thanks for the Help !!

NC

Form3.JPG

Captura.JPG