Nano Every & Serial Ports

Hi, looking for some help please.

I have an Nano Every hooked up to a MPU 9250 compass and also a max3232 ttl to rs232 converter so I can output a nmea sentence.

The sketch runs fine when using Serial (ie the usb serial port), but when I add Serial1 to output to the max3232 board via the tx pin, the sketch runs for maybe 20 secounds or so before stopping and I can see via the serial monitor that the data is incorrect (compass readings jumping while compass is not moving).

For some reason disabling Serial will not run the sketch either.

Any ides would be appreciated

Thanks

Tom

 #include <Wire.h>  
 #include "I2Cdev.h"  
 #include "RTIMUSettings.h"  
 #include "RTIMU.h"  
 #include "RTFusionRTQF.h"   
 #include "CalLib.h"  
 #include <EEPROM.h>  

  
   
 RTIMU *imu;               // the IMU object  
 RTFusionRTQF fusion;          // the fusion object  
 RTIMUSettings settings;         // the settings object  
  
 // User defined variables (for your own boat and geographic location):  
 float magd = 3;     // magnetic deviation; if it's East, it's negative  
  


   
 // Variables preset with recommended values (but can be changed):  
 #define DISPLAY_INTERVAL 1000  // rate (in milliseconds) in which MPU9250 results are displayed  
   
 // Global variables:  
 unsigned long lastDisplay;  
 unsigned long lastRate;  
 int sampleCount;  
 float r = M_PI/180.0f; // degrees to radians  
 float d = 180.0f/M_PI; // radians to degrees 

const byte buff_len = 200;
char CRCbuffer[buff_len];

// create pre-defined strings to control flexible output formatting
String sp = " ";
String delim = ",";
String splat = "*";
String msg = ""; 
float yaw;
float hdm;
float hdt;


void setup()  
 {  
   int errcode;  
   
   Serial.begin(4800);   // output port to computer 
   //Serial1.begin(4800);
    while (!Serial1) {
     // wait for serial port to connect.
  }
    while (!Serial) {
      
  }

   Wire.begin();  
   imu = RTIMU::createIMU(&settings);  
   
   // Checking the IMU, if it fails, simply restarting the connection seems to work sometimes  
   if ((errcode = imu->IMUInit()) < 0) {  
     Serial1.print("Failed to init IMU: "); Serial1.println(errcode);  
   }  
   if (imu->getCalibrationValid())  
     Serial1.println("Using compass calibration");  
   else  
     Serial1.println("No valid compass calibration data");  
   
   lastDisplay = lastRate = millis();  
   sampleCount = 0;  
   
   // Slerp power controls the fusion and can be between 0 and 1  
   // 0 means that only gyros are used, 1 means that only accels/compass are used  
   // In-between gives the fusion mix. 0.02 recommended.  
     
   fusion.setSlerpPower(0.02);  
     
   // use of sensors in the fusion algorithm can be controlled here  
   // change any of these to false to disable that sensor  
     
   fusion.setGyroEnable(true);  
   fusion.setAccelEnable(true);  
   fusion.setCompassEnable(true);  
 }  
   

   
 void loop()  
 { 

 
   // build msg
  char strX[4];   //number of charatrers in string
  char strY[4];
  
  
  float hdt = hdm-magd;           // calculate true heading  
       if (hdt > 360) {  
         hdt = hdt-360;  
       } 
       else if (hdt < 0.0) {  
         hdt = hdt+360;  
       }
        
  int x = hdt;  //heading true
  int y = hdm;   // heading mag
  
  
  String cmd = "$SDVHW";    // a command name
  String T = "T";
  String M = "M";
  dtostrf(x,1,0,strX);      // format float value to string XX.X
  dtostrf(y,1,0,strY);      
  msg = cmd + delim + strX + delim + T + delim + strY + delim + M + delim + delim + delim + delim + splat;
  outputMsg(msg); // print the entire message string, and append the CRC

  
   unsigned long now = millis();  
   unsigned long delta;  
   int loopCount = 1;  
    
   while (imu->IMURead()) {  
     if (++loopCount >= 10)  
       continue;  
     fusion.newIMUData(imu->getGyro(), imu->getAccel(), imu->getCompass(), imu->getTimestamp());  
     sampleCount++;  
     if ((delta = now - lastRate) >= 1000) {      
       sampleCount = 0;  
       lastRate = now;  
     }  
     if ((now - lastDisplay) >= DISPLAY_INTERVAL) {  
       lastDisplay = now;  
       RTVector3 pose = fusion.getFusionPose();  
       RTVector3 gyro = imu->getGyro();  
       float roll = pose.y()*-1*d;     // negative is left roll  
       float pitch = pose.x()*d;       // negative is nose down  
       float yaw = pose.z()*d;         // negative is to the left of 270 magnetic  
       float rot = gyro.z()*d;         // negative is to left  
       //float hdm = yaw-90;             // 0 yaw = 270 magnetic; converts to mag degrees  
       if (yaw < 90 && yaw >= -179.99) {  
        hdm = yaw+270;
       }  
       else if (yaw >= 90 && yaw < 179.99) {
        hdm = yaw-90;
       }
      }  
    }

 }
    void outputMsg(String msg) {
    msg.toCharArray(CRCbuffer,sizeof(CRCbuffer)); // put complete string into CRCbuffer
 
    byte crc = convertToCRC(CRCbuffer);
    Serial1.print(msg);  // omit CRC in console msg
    if (crc < 16) Serial1.print("0");  // add leading zero if needed
    Serial1.println(crc,HEX); 

    Serial.print(msg);  // omit CRC in console msg
    if (crc < 16) Serial.print("0");  // add leading zero if needed
    Serial.println(crc,HEX);
 }



byte convertToCRC(char *buff) {
  // NMEA CRC: XOR each byte with previous for all chars between '

and ‘*’
  char c;
  byte i;
  byte start_with = 0;
  byte end_with = 0;
  byte crc = 0;

for (i = 0; i < buff_len; i++) {
    c = buff[i];
    if(c == ’


){
      start_with = i;
    }
    if(c == '*'){
      end_with = i;
    }      
  }
  if (end_with > start_with){
    for (i = start_with+1; i < end_with; i++){ // XOR every character between '

and ‘*’
      crc = crc ^ buff[i] ;  // compute CRC
    }
  }
  else {
    Serial1.println(“CRC ERROR”);
  }
  return crc;
  //based on code by Elimeléc López - July-19th-2013
}
// -----------------------------------------------------------------------

did you wire ground?
use higher baud rate. for example 115200 baud. 4800 slows your sketch down

Hi, thanks for your reply.

What did you mean by wire ground??, I have all the ground pins connected together already.

Ill try the higher baud rate, but the nmea standard for the equipment is 4800, although I belive is also has a high speed 38400 port as well.

Tom

The higher baud rates did not help, still crashing/stopping output when but Serial and Serial1 in use!!

Thanks

Tom

Solved by using software serial instead of the second independent hardware serial port.

Use a Teensy 3.2. Serial, Serial1 and Serial2 all work with the hardware UARTs just fine. The Every has much hardware and very little software/library support

I am also in search of a second hardware serial on the every.

I also wonder what serial port lights up the tx rx lights i have yet to see them light on two everys using Serial1

I have need to receive and transmit at two different speeds higher than software serial would allow.

I bought a DUE which is overkill for my sketch, just to get more hardware serials.

It would mean “Every”thing to get a second serial on a board the size of a nano.

OK look into MEGAcoreX, its very easy to use unlock the full potential of the nano every.

Now i am quite happy, 2 working hardware UART ports and a bunch more cross functionality of pins that i will never use.

Download MegacoreX in your board manager read the how to on git hub it has the pin outs and walk through there

MegaCoreX

potatofarmer:
I also wonder what serial port lights up the tx rx lights i have yet to see them light on two everys using Serial1

It would mean “Every”thing to get a second serial on a board the size of a nano.

As you have discovered, the standard serial port drivers are somewhat lacking. I've had them lock up and only recover with a board reset or reissuing of the call to begin. Problem has eluded me, but I've gotten the ports to work by just not using the standard Arduino Serial library.
The TX and RX lights are connected to the RX/TX pins between the SAM microcontroller (the USB computer interface) and the ATmega4809, not to any board pins. This is device Serial in the library which is USART3 in the microcontroller.