Use Matlab for measurements

Hi,

I am working on my first research project for university. I want to use the arduino for some measurements and plot the results with Matlab. To make my work easy I had the Idea to create a GUI to define my parameters, start the measurement and in the end plot the data.

Before I actually start with the GUI etc. I tried to get familiar with Matlab <-> Arduino and serial communication, so I wrote a simple skript in Matlab that asks for an integer x and prints it to the arduino. The Arduino then should blink x times.
If I use the serial monitor for communication with the arduino, it works fine but in Matlab it doesn't.
I am new to Serial Communication with Matlab so I don't really now how it is done right but I figure it has something to do with the format Matlab sends the data.

Can you help me?
I am using an Arduino Uno on my Mac and the 2015a release of Matlab.

The Matlab Script is the following blink.m:

clear
clc
arduino=serial('/dev/tty.usbmodem1411','BaudRate',9600); %create serial object
fopen(arduino); %open connection to arduino
pause(2);

sendData = input('How many times shall I blink?: '); %user input 
fprintf(arduino, sendData); %transmit data to arduino
fprintf(arduino, '\r\n');

fclose(arduino); %close connection
delete(arduino);

And on my Arduino:

char incoming_char;
long serial_input_number;
 void setup(){
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}
 void loop() {
  digitalWrite(13,LOW); //turn off LED
  delay(500);
if(Serial.available()>0){
    incoming_char = Serial.read();
    switch(incoming_char){
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
      serial_input_number=serial_input_number*10+(incoming_char-'0');
      break;
}
 for(int i= 0; i<serial_input_number; i++){
 	digitalWrite(13, HIGH);
 	delay(200);
 	digitalWrite(13,LOW);
 	delay(200);
 }
	Serial.flush();
}
}

Thank you.
juhecomp

I am new to Serial Communication with Matlab so I don't really now how it is done right but I figure it has something to do with the format Matlab sends the data.

The Arduino code could be a lot shorter:

  if(Serial.available()>0)
  {
    incoming_char = Serial.read();
    if(incoming_char >= '0' && incoming_char <= '9')
    {
       serial_input_number = (serial_input_number * 10) + incoming_char - '0';

Doing that, and putting all { on lines by themselves, and using Tools + Auto Format to properly indent the code, it should become obvious what the problem is.

You read one character from the serial port, then blink the LED. Then, you read another character from the serial port, and blink the LED. Then, you read another character from the serial port, and blink the LED. Then, you read another character from the serial port, and blink the LED.

Suppose you send enter 12. The '1' arrives, so you blink the LED once.

Later on, the '2' arrives, so you blink the LED 12 times.

Then, the carriage return arrives. You blink the LED 12 more times. Then, the line feed arrives. You blink the LED 12 more times.

Meanwhile, when there is no serial data, you blink the LED 12 times on each pass through loop.

Suppose you then enter 8. How many times are you now going to blink the LED?

The Serial.flush() blocks until all outgoing serial data has been sent. How is that even remotely useful, since your code contains NO Serial.print() or Serial.write() calls?

Thank you for your advice. I changed my code so that it should work now. Using the serial monitor it works fine now but with Matlab I still get no reaction :frowning:

but with Matlab I still get no reaction

You are aware that opening the serial port resets the Arduino, right?

You need to allow time for that to happen. The best way is to have the Arduino send something when it is ready, and have Matlab wait until it has received the Arduino's output before it sends anything.