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.
- I am turning the LED On / Off from the Visual Studio interface
- I also use the LDR, depending on the light atmosphere, the led will turn it on with an analog output.
- 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:
- AnalogReading from LDR to control LED should be first.
- If I press OFF button in VS, the LED should come OFF
- If I press ON button in VS, the LED should come ON, whitout the analogReading.
- If I restart SerialPort, it should come as its original status (to step 1)
Hope can help me, thanks in advance.
NC