RX_TX - long delay between packages

Hi,

I'm new to this forum, and I couldn't find a answer to my proplem, so I created this post.

I try to send a 16 char long array over RX_TX to another arduino.
Measuring with an oscilloscope, I found that the baudrate of 115200 is reached, but there is a delay of 1.5 seconds between packages.

transmitter:

String data = "abcdefghijklmnop";
char charArray[16];
data.toCharArray(charArray, 16);
Serial.print(charArray);

receiver:

if(Serial.available() > 0){
    String data = Serial.readString();   
}

Does anyone know, why this delay occurs?

Post complete code, not excerpts!

http://snippets-r-us.com/

Try to avoid the String class, it wastes memory by fragmenting the available RAM space. What type of Arduino are you using?

char charArray[16];This is large enough to store 15 characters plus the zero terminator. Storing more will cause other parts of the program to fail unpredictably.

Your "String" object has 16 characters. As suggested above, avoid Strings on Arduino.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

When you know how to receive data you will be able to figure out how to send it so it matches what the receiver expects.

...R

Ok, here my whole code. (I thought it would be a bit cleaner to post only a example code)

#include <OneWire.h>
#include <DallasTemperature.h>

int trigger=7;
int echo=6;
long time=0;
int temperature = 0;
long distance=0;
int pot_analogPin = 0;
float float_pot_val = 0;
int pot_val = 0;

#define ONE_WIRE_BUS 2
OneWire ourWire(ONE_WIRE_BUS); 
DallasTemperature sensors(&ourWire);


void setup(){
  
  Serial.begin (115200);
  pinMode(echo, INPUT);
  
  sensors.begin();

}

void loop(){

  digitalWrite(trigger, LOW); 
  delay(5); 
  digitalWrite(trigger, HIGH);
  delay(10);
  digitalWrite(trigger, LOW);
  time = pulseIn(echo, HIGH);
  distance = (time/2) * 0.03432;
  int int_distance = distance;
  String str_distance = String(int_distance);

  sensors.requestTemperatures();
  temperature = int(sensors.getTempCByIndex(0));
  String str_temperature = String(temperature);

  float_pot_val = analogRead(pot_analogPin);
  pot_val = int(100*(float_pot_val / 1023));
  String str_pot_val = String(pot_val);
  
  String data = String(str_distance + "," + str_temperature + "," + str_pot_val);
  char charArray[16];
  data.toCharArray(charArray, 16);
  Serial.print(charArray);
}

So as you can see, I’m actually not sending 16 chars, because the values of the sensors never get bigger than three digits.

receiver:

void setup() {
  Serial.begin(115200);
  Serial.setTimeout(2);
}

void loop() {
  if(Serial.available() > 0){
    String data = Serial.readString(); 
  }
}

Basically I’m trying to read three different sensors (the code was just copied from their manuals) and send this data to another arduino.

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

When you know how to receive data you will be able to figure out how to send it so it matches what the receiver expects.

...R

Thank you, this post is great :slight_smile:
But do you think including markers will speed up the whole thing?

P96:
But do you think including markers will speed up the whole thing?

The speed-up will come because the markers allow you to use non-blocking code. For a given baudrate and number of characters my examples will work as fast as is possible. I normally use 500,000 baud between my PC programs and my Arduinos. Even at that rate the Arduino can work very much faster than the characters arrive.

...R

Measuring with an oscilloscope, I found that the baudrate of 115200 is reached, but there is a delay of 1.5 seconds between packages.

This delay probably comes from the sensor readings.

sensors.requestTemperatures();

This line will consume almost a second (default is parasite mode). Post a wiring diagram, we may give you some hints how to make this substantially faster.

pylon:
This delay probably comes from the sensor readings.

sensors.requestTemperatures();

This line will consume almost a second (default is parasite mode). Post a wiring diagram, we may give you some hints how to make this substantially faster.

Ok, thank you :slight_smile:
The temperature sensor actually was the problem.