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