sending integer via serial communication arduino to arduino problem

Hi all,

im trying to just send an integer e.g 180 from one arduino uno to the other but for some reason im getting this 4294947840. iv been on this for the whole day but cant seem to figure it out. please help!!

the sender coder:

//Sender Code

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

void loop() { 
int value=180; //this would be much more exciting if it was a sensor value

  Serial.write(value / 256);
      
  Serial.write(value % 256);
   delay(50); //allows all serial sent to be received together
}

the receiver code:

//Receiver Code

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

void loop() {
  int val;

      
      while (!Serial.available()){}
//delay(100); //allows all serial sent to be received together
    byte b1 = Serial.read();
    while (!Serial.available()){}
  //  delay(100); //allows all serial sent to be received together
    byte b2 = Serial.read();

    val = b1  + b2 *256 ; 
    Serial.println(val);

}

This all is a bit fishy to start with (for example not syncing and it would be easier to simply bitshift) but you retrieve math is the inverse of the make math. Try

val = b2  + b1 *256 ;

If you want to do reliable serial communication, the serial input basics thread is a good place to start.

1 Like

Hi septillion,

Thank you very much it worked!!

Hi Guys!

I know its been long time since this thred was closed but if u are still around then please have a look at my problem.

I was trying to work out serial com between two arudinos when i came across this post and simply copied the code and found it working.

So far so good.
however i need to also print the stuff received on a 7 seg dispaly.

here is where the problem arises. when i add the 7 seg ment dispaly code your code stops working.
specifically it stops getting the correct transmitted value and insted displays stuff like 4294955015.

my code is attached please have a look

//Receiver Code
int dig_1,dig_2,dig_3,dig_4,refresh=1;
int val;
void setup() {
Serial.begin(9600);

}

void loop() {

while (!Serial.available()){}
//delay(100); //allows all serial sent to be received together
byte b1 = Serial.read();
while (!Serial.available()){}
// delay(100); //allows all serial sent to be received together
byte b2 = Serial.read();

val = b2 + b1 *256 ;
Serial.println(val);

dig_1 = val / 1000; // digit 1 temperature value
dig_2 = (val - 1000 * dig_1) / 100; // digit 2 temperature value
dig_3 = (val - 1000 * dig_1 - 100 * dig_2) / 10; // digit 3 temperature value
dig_4 = val - 1000 * dig_1 - 100 * dig_2 - 10 * dig_3; // digit 4 temperature value
digit_pos(); // function switching the digit positions
}

void displaydigit(int dig_n) // function sending segment enable signals to 7 seg display
{
switch (dig_n)
{
case 0:
digitalWrite(6, LOW); // to segment A
digitalWrite(7, LOW); // to segment B
digitalWrite(8, LOW); // to segment C
digitalWrite(9, LOW); // to segment D
digitalWrite(10, LOW); // to segment E
digitalWrite(11, LOW); // to segment F
digitalWrite(12, HIGH); // to segment G
break;
case 1:
digitalWrite(6, LOW); // to segment A
digitalWrite(7, HIGH); // to segment B
digitalWrite(8, HIGH); // to segment C
digitalWrite(9, LOW); // to segment D
digitalWrite(10, LOW); // to segment E
digitalWrite(11, LOW); // to segment F
digitalWrite(12, LOW); // to segment G

break;
case 2:
digitalWrite(6, LOW); // to segment A
digitalWrite(7, LOW); // to segment B
digitalWrite(8, HIGH); // to segment C
digitalWrite(9, LOW); // to segment D
digitalWrite(10, LOW); // to segment E
digitalWrite(11, HIGH); // to segment F
digitalWrite(12, LOW); // to segment G

break;
case 3:
digitalWrite(6, LOW); // to segment A
digitalWrite(7, LOW); // to segment B
digitalWrite(8, LOW); // to segment C
digitalWrite(9, LOW); // to segment D
digitalWrite(10, HIGH); // to segment E
digitalWrite(11, HIGH); // to segment F
digitalWrite(12, LOW); // to segment G

break;
case 4:
digitalWrite(6, HIGH); // to segment A
digitalWrite(7, LOW); // to segment B
digitalWrite(8, LOW); // to segment C
digitalWrite(9, HIGH); // to segment D
digitalWrite(10, HIGH); // to segment E
digitalWrite(11, LOW); // to segment F
digitalWrite(12, LOW); // to segment G

break;
case 5:
digitalWrite(6, LOW); // to segment A
digitalWrite(7, HIGH); // to segment B
digitalWrite(8, LOW); // to segment C
digitalWrite(9, LOW); // to segment D
digitalWrite(10, HIGH); // to segment E
digitalWrite(11, LOW); // to segment F
digitalWrite(12, LOW); // to segment G

break;
case 6:
digitalWrite(6, LOW); // to segment A
digitalWrite(7, HIGH); // to segment B
digitalWrite(8, LOW); // to segment C
digitalWrite(9, LOW); // to segment D
digitalWrite(10, LOW); // to segment E
digitalWrite(11, LOW); // to segment F
digitalWrite(12, LOW); // to segment G

break;
case 7:
digitalWrite(6, LOW); // to segment A
digitalWrite(7, LOW); // to segment B
digitalWrite(8, LOW); // to segment C
digitalWrite(9, HIGH); // to segment D
digitalWrite(10, HIGH); // to segment E
digitalWrite(11, HIGH); // to segment F
digitalWrite(12, HIGH); // to segment G

break;
case 8:
digitalWrite(6, LOW); // to segment A
digitalWrite(7, LOW); // to segment B
digitalWrite(8, LOW); // to segment C
digitalWrite(9, LOW); // to segment D
digitalWrite(10, LOW); // to segment E
digitalWrite(11, LOW); // to segment F
digitalWrite(12, LOW); // to segment G

break;
case 9:
digitalWrite(6, LOW); // to segment A
digitalWrite(7, LOW); // to segment B
digitalWrite(8, LOW); // to segment C
digitalWrite(9, HIGH); // to segment D
digitalWrite(10, HIGH); // to segment E
digitalWrite(11, LOW); // to segment F
digitalWrite(12, LOW); // to segment G

break;
}
delay(1);
}

void digit_pos() // function switching the digit positions
{
digitalWrite(2, HIGH); // digit 1 on
digitalWrite(3, LOW); // digit 2 off
digitalWrite(4, LOW); // digit 3 off
digitalWrite(5, LOW); // digit 4 off
displaydigit(dig_1);
delay(refresh); // display hold
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
displaydigit(dig_2);
delay(refresh);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
displaydigit(dig_3);
delay(refresh);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
displaydigit(dig_4);
delay(refresh);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}

cheers,
Marrc

@OP

1. You are communicating between two Arduinos using hard UART Ports (UART) which are (at the same time) connected with their respective Serial Monitors (Fig-1). This kind of connection is neither recommended nor supposed to be working reliably. The alternate solution is to connect them using soft UART Ports (SUART, Fig-2) and leave the UART Ports to remain connected with Serial Monitors/IDEs for sketch debugging/uploading.

uartUNO-1.png
Figure-1:

uartUnoUno-23.png
Figure-2:

2. Let us see why are you receiving 4294947840 when you are sending 180 using / and % operators.
(1) First you send b1 = 00 (180/250). //quotient
(2) Then you are sending 180 (180%256). //remainder

(3) First you receive b1 = 0x00.
(4) Then you receive b2 = 180.

(5) At the receiver, you are computing (though wrong formula)--
int val = b1 + b2256 //0 + 180256 = 46080 = 0xB400

How much value in HEX would be stored in variable val of type int (signed data type)? 46080 (0xB400)? Because the data type is int (signed 16-bit) which can hold 0x8000 to 0x7FFF(-32768 to 32767), the apparent result 46080 (0xB400) will not be accommodated in variable val. As a result, the compiler re-sizes the variable to 32-bit (unsigned) and fills up the upper 16-bit with the sign bit (1) of B400; the unsigned value in val is now: FFFFB400 in hex = 4294947840 in decimal. This is the value (wrong to you; but, correct to the MCU!) being shown in the Serial Monitor.

3. The solution is --
(1) Declare val as unsigned: unsigned int val;
(2) val = b1256 + b2 = 0256 + 180 = 180. //this formula is wrong: val = b1 + b2*256

4. Receiver Sketch

//Receiver Code
#include<SoftwareSerial.h>
SoftwareSerial  SUART(2, 3); //SRX of UNO = 2, STX of UNO = 3

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
}

void loop()
{
  unsigned int val;
  while (!SUART.available()) {}
  //delay(100); //allows all serial sent to be received together
  byte b1 = SUART.read();
  while (!SUART.available())
  {}
  //  delay(100); //allows all serial sent to be received together
  byte b2 = SUART.read();

  val = b1*256  + b2;// * 256 ; //46080 ==> 
  Serial.println(val);  //shows: 180
}

5. Transmitter Sketch

//Sender Code
#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3); //SRX = 2 of UNO

void setup() 
{
  Serial.begin(9600);
  SUART.begin(9600);
}

void loop() {
  int value = 180; //this would be much more exciting if it was a sensor value

  SUART.write(value / 256);

  SUART.write(value % 256);
  delay(2500); //allows all serial sent to be received together
}

6. Receiver Serial Monitor
smy1.png

uartUNO-1.png

uartUnoUno-23.png

smy1.png

2 Likes

Dear Golam Mustafa,

You have saved me lot of heart ache and not to mention time. I had spent whole of last week pondering over this problem. the way you have described it (especially the pics and op screen) is outstanding to sayy the least. and all this for nothing? you are simply a person with heart of gold.

my best wishes go out to you wherever you are.

Best Regards,
Marrc