communication between 2 arduino boards

Hey guys have a simple problem between communication
i want to send a val between 2 board, UNO is TX and MEGA is RX but the val i get on Mega is four times smaller than the original val from UNO
UNO

void setup()
{
	Serial.begin(115200);
}

void loop()
{
	VAL();
}

void VAL()
{
	byte val = analogRead(A1);
	Serial.write(val);
}

MEGA

void setup()
{
	Serial.begin(115200);
	Serial.print("RDY");
	Serial1.begin(115200);
}

void loop()
{
	if(Serial1.available()>0)
	{
		Serial.print("Val ");
		int val = Serial1.read();
		Serial.print(val);
	}
}
	byte val = analogRead(A1);

analogRead returns an int, not a byte, so you are truncating the 2 most significant bits of the 10 bits that analogRead() returns. If you want the full 10-bit precision of the analog read you need to send both the highByte() and the lowByte() of the int that analog read returns, and reconstruct the int using addition and shifting on the receiver end. If you only need 8-bits of precision, then right shift the int returned by analogRead() by two before sending it. The result will be a value between 0 and 255 that is mapped to the 0 to 1023 range.

... if you choose the two byte solution, you need something to synchronise what is the 1st and 2nd byte. You can rely on that if you reset both machines, or only reset when there is an idle situation, so that they can remain "in step" of the protocol.

Remember that Serial.read() also only returns a single byte.

It may work fine - but if it goes awry you now know of a possible hazard.

A toally differnet one is to use Serial.println(value); which will transmit the value as several ASCII characters including a terminating NewLine. This can be parsed on the receiving side with the Serial1.parseInt(). This way you can transfer intergers and(almost) not worry. (there is a potential blocking state where it waits for the terminating character.... ah, it's never simple :frowning: )

... if you choose the two byte solution, you need something to synchronise what is the 1st and 2nd byte. You can rely on that if you reset both machines, or only reset when there is an idle situation, so that they can remain "in step" of the protocol.

Remember that Serial.read() also only returns a single byte.

It may work fine - but if it goes awry you now know of a possible hazard.

A toally differnet one is to use Serial.println(value); which will transmit the value as several ASCII characters including a terminating NewLine. This can be parsed on the receiving side with the Serial1.parseInt(). This way you can transfer intergers and(almost) not worry. (there is a potential blocking state where it waits for the terminating character.... ah, it's never simple smiley-sad )

did like u seed im sending from UNO a val that's equal to 900 but on MEGA i receive something like this

Val 1
Val 901
Val 901
Val 1
Val 901
Val 901
Val 901
Val 901
Val 901
Val 901
Val 901
Val 1
Val 24565
Val 901
Val 1
Val 9001
Val 9011
Val 24565
Val 25365
Val 9
Val 901
Val 901
Val 1
Val 24565
Val 901
Val 902
Val 901
Val 9901
Val 9901
Val 9001
Val 9011
Val 901
Val 901
Val 1
Val 901
Val 9901
Val 901
Val 901
Val 1
Val 9011
Val 9011
Val 9011
Val 9011
Val 24565
Val -32061

analogRead returns an int, not a byte, so you are truncating the 2 most significant bits of the 10 bits that analogRead() returns. If you want the full 10-bit precision of the analog read you need to send both the highByte() and the lowByte() of the int that analog read returns, and reconstruct the int using addition and shifting on the receiver end. If you only need 8-bits of precision, then right shift the int returned by analogRead() by two before sending it. The result will be a value between 0 and 255 that is mapped to the 0 to 1023 range.

did that but on UNO my val is between 0-1023 and on MEGA is something like this (0-255) and this four times basically

UNO => MEGA
0 = 0
128 = 255
129 = 0
256 = 255
257 = 0
512 = 255
513 = 0
1023 = 255

THX_RoG:
did like u seed

Really? Where's the code?

did that

Really? Where's the code?

sry i forgot
MEGA

void setup()
{
	Serial.begin(115200);
	Serial.print("RDY");
	Serial1.begin(115200);
}

void loop()
{
	if(Serial1.available()>0)
	{
		Serial.print("Val ");
		int val = Serial1.parseInt();
		Serial.println(val);
	}
}

UNO

void setup()
{
	Serial.begin(115200);
}

void loop()
{
	VAL();
}

void VAL()
{
	int val = analogRead(A1);
	Serial.println(val);
}

You aren't checking for enough characters being available before calling parseInt?

A better approach is to split the 10 bits up into two halves and send each with a tag to indicate
if its the high or low byte:

void sendSample(int sample)
{
  byte low = sample & 0x1F ; // bottom 5 bits: 000xxxxx
  byte high = (sample >> 5) | 0xE0 ; // top 5 bits tagged with 111xxxxx
  Serial1.write (high) ;
  Serial1.write (low) ;
}

then to read you wait for a high byte followed by a low and stick them together:

byte myRead ()
{
  while (Serial.available () < 1)
  {}
  return Serial.read () ;
}

int readSample ()
{
  byte high, low ;
  do
  {
    do
    {
      high = myRead () ;
    } while ((high & 0xE0) != 0xE0) ;  // wait for high byte tag
    low = myRead () ;
  } while ((low & 0xE0) != 0x00) ;  // if low byte isn't tagged correctly retry the whole thing
  return ((high & 0x1F) << 5) | low ;  // stictch together
}