1

When I run this script serial communication just stops after a random period of time. There is no error it just stops outputting data and does not respond to inputs in IDE. Does anyone know a fix for this?. This is not an IDE issue as serial communication also stops without any reported error when reading serial in a python script.

// mpu9250 stuff
#include <MPU9250_WE.h>
#include <Wire.h>
#define MPU9250_ADDR 0x68
MPU9250_WE myMPU9250 = MPU9250_WE(MPU9250_ADDR);
// motor stuff
#include <AFMotor.h>
AF_DCMotor motorA(1);
AF_DCMotor motorB(2);
int lf;
int rf;
void setup() {
  Serial.begin(115200);
  Serial.setTimeout(10);
  // compass stuff
  Wire.begin();
  if(!myMPU9250.init()){
    Serial.println("MPU9250 does not respond");
  }
  else{
    Serial.println("MPU9250 is connected");
  }
  if(!myMPU9250.initMagnetometer()){
    Serial.println("Magnetometer does not respond");
  }
  else{
    Serial.println("Magnetometer is connected");
  }

  Serial.println("Position you MPU9250 flat and don't move it - calibrating...");
  delay(1000);
  myMPU9250.autoOffsets();
  Serial.println("Done!");
  myMPU9250.setSampleRateDivider(5);
  myMPU9250.setAccRange(MPU9250_ACC_RANGE_2G);
  myMPU9250.enableAccDLPF(true);
  myMPU9250.setAccDLPF(MPU9250_DLPF_6);
  myMPU9250.setMagOpMode(AK8963_CONT_MODE_100HZ);
  delay(200); 

  // motors
  motorA.setSpeed(50);
  motorB.setSpeed(50);
  motorA.run(RELEASE);
  motorB.run(RELEASE);
  
  

}

void loop() {
  xyzFloat angles = myMPU9250.getAngles();
  xyzFloat magValue = myMPU9250.getMagValues();
  Serial.print(magValue.x);
  Serial.print(",");
  Serial.print(magValue.y);
  Serial.print(",");
  Serial.print(magValue.z);
  Serial.print(",");
  Serial.print(angles.x);
  Serial.print(",");
  Serial.println(angles.y);

  //motors
  if (Serial.available()>0){
    String ser = Serial.readString();
    String left = (split(ser, '.', 0));
    String right = (split(ser, '.', 1));
      if (ser.length() > 0){
        int lf = left.toInt();
        int rf = right.toInt();
        Serial.print(lf);
        Serial.print("   ");
        Serial.println(rf);
        motorA.setSpeed(lf);
        motorB.setSpeed(rf);
        if (lf > 0){
          motorA.run(BACKWARD);
          }
        if (lf == 0){
          motorA.run(RELEASE);
          }
        if (lf < 0){
          motorA.run(FORWARD);
          }
        if (rf > 0){
          motorB.run(BACKWARD);
          }
        if (rf == 0){
          motorB.run(RELEASE);
          }
        if (rf < 0){
          motorB.run(FORWARD);
          }
    }
  }

  if(!Serial) {  //check if Serial is available... if not,
    Serial.end();      // close serial port
    delay(10);    
    Serial.begin(115200); // reenable serial again
    Serial.println("reset");
  }
    
}
String split(String data, char separator, int index)
{
    // spliting a string and return the part nr index
    // split by separator
    
    int stringData = 0;        //variable to count data part nr 
    String dataPart = "";      //variable to hole the return text
    
    for(int i = 0; i<data.length()-1; i++) {    //Walk through the text one letter at a time
      
      if(data[i]==separator) {
        //Count the number of times separator character appears in the text
        stringData++;
        
      }else if(stringData==index) {
        //get the text when separator is the rignt one
        dataPart.concat(data[i]);
        
      }else if(stringData>index) {
        //return text and stop if the next separator appears - to save CPU-time
        return dataPart;
        break;
        
      }

    }
    //return text if this is the last part
    return dataPart;
}
3
  • 4
    You're turning your heap into swiss cheese with all that String abuse. Commented Jul 9, 2021 at 13:30
  • 6
    And here is the obligatory Majenko blog entry about the Evils of Arduino Strings. Commented Jul 9, 2021 at 14:23
  • add the serial output to your question. Commented Jul 10, 2021 at 7:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.