I am new to Arduino. I want to send the stored integer value from nano to Mega. Due to some reason, i can't change the nano board and the software serial is the only option. Here is the code for transmitter board
@kk1107 Introductory Tutorials is a section for posting tutorials for beginners, hence the NAME.
As you are not posting a tutorial for beginners I have moved your post here.
1. You have the following number to send from NANO to MEGA using soft UART (SUART) Port. int h = 500;
2. Note that the value of a variable is always saved as binary in memory location. So, 500 has been saved as 0000000011110100. In hex-base, it is 01F4 which is written as a matter of convenience. Here, the upper byte is 01 and the lower byte is F4.
3. When you have executed softSerial.write(h), the NANO has sent 11110100 (the lower byte, F4) to MEGA. write() method always handles 8-bit (1-byte) data.
4. To receive/display the number (F4) of Step-3 by the MEGA, we have to execute the following sketch at the MEGA side:
(Check that D10-pin of NANO is connected with TX1-pin of MEGA; D11-pin of NANO is connected with RX1-pin of MEGA.). GND-pin of NANO is connected with GND-pin of MEGA.
//#include <SoftwareSerial.h> no need; you are using hardwae UART1 Port
//SoftwareSerial softSerial(10, 11); no need; you are using hardware UART1 Port
byte ip; //data are coming as binary bits and not in ASCII
void setup()
{
//softSerial.begin(115200);
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
if (Serial1.available())
{
ip = Serial1.read();
//Serial1.print(ip);
Serial.println(ip, HEX); //or Serial.println(ip, BIN); to see 11110100
}
}
5. To send 500 from NANO and to receive/display by MEGA, we can execute the following sketches.
For NANO:
#include <SoftwareSerial.h>
SoftwareSerial softSerial(10, 11);
void setup()
{
softSerial.begin(9600);
}
void loop()
{
int h = 500; //01F4
softSerial.write(lowByte(h)); //F4 is sent
softSerial.write(highByte(h)); //01 is sent
delay (1000); //test interval
};
For MEGA:
//#include <SoftwareSerial.h>
//SoftwareSerial softSerial(10, 11);
byte ipL, ipH;
void setup()
{
//softSerial.begin(115200);
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
byte n = Serial1.available();
if (n == 2) //2 bytes are accumulated in the seial BUFFer
{
ipL = Seial1.read(); //ipL holds F4
ipH = Serial.read(); //ipH holds 01
int h = ipH<<8 | ipL; //ip holds 01F4
Serial.println(ip, DEC); //shows: 500
}
}