Matlab-Arduino serial communication problems

Hi there,

I'm working with Arduino since few days and I'm facing some problems, so I would be very grateful if anyone could help.

I have to send some parameters from Matlab to Arduino software in order to make a Led light flickering at specific frequencies. Since I got that Serial.read does read one byte at a time, I used this piece of code I found in order to convert data from in char:

while (Serial.available())
  {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;

while this is the code I'm trying to use in order to send parameters to Arduino:

clear all
clc

%fr = 1; %store data
freqs = 0;

% in order to find the serial port use instrfind
%arduino=serial('/dev/tty.usbmodem1411','BaudRate',9600); % create serial communication object on port COM4
arduino=serial('/dev/cu.usbmodem1411','BaudRate',9600);

fopen(arduino); % initiate arduino communication
pause(10);


fprintf(arduino, '%i', freqs);
y = fscanf(arduino, '%i')


fclose(arduino);

I understand that the variable I'm trying to send to Arduino (namely, 'freqs') is in the wrong format. What should it be? Or do you have any other suggestion?

Thank you very much in advance.

Bests, Federica

What should it be?

How are we supposed to know? What you do with the String you collect from the serial port is not part of the snippet you posted.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

PaulS: How are we supposed to know? What you do with the String you collect from the serial port is not part of the snippet you posted.

Sorry, here is what it does with the string:

if (stringComplete)
   {
     //nel caso, trasformo la stringa in numero
     inputNumber = inputString.toInt();

     //azzero stringa e variabile di controllo,
     //per prepararmi alla prossima lettura seriale!
     inputString = "";
     stringComplete = false;
   }

   if (inputNumber == 0) {
     digitalWrite(ledPin, LOW);
     
   } else {


   // usiamo la variabile numerica appena letta dalla seriale:
   // con l'operatore modulo  '%' possiamo fare un "allarme" 
   // che scatta ogni "inputNumber" loopCounter. 
   // Esempio: se inputNumber è 3, allora ogni 3 loop questo allarme scatta
   // e setta la variabile led_on.
   if (loop_counter % inputNumber == 0)
   {
     led_on = 1 - led_on;  //trucco! ad ogni chiamata led_on sarà 0, poi 1, poi 0, poi 1, etc...
   }


   if (led_on)
   {
     digitalWrite(ledPin, HIGH);
   } else
   { 
     digitalWrite(ledPin, LOW);
   }

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

…R

Hi Robin,
thank you for your useful advices and tutorial.
If I correctly understood I should my code following examples 4 and 5 of your tutorial, right?

fede_mau: If I correctly understood I should my code following examples 4 and 5 of your tutorial, right?

Something like that. The devil is in the detail.

...R