I am transmitting an integer value from the Tx pin (PIN 1) and receving the same data at the recever pin (PIN0). However, after 2 iterations, the received data is getting changed with respect to the data sent. Here is my code, Kindly give your suggestions, where I am going wrong.
#include <SoftwareSerial.h>
char str[4];
char str1[4];
int senddata= 1234;
void setup()
{
Serial.begin(9600);
}
void loop()
{
int i=0;
itoa(senddata, str1, 10); //Turn value into a character array
Serial.write(str1);
if (Serial.available())
{
while(Serial.available() && i<4)
{
str[i++] = Serial.read();
}
if (i>0)
Serial.println(str);
}
}
I am not receiving 1234 at the output but 123 or 12 or 1234 sometimes.