Serial comunication between 2 Arduino's (int variable)

Hello, i have simple 433mhz module(the one with the seperate Transmit and Recive units).

The transmit bord basicly has this code on it:

Serial.print('V');
Serial.println(i); // 0 - 255

i increases every second or so by 1; 'V' for varification in case of an bad signal;

Now when i only have the recive module directly connected with my PC true the serial chip (without the Atmel chip), i get this output, as you would expect:

20:40:57.999 -> V122
20:40:57.999 -> V122
20:40:58.099 -> V122
20:40:58.099 -> V122
20:40:58.099 -> V122
20:40:58.099 -> V122
20:40:58.099 -> V122

Now, i want the reciving arduino to give me that value i so that i can control an led with pwm.
I have tried this:

while (Serial.available() > 2){
    serialRead = Serial.read();
    if(serialRead == 'V'){
      int val = Serial.read();
      analogWrite(9, val);
      Serial.println(val); //debug
    }
  }

But then the Led flashes randomly and outputs random values.

So, any sugestions how i can get an stable value?
Thank you!

Serial.println(i); // 0 - 255

First thing is that this code is not sending a single byte value, but the ascii values of the digits. if i were equal to 123, then the code would send three bytes 49(ascii ‘1’) 50(ascii ‘2’) 51(ascii ‘3’) and a line return(ascii 10).

Your receiving code reads just a single byte with

int val = Serial.read();

You can change the receiving code to deal with the ascii transmission.
Serial.parseInt() would be a “blocking” but simple way of reading the ascii.

Or you can change the sending code to only send a single byte value(0-255) with Serial.write(i);

Robin2’s excellent tutorial on serial communication methods may be of interest

Also, may I suggest an easy to use and reliable serial data transfer library that will automatically handle everything behind the scenes (apart from having to split the int into its byte pieces and reconstructing the bytes on the other side).

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.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

…R

everything said in the above answers are valid. you need to pick whether you want to use raw byte format or if you want to use text encoding. if you are looking for validation i would use a character at the beginning and the end. otherwise you cant tell if or when you got a complete number.

if you are looking to code out this sort of validation yourself ... cheers to you. but it would be considered somewhat advanced coding. you would need to setup code to listen to your validation character. and only then begin to starting recording incoming further bytes for numbers. untill finally your code received the end character and parces out what it received in the middle and of course returns to listening for the start character again.

Robin2:
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.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker

Serial.print(value1);
Serial.print(’,’); // comma separator
Serial.print(value2);
Serial.println(’>’); // end marker

Will you please do something to stop this auto response business? Write some new codes for the poster and others. Whenever an issue comes on UART/Serial communication @Robin2 makes (for most of the times) an auto response.

GolamMostafa:
Will you please do something to stop this auto response business? Write some new codes for the poster and others. Whenever an issue comes on UART/Serial communication @Robin2 makes (for most of the times) an auto response.

You auto-response, too. I can't tell you how many times I've seen that diagram you always post.

Power_Broker:
You auto-response, too. I can't tell you how many times I've seen that diagram you always post.

Partial Fair Point. (Actually, I have no auto-response machine; I do manual post of the diagram for every new post having done some modification on it if needed.)

GolamMostafa:
Will you please do something to stop this auto response business?

No. It is effective and an efficient use of my limited time. Many people have commented favourably on my tutorial.

And this is just another example of where you interrupt other people’s Threads. You could easily have made this comment in a Thread that you started yourself.

…R

Robin2:
Many people have commented favourably on my tutorial.

I am one of them.