matlab-arduino serial communication

17 views (last 30 days)
Handel
Handel on 3 Oct 2011
Answered: navin gopaul on 30 May 2014
hi, i have these code in matlab. this code allow send data for turn on and off some leds in a given sequence:
%Comunicación serial Arduino-MatLab
delete(a);
clear all;
close all;
a = serial('COM3','BaudRate',9600);
fopen(a);
b = [100;200;300;400];
c = [200;300;400;100];
d = [300;400;100;200];
e = [400;100;200;300];
k = size(b);
bs = num2str(b);
cs = num2str(c);
ds = num2str(d);
es = num2str(e);
for i = 1: k(1)
send(i,1:12) = strcat(bs(i,1:3),cs(i,1:3),ds(i,1:3),es(i,1:3));
end
sis = size(send);
sendt = send';
for i = 1: sis(1)*sis(2)
sendr(i) = sendt(i);
end
fwrite(a,sendr)
the arduno code is that:
//Comunicación serial MatLab-Arduino
int incomingbyte[4][3]; //matriz de datos (byte) leidos
/*Declaramos las variables correspondientes a los pines que
encenderán o apagarán a medida que llegan las instrucciones*/
int LedPin13 = 13; //Pin 13 donde el LEDse apagará o encenderá dependiendo de los dattos recibidos
int LedPin10 = 10;
int LedPin7 = 7;
int LedPin3 = 4;
void setup()
{
Serial.begin(9600); //abrir puerto serial y estblecer la velocidad en baudios (bps)
//abrimos los pines como puertos de salida//
pinMode(LedPin13, OUTPUT);
pinMode(LedPin10, OUTPUT);
pinMode(LedPin7, OUTPUT);
pinMode(LedPin3, OUTPUT);
}
void loop()
{
if(Serial.available() > 0)
{
int i;
int j;
for(i = 0; i < 4; i = i + 1)
{
for(j = 0; j < 3; j = j + 1)
{
incomingbyte[i][j] = Serial.read(); // lee el byte entrante
Serial.println(incomingbyte[i][j]);
}
}
int tiempo[4];
int g;
for(g = 0; g < 4; g = g + 1)
{
tiempo[g] = (incomingbyte[g][0] - 48)*100 + (incomingbyte[g][1] - 48)*10 + (incomingbyte[g][2] - 48);
Serial.println(tiempo[g]);
}
digitalWrite(LedPin13, HIGH);//encender el LED
delay(tiempo[0]*10);
digitalWrite(LedPin13, LOW);//apagar el LED
digitalWrite(LedPin10, HIGH);
delay(tiempo[1]*10);
digitalWrite(LedPin10, LOW);
digitalWrite(LedPin7, HIGH);
delay(tiempo[2]*10);
digitalWrite(LedPin7, LOW);
digitalWrite(LedPin3, HIGH);
delay(tiempo[3]*10);
digitalWrite(LedPin3, LOW);
}
}
my problem is that matlab cant send the sendr matrix from the script but in command windows i write the same last line ("fwrite(a,sendr)") then it can run successfully.
thanks a lot for the aid.

Answers (2)

Walter Roberson
Walter Roberson on 3 Oct 2011
The entire MATLAB part of the program after the fopen() can be replaced by
fprintf(a, '%3d',b,c,d,e);
However, in the general case that does not match the input expected by the C program you show, as the code you have also does not match the input expected. To repair, instead use
fprintf(a, '%03d',b,c,d,e);
so that values that happen to be less than 100 have leading 0s put in. (Your existing code would generate spaces in that case, but spaces are not expected by the C code.)

navin gopaul
navin gopaul on 30 May 2014
I got here looking for an answer. solves my problem, thanks.

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!