serial.write (help)

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

How is it "not working"?.

What is connected to ledPin1?

I have no specific knowledge of VB. However

outByte = digitalRead(ledPin1);
Serial.print(outByte);

sends one single byte. Maybe you want to code

outByte = digitalRead(ledPin1);
Serial.print(outByte, DEC);

to send the value as ASCII characters. Or maybe you need to send it like

outByte = digitalRead(ledPin1);
Serial.println(outByte, DEC);

to make VB pick it up.

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?

Cheers, Udo

outByte = digitalRead(ledPin1);
Serial.print(outByte);

Despite the byte in the name, outByte is declared as an int, so the Serial.print statement should print two bytes, shouldn't it?

I hooked Pin 13 Led
I hooked Pin 12 Led
I try to see if he sends me 1 or 0 when the Led on or off
Even if I do this:

Serial.write('asdaa');

or this

Serial.write('1');

or this

Serial.write('12937');

nothing !
It does not work ... sorry for bad english i from Israel :\

edit :
in the monitor when i send him 1 and the ledpin13 in on it's type 0
and the led on

Does the "nothing" relate to the VB side only? Or do you see also nothing if you use the serial monitor of the IDE?

@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.

i put in pin 12 led and i connect the led in 12 and gnd
same in pin 13

No current limiting resistor!!! :o

what ?

You said:-

i put in pin 12 led and i connect the led in 12 and gnd

If you do that you will damage your arduino, you need a series current limiting resistor in line with each LED you use.
http://www.thebox.myzen.co.uk/Tutorial/LEDs.html

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.

However he implemented

    pinMode(ledPin1, INPUT);

So I guessed he would not have connected a LED to it. But his latest answer made the issue clear :slight_smile:

in the monitor i get 0 but in vb nothing
when i do

Serial.write('sdfsf');

it's who in tha monitor f
in vb null
when i do Serial.print or println('asdf');
it's who 2740

That is easy to solve, you are looking for ascii codes but out putting strings, replace 0 with 48 and 1 with 49, in the arduino, leave the VB the same

i don't understand :
what to do to get n vb the serial write

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.