Send a negative data

Hi, all.
I am a new arduino user. I succeeded to send a positive number and it work perfectly. I want to send a negative from a arduino to another. Please help me. I though I can transform a number to a char. I am stucking how to program.

give an example of the positive number you sent (type and number)

give an example of the negative number you want to send (type and number)

Usually, negative numbers (integers) are determined by setting the highest bit to 1.

Usually, negative numbers (integers) are determined by setting the highest bit to 1.

No, it isn't that simple, that's sign and magnitude representation.
Most processors (the Arduino's AVR included) use two's complement representation for signed integers.

OP, post your code.

Hi, all.
I am a new arduino user. I succeeded to send a positive number and it work perfectly. I want to send a negative from a arduino to another. Please help me. I though I can transform a number to a char. I am stucking how to program.

Yes, you've already asked the question, but not posted any of your code, so helping is difficult.

This is the sending code:
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
int rx_in;
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);

}

void loop()
{

rx_in = pulseIn(7,HIGH);
rx_in = rx_in/10;
if (mySerial.available())
Serial.write(mySerial.read());
mySerial.write(rx_in);
delay(100);
Serial.println(rx_in);
}

This is the receiving code, with mega

void setup()
{
Serial3.begin(9600);
Serial.begin(9600);
}
void loop()
{
if(Serial3.available())
Serial.println(Serial3.read(),DEC);
delay(100);
}

thank you!!

KellieFins:
give an example of the positive number you sent (type and number)

give an example of the negative number you want to send (type and number)

I can send the data like 0-500, I want to send the negative number like -10.

AWOL:
Yes, you've already asked the question, but not posted any of your code, so helping is difficult.

Sorry. It is kinda late. I uploaded my code. Please check it and help me.

Delta_G:
How do you want to send them? Just the number itself or an ascii string representation of the number.

Show us the code for where you sent the positive one before.

Hi, Delta_G,
I uploaded my code. If there is something wrong, please tell me.

What happens when you try to send a negative number ?

Negative twos-complement integers always have the highest bit set to one.

if(Serial3.available())

Serial.println(Serial3.read(),DEC);
delay(100);

That is always going to receive a single byte. I'm not sure what the delay is for.

int rx_in;
...  
  rx_in = pulseIn(7,HIGH);
  mySerial.write(rx_in);

You are reading into an int (16 bits) but writing a byte (8 bits). You will lose data.

michinyon:
Negative twos-complement integers always have the highest bit set to one.

Hi, Michinyon,

Can you give me more detail?

UKHeliBob:
What happens when you try to send a negative number ?

I receiving a series random number.

I very much doubt it is random
Shall I lock this guessing game now, or are you going to post code?

Hi, Nick Gammon
If I dont use delay, I won't get any data. I dont know why. The code about rx_in is for testing.

AWOL:
I very much doubt it is random
Shall I lock this guessing game now, or are you going to post code?

I dont have these devices right now. I remember that when I sent a negative number, it shows on another aruduino a three or four positive repeatedly. Is that right? The code I already post.

Did you fix the problems Nick pointed out?
Can you post the code with the fixes?

Please read this:

You can send positive, negative, data, long strings, etc.

The technique is described above.