Send numerical values from Matlab to arduino
6 次查看(过去 30 天)
显示 更早的评论
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
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
2019-2-23
It didnt work for me and gives the error:
"OBJ must be connected to the hardware with FOPEN"
回答(1 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File 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!