Sending an 11 element array of ints out through the serial port

Hi,

I am new to coding and in need of some guidance.

I am trying to send an 11 element array of 16 bit integers from my arduino to matlab and i am having a little trouble sending it efficiently. I want to use serial.write(), but that only sends one byte of my array element…i obviously need both bytes.

Does anyone know of some stock code or library that will read my 11 element array send it out on the serial?

i posted below the code as it currently is. Right now, the arduino inefficiently sends the entire array out a character string.
It is for a FreeIMU if you happen to know what this and are curious.

#include <ADXL345.h>
#include <bma180.h>
#include <HMC58X3.h>
#include <ITG3200.h>
#include <MS561101BA.h>
#include <I2Cdev.h>
#include <MPU60X0.h>
#include <EEPROM.h>

//#define DEBUG
#include "DebugUtils.h"
#include "FreeIMU.h"
#include <Wire.h>
#include <SPI.h>

int raw_values[11];
char str[512];
float val[9];


// Set the default object
FreeIMU my3IMU = FreeIMU();

void setup() { 
  Serial.begin(115200);
  Wire.begin();
  
  delay(500);
  my3IMU.init(true); // the parameter enable or disable fast mode
  delay(500);
}

void loop() {
  my3IMU.getRawValues(raw_values);
  #if HAS_MS5611() 
    // with baro
    sprintf(str, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d", raw_values[0], raw_values[1], raw_values[2], raw_values[3], raw_values[4], raw_values[5], raw_values[6], raw_values[7], raw_values[8], raw_values[9], raw_values[10]);
  #else  
    // without baro
    sprintf(str, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t", raw_values[0], raw_values[1], raw_values[2], raw_values[3], raw_values[4], raw_values[5], raw_values[6], raw_values[7], raw_values[8]);
  #endif
  Serial.print(str);
  Serial.print('\n');
  
  /*
  my3IMU.getValues(val);
  sprintf(str, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d", int(val[0]), int(val[1]), int(val[2]), int(val[3]), int(val[4]), int(val[5]), int(val[6]), int(val[7]), int(val[8]));  
  Serial.print(str);
  Serial.print(10, BYTE);
  */
}

Is that all you're sending or will there be more?

Have you tried a FOR loop and Serial.print?

Yes, i tried that. It prints one byte of the array element only.

rameymp: Yes, i tried that. It prints one byte of the array element only.

Show what you tried. A for loop is the right way.

i tried the below code, but serial monitor only gave me one character for each element

#include <ADXL345.h>
#include <bma180.h>
#include <HMC58X3.h>
#include <ITG3200.h>
#include <MS561101BA.h>
#include <I2Cdev.h>
#include <MPU60X0.h>
#include <EEPROM.h>

//#define DEBUG
#include "DebugUtils.h"
#include "FreeIMU.h"
#include <Wire.h>
#include <SPI.h>

int raw_values[11];
char str[512];
float val[9];


// Set the default object
FreeIMU my3IMU = FreeIMU();

void setup() { 
  Serial.begin(115200);
  Wire.begin();
  
  delay(500);
  my3IMU.init(true); // the parameter enable or disable fast mode
  delay(500);
}

void loop() {
  my3IMU.getRawValues(raw_values);
  #if HAS_MS5611() 
    // with baro
    sprintf(str, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d", raw_values[0], raw_values[1], raw_values[2], raw_values[3], raw_values[4], raw_values[5], raw_values[6], raw_values[7], raw_values[8], raw_values[9], raw_values[10]);
  #else  
    // without baro
    sprintf(str, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t", raw_values[0], raw_values[1], raw_values[2], raw_values[3], raw_values[4], raw_values[5], raw_values[6], raw_values[7], raw_values[8]);
  #endif
  
  
  for(int i=0; i<=10; int++)
  {
    Serial.write(raw_values[i]);
  }
  
  //Serial.print(str);
  Serial.print('\n');
  
  
  /*
  my3IMU.getValues(val);
  sprintf(str, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d", int(val[0]), int(val[1]), int(val[2]), int(val[3]), int(val[4]), int(val[5]), int(val[6]), int(val[7]), int(val[8]));  
  Serial.print(str);
  Serial.print(10, BYTE);
  */
}

There is a difference between Serial.print and Serial.write, can you figure out the difference?

yes, i think print uses ascii characters to represent numbers. and serial.write() actually uses the number represented in binary

  for(int i=0; i<=10; int++)

i++ more like, not int++

And you are going to want some sort of separator unless you want a jumbled mess of numbers like:

2342934876342598736543

  for(int i=0; i<=10; i++)
  {
    Serial.print(raw_values[i]);
    Serial.print ('\t');
  }
  Serial.println ();

sorry, that was a mistake. I had to retype it after you asked me and i didn’t check to see if it compiled.

I was hoping to parse it on the matlab side. I just need to get the data out whole and in order.

Nick,

won't the Print function use ascii characters to represent the actual number? I was hoping to avoid that for efficiency reasons.

How does matlab receive the incoming data, as whole numbers or individual characters?

it depends. matlab is kind of setup to receive numbers, but there are ascii decode blocks that can do the job. Although, i am having trouble with matlab missing the terminator for some reason.

Is it NULL terminated or will any non numerical character work?

When the for loop is done, send Serial.print('/0'); or a simple period ('.') .

matlab(simulink) allows for selection of the standard terminating characters like /n,/t, /lf. How does the arduino terminate?

'/n' newLine, '/c' carrier Return or '/0' null

rameymp: Nick,

won't the Print function use ascii characters to represent the actual number? I was hoping to avoid that for efficiency reasons.

Yes it will. However you could change it to do a write, however I think you would need to do the individual bytes in that case. If you are sending ints you could use lowByte and highByte macros.

You have be pretty damn sure you are synchronized at the receiving end in this case, one byte out and the results will be meaningless.

matlab(simulink) allows for selection of the standard terminating characters like /n,/t, /lf.

I don't see how that will work, because \n is the value 10, and if you need to send the value 10 from your array it will look the same.

How does the arduino terminate?

It terminates with whatever you choose to output.

This demo illustrates sending binary data from a PC to an Arduino. It would be the same concept in the other direction. I don't know how Matlab would handle binary data, but it is easy with Python.

...R

Thanks for the help everyone!

Although, with your foreboding comments about using binary data....i may just break down and send ascii.

If i get it to work, i will definitely post the code. I haven't been able to find anyone else who has tried this....maybe there is a reason for that.