Send numerical values from Matlab to arduino

조회 수: 8 (최근 30일)
Naseeb Gill
Naseeb Gill 2017년 4월 20일
답변: michael jesus 2019년 5월 25일
I have an array (2x4) in Matlab which may contains integer values as well as values in decimals, may e positive or negative. For example: [1.1, 23, -1.56, 5.29; 2.14, 2.39, -67, 4.001]. I have to send these values to arduino using matlab. How to do so? I know how to send integer values to arduino from matlab but it is not working with decimal values.
Matlab Code to send integer values is below:
portName = 'COM5';
s = serial(portName,'BaudRate',9600,'Terminator','LF');
s.timeout = 1;
try
try
fopen(s);
catch
delete(s);
fopen(s);
end
catch
disp('Unable to open the port ');
end
angle = [1.3,2];
dataOut = angle;
dataOut_ = char(dataOut);
fprintf(s,'%d',dataOut_);
Arduino code is given below:
int d1,d2;
char d[4];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0)
{
for(int i=0; i<3;i++)
{
d[i]= Serial.read();
}
d1 = d[0]-'0';
if (d1 == 1.3)
{
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
}
}
  댓글 수: 2
RaulOrtiz Fernandez
RaulOrtiz Fernandez 2018년 7월 6일
Hello, for me, the easiest way to send numerical values from matlab to arduino is the following:
Code in Matlab:
delete(instrfind({'Port'},{'COM4'}));
PS=serial('COM4');
set(PS,'Baudrate',9600); % se configura la velocidad a 9600 Baudios
set(PS,'StopBits',1); % se configura bit de parada a uno
set(PS,'DataBits',8); % se configura que el dato es de 8 bits, debe estar entre 5 y 8
set(PS,'Parity','none'); % se configura sin paridad
set(PS,'Terminator','CR/LF');% “c” caracter con que finaliza el envío
set(PS,'OutputBufferSize',8); % ”n” es el número de bytes a enviar
set(PS,'InputBufferSize' ,8); % ”n” es el número de bytes a recibir
set(PS,'Timeout',5); % 5 segundos de tiempo de espera
kp =input('Ingrese valor de kp: ','s');
fprintf(PS, '%s', kp);
fclose(PS);
delete(PS);
clear PS;
Code in arduino:
int led = 12;
String text;
int ki;
void setup() {
//pinMode(led, OUTPUT);
Serial.begin(9600);
ki = 0;
}
void loop() {
if (Serial.available()){
text = Serial.readStringUntil('\n');
ki = text.toInt();
if(ki == 1000){
analogWrite(led,255);
}
if(ki == 0){
analogWrite(led,0);
}
To test that all work properly, connect a led in the pin d12 and run the code in matlab an put 1000. If works , the led will be blinked.
Hi hope this help you and other people.
Fatima Kishimoto
Fatima Kishimoto 2019년 2월 23일
It didnt work for me and gives the error:
"OBJ must be connected to the hardware with FOPEN"

댓글을 달려면 로그인하십시오.

답변 (1개)

michael jesus
michael jesus 2019년 5월 25일
give me a process in Simulink, I need to pack the data from the graph to the Arduino as I do that communication through the serial port

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by