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