Communication between 2 Arduinos and delay

Hi everyone.

I made a code to control some motors but whey are so many sensors that the time delay from each loop was too long. Is result in bad answers for my project.
then I think of one solution, using another Arduino to process the information and then, using serial communication (Tx, Rx), sending the final information to the Main Arduino.

But it also returns me a long delay in a loop when I receiver the information (About 20000 us, for each receiver loop) and I need that it doesn’t pass 1000us or 1500us.

I tried to receiver one char peer loop and, when the information is complete, interpret the string.
It’s not so important how much time I need to receive the data but it’s really important that each loop doesn’t pass 1500 us.

Can someone give me a solution?
There are my codes:

Transmitter

#include <SimpleKalmanFilter.h>

//Sender Code
#include <Wire.h>
#include <MS5611.h>


SimpleKalmanFilter pressureKalmanFilter(1, 1, 0.01);

char data[5];

MS5611 ms5611;

float estimated_altitude;

void setup() {
  Serial.begin(9600);
  while(!ms5611.begin(MS5611_ULTRA_HIGH_RES))
  {
    delay(500);
  }
}

void loop() {
  double realTemperature2 = ms5611.readTemperature(true);
  long realPressure2 = ms5611.readPressure(true);
  double realAltitude2 = ms5611.getAltitude(realPressure2);

  estimated_altitude = pressureKalmanFilter.updateEstimate(realAltitude2);
  char *valor = dtostrf(estimated_altitude,4,2,data);
Serial.println(valor);
}

Receiver

const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data

boolean newData = false;
unsigned long loop_timer;

 static byte ndx = 0;
 char endMarker = '\n';
 char rc;

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

void loop() {



           if (Serial.available() > 0 && newData == false) {
 rc = Serial.read();

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


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

 
  Serial.println(micros() - loop_timer);
  loop_timer = micros(); 
}

Thanks until yet for our help

Serial transmission of your valor data is going to take about 6ms. Increasing the baud rate will improve your speed, but even then, it may not be enough.

Two points:

  • Your ‘data[5]’ buffer may not be big enough. I'm thinking you're writing beyond its bounds.

  • Better yet, don’t even bother with dtostrf(). Send your ‘estimated_altitude’ variable as binary. A float only occupies 4 bytes.

I wonder if this is another case of a poorly written library that uses blocking code to get sensor data. Maybe it would be better not to use the library and figure out your own code to read the sensors. Some sensors need a message to start a reading and then take some time to produce the result. But the Arduino may not have to wait around with its hands in its pockets while the reult is being prepared. It may be able to go off and do something useful and come back for the result when it is ready.

If my supposition is correct then it could all be done on one Arduino.

As it is you are not using the code from Serial Input Basics correctly. Look carefully at how my examples are structured and use the same structure for your program. My examples have been specifically written so that they do NOT block while waiting for data.

...R

That's really a problem with the library, I sow that are two delays of over 1s. But I was trying to optimize the delay problem with another Arduino. Using faster baud rate and "yet" made the code really faster, and it helps. But I think that the best solution is really to understand the sensor and try to make my own library. I just never made it before, that is, I need to study a little more.

thanks, to everyone.

donraf:
and try to make my own library.

There is no need to build the code into the style of an Arduino library unless you plan to make it available to others in a convenient format.

I suggest you get it working as part of a regular program before considering the extra complications of creating a library.

…R