I am sending values of 30 and 40 from vb to arduino to control led on and off,but it is not responding.
The receiver led in the arduino blinks.
int ledPin = 13; // the number of the LED pin
// variables will change:
//int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// // initialize the pushbutton pin as an input:
// pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
// buttonState = digitalRead(buttonPin);
int value;
while(Serial.available()==0)
value= Serial.read();
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (value == 30){digitalWrite(ledPin, HIGH);}
else if (value==40) {digitalWrite(ledPin, LOW);}
}
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
Robin2:
I believe this code (from the Original Post) implies that the opposite is the problem.
value= Serial.read();
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (value == 30)
...R
No, it actually proves that what I wrote it true.
He think's that his code is sending text 30.
He wants the human-readable 30.
But he doesn't realize that human-readable 30 aka text 30 aka "30" is not 30.
if you wish to transmit binary values 30 ans 40 you can use the SerialPort Write method which takes a byte array as a parameter, e.g.
' every second transmit binary sequence 30 40 30 40 to blink LED
Imports System.IO.Ports
Imports System.Threading
Module Module1
Sub Main()
Dim serialPort = New SerialPort()
serialPort.PortName = "COM10"
serialPort.BaudRate = 115200
serialPort.Open()
While True
' define a single byte array and transmit it
Dim data() As Byte = {30}
serialPort.Write(data, 0, 1) ' transmit 30
Thread.Sleep(1000)
data(0) = 40
serialPort.Write(data, 0, 1) ' transmit 40
Thread.Sleep(1000)
End While
End Sub
End Module