Serial communication - char array sended through the serial to another arduino

Hi

I am working on one project where I am using serial communication. I have used your code from Serial Input Basics - Example 3. I have one problem here is output from my serial monitor.

<Arduino is ready>
0
0
0
0
This just in ... 43*20*36*45
43
20
36
45
This just in ... 43*20*1023*45
43
20
1022
45
This just in ... 43*20*26*45
43
20
26
45
This just in ... 43*20*2<43*20*1656*45
3241
20
1654
45
This just in ... 42**20*481*45
0
20
480
45
This just in ... 42*
<42*20*481*45<43*20*244*45
-19296
20
243
45
This just in ... 43*20*1445
0
43
20
1443
This just in ... 42*20*480*4<43*20*481*45
5241
20
480
45
This just in ... 43*20*481*45
43
20
480
45
This just in ... 43<42*20*1643*45<43*20*1661*45
-19296
20
1659
45

first three are correctly received but other no. What cause this ?

here is receiver code:

const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithStartEndMarkers();
    showNewData();
    delay(500);
  
  int a = 3, temp = 0;
  int number[4] = {0,0,0,0};
  for(int i = (unsigned)strlen(receivedChars)-1; i >= 0;i--)    {
        if(receivedChars[i] != '*') {
            number[a] += ((int)receivedChars[i]-48)*pow(10,temp);
            temp++;
        }
        else {
            a--;
            temp = 0;
        }
       
    }
    for(int m = 0; m < 4; m++)  {
        Serial.println(number[m]);
    }
    delay(500);
    
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

sender code:

long duration, distance, RightSensor, BackSensor, FrontSensor, LeftSensor;
char sendMe[40];
long out[4];
#define trigPin1 3
#define echoPin1 2
#define trigPin2 4
#define echoPin2 5
#define trigPin3 7
#define echoPin3 8

void setup()  {
  Serial.begin(9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
 
 
}
 
void loop() {
  SonarSensor(trigPin1, echoPin1);
  RightSensor = distance;
  SonarSensor(trigPin2, echoPin2);
  LeftSensor = distance;
  SonarSensor(trigPin3, echoPin3);
  FrontSensor = distance;
  Serial.print("<");
  Serial.print(RightSensor);
  Serial.print("*");
  Serial.print(LeftSensor);
  Serial.print("*");
  Serial.print(FrontSensor);
  Serial.print("*");
  Serial.print("45");
  Serial.println(">");
  
  
}
 
void SonarSensor(int trigPin, int echoPin)
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
 
}

Did you make it italic on purpose? Please read How to use the forum and edit accordingly please :)

Using pow() to produce 10 * 10 or 10 * 10 * 10 is ... What's the word I'm looking for? ... Stupid. That 's it.

You have a string. Use atoi() to convert it to an int, until you KNOW what you are doing.

Anonymous printing sucks. It is no harder to type ONCE:

   Serial.print("number[");
   Serial.print(i);
   Serial.print("] = ");
   Serial.println(number[i]);

than it is to type:

   Serial.println(number[i]);

My version conveys a LOT more information, for very little extra effort.

Where is problem ? Can you paste here edited code please.

At 9600 baud, you’re sending roughly a character per millisecond at an extremely high repetition rate. Your receiver side can not keep up with that and you’re filling the software receive buffer and it will eventually be full. Any data received after that is discarded till there is space.

Slow down the sender (add e.g. delay(500) for testing purposes at the end of the sender’s loop().

Note:

marcopinter999:
Hi

I am working on one project where I am using serial communication. I have used your code from Serial Input Basics - Example 3. I have one problem here is output from my serial monitor.

<Arduino is ready>

0
0
0
0
This just in … 43203645
43
20
36
45
This just in … 43
20102345
43
20
1022
45
This just in … 43202645
43
20
26
45
This just in … 43
202<4320165645
3241
20
1654
45
This just in … 42**2048145
0
20
480
45
This just in … 42*
<422048145<432024445
-19296
20
243
45
This just in … 43201445
0
43
20
1443
This just in … 42204804<432048145
5241
20
480
45
This just in … 432048145
43
20
480
45
This just in … 43<42
20164345<43201661*45
-19296
20
1659
45




first three are correctly received but other no. What cause this ?

Actually, the second one is already incorrect (1023 versus 1022); typing mistake?

@marcopinter999, please be kind enough to change the name of this Thread so that other people searching for my tutorials will not be confused. If you modify your Original Post you can edit the title. Perhaps just add " question" to the end of your title.

My tutorials are Serial Input Basics - Updated and the earlier version Serial Input Basics

Thank you.

...R

sterretje you have true now it works very well

Here is sender code:

long duration, distance, RightSensor, BackSensor, FrontSensor, LeftSensor;
char sendMe[40];
long out[4];
#define trigPin1 3
#define echoPin1 2
#define trigPin2 4
#define echoPin2 5
#define trigPin3 7
#define echoPin3 8

void setup()  {
  Serial.begin(9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
 
 
}
 
void loop() {
  SonarSensor(trigPin1, echoPin1);
  RightSensor = distance;
  SonarSensor(trigPin2, echoPin2);
  LeftSensor = distance;
  SonarSensor(trigPin3, echoPin3);
  FrontSensor = distance;
  Serial.print("<");
  Serial.print(RightSensor);
  Serial.print("*");
  Serial.print(LeftSensor);
  Serial.print("*");
  Serial.print(FrontSensor);
  Serial.print("*");
  Serial.print("45");
  Serial.println(">");
  delay(500);
  
}
 
void SonarSensor(int trigPin, int echoPin)
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
 
}

Receiver code:

const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithStartEndMarkers();
    showNewData();
    delay(500);
  
  int a = 3, temp = 0;
  int number[4] = {0,0,0,0};
  for(int i = (unsigned)strlen(receivedChars)-1; i >= 0;i--)    {
        if(receivedChars[i] != '*') {
            number[a] += ((int)receivedChars[i]-48)*pow(10,temp);
            temp++;
        }
        else {
            a--;
            temp = 0;
        }
       
    }
    for(int m = 0; m < 4; m++)  {
        Serial.println(number[m]);
    }
    delay(500);
    
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

marcopinter999: sterretje you have true now it works very well

Thank you for changing the Title. And glad to hear you have it working.

...R