Arduino + Visual Studio (LDR and LED)

Hi everyone.

This is my first Question on this Arduino Forum. I hope someone can help me with my issue.

I am using an Arduino and Visual Studio for an aplication with a LDR and LED. What I am trying to do is to control the LED from the Visual Studio interface.

  1. I am turning the LED On / Off from the Visual Studio interface
  2. I also use the LDR, depending on the light atmosphere, the led will turn it on with an analog output.
  3. I am using the same pin (pin 5 from Arduino) as Digital output and Analog output. (I know it is possible to do that but I do not remember how did I do it)

The problem is: When I want to turn the LED ON or OFF from the VS interface, it does not do anything, because there is an analog reading at that moment, so, how can I interrupt the analog reading?

This is my Arduino Code

int led = 5;      //Pin 5 from the Arduino Nano
int ldr = A0;     //Pin A0 from the Arduino Nano
int valor;        //Value for the analog Reading
int valor2;       //Value received from Visual Studio

void setup()
{
 pinMode(led, OUTPUT);      //Pin 5 as an Output
 Serial.begin(9600);        //Serial port
}

void loop()
{
  analogico();
  digital();
  escoger();
}

void digital()           //This is the part where the value from Visual Studio 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);
    }
  }
}


void analogico()     //AnalogReading from the LDR
{
  valor = analogRead(ldr);
  valor = constrain(valor, 0, 300);
  valor = map(valor, 0, 300, 255, 0);
  analogWrite(led, valor);
  Serial.print("Valor :");
  Serial.println(valor);
}
 //This is the code from Visual Studio
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

What I really need is, when I press Button 'a' in VS, the LED should come ON, when I press button 'b' in VS, the LED should come OFF and the analog reading should be interrupted.

The steps should be like this.

When Arduino is powered:

  1. AnalogReading from LDR to control LED should be first.
  2. If I press OFF button in VS, the LED should come OFF
  3. If I press ON button in VS, the LED should come ON, whitout the analogReading.
  4. If I restart SerialPort, it should come as its original status (to step 1)

Hope can help me, thanks in advance.

NC

Captura.JPG

What I am trying to do is to control the LED from the Visual Studio interface.

Visual Studio is an application development environment. It has NO mechanism for talking to the serial port.

An application that you develop using Visual Studio MIGHT. So talk about YOUR app, not the environment that was used to develop it.

The problem is: When I want to turn the LED ON or OFF from the VS interface, it does not do anything, because there is an analog reading at that moment

That is NOT the reason why the Arduino is not turning the LED on or off. Serial data is buffered. The Arduino will read it when the function that calls the analogRead() function ends.

Where does you app open the serial port?

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