Sending 16 bits from Matlab to Arduino UNO

Hi,

I’m trying to send a 16 bits sine wave from matlab and receive it with Arduino Serial to the process it. The problem is that I’m not getting the correct sine wave. I’m getting the picture attached. It’s almost the sine wave but in the end the bits die. My Arduino code is the following:

 while (Serial.available() > 1 && configura == false) {

     
     
        //serialFlush();
        dato = Serial.read() + Serial.read()*256;
//        //Serial.read();
//        //Serial.read();
//        if (contador == 0) {
//           planta = dato;
//           setPlanta(planta);
////           Serial.println("planta ");
////           Serial.println(planta,DEC);
//        }
//        else if (contador == 1) {
//           largo = dato;
////           Serial.println("largo ");
////           Serial.println(largo,DEC);
//        }
//
//        else{
//          if (contador-2 >= largo) {
//            configura = true;
//            contador = 0;
//            }
//          signal_rx[contador-2] = dato;
//          //Serial.println(contador);
//          Serial.println(dato);
//        }
        signal_rx[contador] =dato;
        Serial.println(signal_rx[contador]);
        //Serial.println(contador);
        contador++;
         if (contador >= 128){
        configura = false;
      }
      }

And from matlab I’m just sending 16 bits like this:

input_mix = [parametros,input];
% Primero se debe abrir el puerto serial
s = serial(serial_port,'BaudRate',9600);
try
    fopen(s);
catch err
    fclose(instrfind);
    error('Error! Verifica que el puerto esté abierto.');
end
%fwrite9(s,'A','char');

%pause(1);
for i=1:length(input_mix)
   fwrite(s,input_mix(i),'uint16');
   
end



fclose(s);

Idk why it breaks at the end of the sine wave. By the parametros is a 2 length array and input is a 128 length array. Any advise? Thanks!

Any advise?

Post ALL of your code.

Explain what that picture is trying to tell us.

I can't see how input_mix is supposed to be a sine wave.

When you post code, GET RID OF COMMENTED OUT CODE. That code has NOTHING to do with the problem.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R

marcelobg: I'm trying to send a 16 bits sine wave from matlab and receive it with Arduino Serial to the process it.

Why?

Why can't you process the sinewave in Matlab ?

Hi guys,

Thanks for the replies.

The full code is:

#include <SPI.h>
#include <Wire.h>


/// PARAMETROS //////////////////////////////////////////////////////////////////


boolean newData = false;
byte val = 0;
unsigned int dato;
unsigned int signal_rx[128];
byte b[2];
static byte ndx = 0;
unsigned int parametros;
unsigned int planta_nominal = 3;
unsigned int planta_1 = 4;
unsigned int planta_2 = 5;
unsigned int planta_3 = 6;
unsigned int fs;
unsigned int planta = -1;
unsigned int aux_largo;
unsigned int contador;
unsigned int largo;
int numChars;
unsigned int cont;
unsigned int  cont_largo = 0;
boolean configura = false;
//////// CONFIGURACION ////////////////////////////////////////////////////////

void setup()
{
  //Wire.begin(0x10);
  pinMode(planta_nominal,OUTPUT);
  pinMode(planta_1,OUTPUT);
  pinMode(planta_2,OUTPUT);
  pinMode(planta_3,OUTPUT);
  planta = -1;
Serial.begin(9600);
}

////////// MAIN ///////////////////////////////////////////////////////
void loop()
{


  // Primero recibimos los parametros enviados por matlab: Largo, Fs y tipo planta
  while (Serial.available() > 1) {

 
        dato = Serial.read() + Serial.read()*256;
        signal_rx[contador] =dato;
        Serial.println(signal_rx[contador]);
        //Serial.println(contador);
        contador++;
      }
}

Explain what that picture is trying to tell us.

I can’t see how input_mix is supposed to be a sine wave.

This is what input_mix supposed to be: First 2 numbers on the array are parameters and the rest is

n = 61; % numero de muestras
t = linspace(0,2*pi,n);    
Input1 = 2.5+2.5*sin(t); % Señal analogica
quant=10/(2^16-1);
sine_wave=round(Input1/quant); % Señal digital

where sine_wave is input. The picture is the Serial plotter from Arduino IDE. It’s supposed that the Arduino should plot one period of the Sine. As you can see, at te end of the sine it plots rubbish.

Why?

Why can’t you process the sinewave in Matlab ?

Sorry my mistake, I don’t want to process it, I just want it to send the sine t a a DAC then receive the data from the DAC, into another Arduino, use that Arduinos ADC and send it back to the first Arduino via i2c and finally return all to matlab and see if the Sine is a bit the same as before.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

Yeah, I’ve check those examples, they are pretty cool. But they are for 8 bits. I’ve tried changing a bit so i can receive 16 bits, but I didn’t go well. Maybe I was doing something wrong. I’ll try with that

marcelobg: Yeah, I've check those examples, they are pretty cool. But they are for 8 bits. I've tried changing a bit so i can receive 16 bits, but I didn't go well. Maybe I was doing something wrong. I'll try with that

All serial data is sent in 8-bit bytes. If your Matlab code is sending 16 bit binary data you can easily combine the pair of bytes after you have received the data.

It would be much easier to debug your project if your Matlab code sends the data in human readable form. The parse example shows how to convert that back to integers.

...R