hi some 1 know why it's not working ? (the serial.write)
the arduino send the value to vb ...
int ledPin = 13;
int ledPin1 = 12;
int inputByte;
int outByte;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, INPUT);
}
void loop() {
inputByte = Serial.read();
if (inputByte == '1') {
digitalWrite(ledPin, HIGH);
outByte = digitalRead(ledPin1);
Serial.print(outByte);
}
if (inputByte == '2') {
digitalWrite(ledPin, LOW);
}
}
in vb i do this
Private Sub Command1_Click()
'TRANSMIT
MSComm1.Output = "1"
MsgBox MSComm1.Input
End Sub
Private Sub Command2_Click()
MSComm1.Output = "2"
End Sub
Private Sub Form_Load()
'SETUP
With MSComm1 'sets up COM settings
.Handshaking = 0
.RThreshold = 0
.RTSEnable = True
.CommPort = 5
.Settings = "9600,n,8,1"
.SThreshold = 0
.PortOpen = True
End With
End Sub
Did you already check with the serial monitor if your sketch is working as expected? Did you test your VB with some other data source then the Arduino?
@chen
You have an LED plugged into pin 12? That pin is declared as an INPUT pin, which means that something needs to be supplying 0 or 5V. The digitalRead will determine whether the input voltage is closer to 0 (off) or to 5V (or Vref, if Vref is lower than 5V) (on).
The LED does not supply voltage. It is a consumer. Therefore, reading an LED is pointless.
LEDs are not current limiting devices. They will draw lots of current - more than the Arduino can supply without damage. Grumpy_Mike is suggesting that you should have a resistor in series with the LEDs.
My point still remains, though. The LED is an output device. Reading it, in general, is meaningless.
Though this has nothing to do with the inability of your VB application to get data from the Arduino.
Are you able to see output in the Serial Monitor window?
The LED is an output device. Reading it, in general, is meaningless.
Agreed, there is little point in reading back the state of an LED.
However, if you do read an output pin, and that pin is being held at a low voltage by an excessive external load, then you will always read back a zero from it despite you having written a one to it.
In VB, or C#, when you send something like this "0" over serial to a COM port it is received by the arduino as a BYTE, this is why your code isn't working. A byte of zero is a null char, a byte of 48 is the same as a 0 char, what you send to the arduino via serial.
There for this code will work, as it doesn't look for the data of byte 0, rather it looks for the data of string 0 which is the same as byte 48
int ledPin = 13;
int ledPin1 = 12;
int inputByte;
int outByte;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, INPUT);
}
void loop() {
inputByte = Serial.read();
if (inputByte == 48) {
digitalWrite(ledPin, HIGH);
outByte = digitalRead(ledPin1);
Serial.print(outByte);
}
if (inputByte == 49) {
digitalWrite(ledPin, LOW);
}
}
hi thanks but the code that i build is fine and he work !
the code that you give me not work
not the read and not the write
i send 1 from vb and the led is on but when the arduino send to vb the value in pin 12 this not work the serial.write not work the read worked fine
I copied and pasted your VB code into a project. On the form I added two buttons and an MSComm object.
I pasted your Arduino code into a new sketch. I got rid of the pin 12 stuff, since reading an LED is useless.
I made the Arduino sketch send "Testing" when it received a "1".
I uploaded the sketch, and started the VB app. I pushed button 1, and got a beep and message box that was blank. The LED lit up. I dismissed the message box, and pressed button 2. The LED went off. Then, I pressed button 1 again. The LED went back on, and I got the beep and the message box. This time, the message box said Testing. After the first missed message, every message was received.
I was not able to make the message appear immediately.