Arduino Mega 2560 Serial Input Cycle Problem

A command is sent to Arduino Mega 2560 via serial port for collecting data. It was working fine when the command is sent in 1 sec cycle. When the cycle was changed to 100 ms, the Arduino Mega 2560 failed to receive the command. It is OK for Arduino UNO to receive the same command in repetition period of 100ms. I would be grateful if Anyone could solve this Arduino Mega 2560 problem ?

I can't see your code, so unfortunatelly I can't give a solution.

When you do post your code, please do so in code tags.

If I want to receive commands via Serial,
I collect all the incoming chars in a buffer,
when the line delimiting CR/LF comes along,
my collecting routine calls the function that processes the received line,
and clears the collection buffer after that function returns.

For this simple example, the processing is just a print, all on Serial.

void setup() {
  Serial.begin(115200);
  Serial.println(F("Echo lines"));
}

void loop() {
  handleSerial();
}

void oneLineReceived(const char * buffer) {
  Serial.println(buffer);
}

const byte maxMsgLen = 64;
const byte cbLF = 10;
const byte cbCR = 13;

void handleSerial() {
  static uint8_t bIndex;
  static uint8_t buffer[maxMsgLen + 1];
  bool lineReady = false;
  if (Serial.available()) {
    uint8_t inChar = Serial.read();
    if (inChar != cbLF) {
      if (inChar == cbCR) {
        lineReady = true;
      } else {
        buffer[bIndex++] = inChar;
        lineReady = (bIndex == maxMsgLen);
      }
      if (lineReady) {
        buffer[bIndex] = 0;
        oneLineReceived((const char*)buffer);
        bIndex = 0;
      }
    }
  }
}

Below please find my code. When the command 'C' is sent from other device every 1 sec, it works. When the command 'C' is sent from another device every 100 ms. It failed to receive the command. The same code is OK in Arduino UNO to receive command 'C' every 100 ms.

#include <Wire.h>
#include <NewPing.h>
#define SONAR_NUM 3                      // Number of Sonar
#define MAX_DISTANCE 400                  // Maximum distance (in cm) to ping
NewPing sonar[SONAR_NUM] = {              // Ultrasonic Sensor Object array
  NewPing(13, 12, MAX_DISTANCE),
  NewPing(11, 10, MAX_DISTANCE),
  NewPing(9, 8, MAX_DISTANCE)
}

void loop() {
  // put your main code here, to run repeatedly:
  inbyte = ' ';

  if (Serial.available() > 0){
    inbyte = Serial.read(); 
  }
  if (inbyte == 'C'){
    for (i = 0; i < SONAR_NUM; i++){
//      delay(10);
      sid = String(i + 1);
      duration = sonar[i].ping();      
      distance = (duration/2)*0.0343;
      distance = sonar[i].ping_cm();
      dist = String(distance);
      outpackage = "u"+sid+" "+dist+"\n";
      Serial.print(outpackage);
      Serial1.print(outpackage);*/
      sid = String(i);
      Serial.print(sid);
    }    
    Serial.print("E\n");
  } else if (inbyte == ' '){
    // do nothing 
  } else {
    Serial.write(inbyte);
//    Serial1.write(inbyte);
  }
}

Do you know how long these calls to ping take?

a7

What form does this failure take?

Also, please post all your code.

Only missing the setup section of the code. The failure is no data sent out to the serial port.
Eventually, I moved the serial port connection from USB to TX0 and RX0. It works well. Not sure whether it is the USB to UART conversion program issues in Arduino Mega 2560. Or may be the compatible hardware problem.

That does not make sense, RX0 and TX0 are the USB connection.

Yes RX0 and TX0 are connected to USB of Arduino Mega with a USB to UART converter chip. We can directly connect to TX0 and RX0 using an external USB to UART cable. It bypasses the internal conversion chip. It works then.

I think you have probably killed the USB to Serial converter chip, if it is not working.

Normally you would use one of the three free hardware Serials for such a connection.

No, the chip is not killed. It works if the 'C' command is sent every 1 sec. If the 'C' command is sent in a period shorter than 850 ms, it fails. I have tested 3 Arduino Mega 2560 compatible boards. The same result !

So maybe you post the code that shows that behavior.

The code that you posted above, can not compile.

Sounds like the problem may be in the "other device" that is communicating with the Mega, since changing the USB-to-serial interface solves the problem. The Mega itself cannot tell what is connected to the Rx and Tx pins (as long as you do not overload them), so the code is likely working fine with the on-board USB-to-serial interface and your "other device" is not handling the USB connection properly.

The code is missing the setup section when I copied it. Below please find the completed code:

#include <Wire.h>
#include <NewPing.h>
#define SONAR_NUM 3                      // Number of Sonar
#define MAX_DISTANCE 400                  // Maximum distance (in cm) to ping
NewPing sonar[SONAR_NUM] = {              // Ultrasonic Sensor Object array
  NewPing(13, 12, MAX_DISTANCE),
  NewPing(11, 10, MAX_DISTANCE),
  NewPing(9, 8, MAX_DISTANCE)
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial1.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  inbyte = ' ';

  if (Serial.available() > 0){
    inbyte = Serial.read(); 
  }
  if (inbyte == 'C'){
    for (i = 0; i < SONAR_NUM; i++){
//      delay(10);
      sid = String(i + 1);
      duration = sonar[i].ping();      
      distance = (duration/2)*0.0343;
      distance = sonar[i].ping_cm();
      dist = String(distance);
      outpackage = "u"+sid+" "+dist+"\n";
      Serial.print(outpackage);
      Serial1.print(outpackage);*/
      sid = String(i);
      Serial.print(sid);
    }    
    Serial.print("E\n");
  } else if (inbyte == ' '){
    // do nothing 
  } else {
    Serial.write(inbyte);
//    Serial1.write(inbyte);
  }
}

No, the same software in other device is working well with Arduino UNO (using on-board USB-to-serial interface).

Below find the list of errors produced by that failed try.

sketch_nov08b:11:1: error: expected ',' or ';' before 'void'
 void setup() {
 ^~~~
Somewhere\sketch_nov08b\sketch_nov08b.ino: In function 'void loop()':
sketch_nov08b:19:3: error: 'inbyte' was not declared in this scope
   inbyte = ' ';
   ^~~~~~
Somewhere\sketch_nov08b\sketch_nov08b.ino:19:3: note: suggested alternative: 'byte'
   inbyte = ' ';
   ^~~~~~
   byte
sketch_nov08b:25:10: error: 'i' was not declared in this scope
     for (i = 0; i < SONAR_NUM; i++){
          ^
sketch_nov08b:27:7: error: 'sid' was not declared in this scope
       sid = String(i + 1);
       ^~~
Somewhere\sketch_nov08b\sketch_nov08b.ino:27:7: note: suggested alternative: 'sin'
       sid = String(i + 1);
       ^~~
       sin
sketch_nov08b:28:7: error: 'duration' was not declared in this scope
       duration = sonar[i].ping();
       ^~~~~~~~
Somewhere\sketch_nov08b\sketch_nov08b.ino:28:7: note: suggested alternative: 'union'
       duration = sonar[i].ping();
       ^~~~~~~~
       union
sketch_nov08b:29:7: error: 'distance' was not declared in this scope
       distance = (duration/2)*0.0343;
       ^~~~~~~~
Somewhere\sketch_nov08b\sketch_nov08b.ino:29:7: note: suggested alternative: 'isSpace'
       distance = (duration/2)*0.0343;
       ^~~~~~~~
       isSpace
sketch_nov08b:31:7: error: 'dist' was not declared in this scope
       dist = String(distance);
       ^~~~
Somewhere\sketch_nov08b\sketch_nov08b.ino:31:7: note: suggested alternative: 'div'
       dist = String(distance);
       ^~~~
       div
sketch_nov08b:32:7: error: 'outpackage' was not declared in this scope
       outpackage = "u"+sid+" "+dist+"\n";
       ^~~~~~~~~~
sketch_nov08b:34:34: error: expected primary-expression before '/' token
       Serial1.print(outpackage);*/
                                  ^

Using Strings will not help your performance, nor stability.

To get anywhere with this, I'd like to see the compilable code that you ran on the Uno and the code that ran on the Mega. They can't be the same, if only because you're using Serial1 in the Mega version.

It would be nice too if you would run all the code you post and confirm what it does.

Sorry that I tried to simplify the code for discussion. The following code is successfully run Mega with 'C' command every 1 second but failed for 100 ms.

#include <Wire.h>
#include <NewPing.h>
#define SONAR_NUM 3                      // Number of Sonar
#define MAX_DISTANCE 400                  // Maximum distance (in cm) to ping
NewPing sonar[SONAR_NUM] = {              // Ultrasonic Sensor Object array
  NewPing(13, 12, MAX_DISTANCE),
  NewPing(11, 10, MAX_DISTANCE),
  NewPing(9, 8, MAX_DISTANCE)
};

uint8_t i;
char inbyte;           // command byte received from Rpi (Serial 1)
String sid;           // String of Ultrasonic sensor id
String dist;          // String of distance value
String outpackage;    // String sent to rpi via Serial 1
String irsensor;      // String of ir sensor id
String irstate;       // ir sensor state
// unsigned int iteration;
unsigned int duration;
unsigned int distance;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial1.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  inbyte = ' ';

  if (Serial.available() > 0){
    inbyte = Serial.read(); 
  }
  if (inbyte == 'C'){
    for (i = 0; i < SONAR_NUM; i++){
//      delay(10);
      sid = String(i + 1);
      duration = sonar[i].ping();      
      distance = (duration/2)*0.0343;
      distance = sonar[i].ping_cm();
      dist = String(distance);
      outpackage = "u"+sid+" "+dist+"\n";
      Serial.print(outpackage);
      Serial1.print(outpackage);
    }    
    Serial.print("E\n");
  } else if (inbyte == ' '){
    // do nothing 
  } else {
    Serial.write(inbyte);
//    Serial1.write(inbyte);
  }
}

I further simplified the code as below to run both in Mega and UNO with 'C' command every 100 ms. It is success in UNO but failed in Mega.

#include <Wire.h>
#include <NewPing.h>
#define SONAR_NUM 3                      // Number of Sonar
#define MAX_DISTANCE 400                  // Maximum distance (in cm) to ping
NewPing sonar[SONAR_NUM] = {              // Ultrasonic Sensor Object array
  NewPing(13, 12, MAX_DISTANCE),
  NewPing(11, 10, MAX_DISTANCE),
  NewPing(9, 8, MAX_DISTANCE)
};

uint8_t i;
char inbyte;           // command byte received from Rpi (Serial 1)
String sid;           // String of Ultrasonic sensor id
String dist;          // String of distance value
String outpackage;    // String sent to rpi via Serial 1
String irsensor;      // String of ir sensor id
String irstate;       // ir sensor state
// unsigned int iteration;
unsigned int duration;
unsigned int distance;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial1.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  inbyte = ' ';

  if (Serial.available() > 0){
    inbyte = Serial.read(); 
  }
  if (inbyte == 'C'){
    for (i = 0; i < SONAR_NUM; i++){
//      delay(10);
/*      sid = String(i + 1);
      duration = sonar[i].ping();      
      distance = (duration/2)*0.0343;
      distance = sonar[i].ping_cm();
      dist = String(distance);
      outpackage = "u"+sid+" "+dist+"\n";
      Serial.print(outpackage);
      Serial1.print(outpackage);*/
      sid = String(i);
      Serial.print(sid);
    }    
    Serial.print("E\n");
  } else if (inbyte == ' '){
    // do nothing 
  } else {
    Serial.write(inbyte);
//    Serial1.write(inbyte);
  }
}

Please run the code you post. That latest one won't compile on an Uno and I'm not going to bother trying to figure out what you really did.