Sending values from Matlab to arduino using serial communication

24 visualizaciones (últimos 30 días)
Naseeb Gill
Naseeb Gill el 18 de Feb. de 2017
Comentada: Walter Roberson el 20 de Nov. de 2019
I want to send numeric value from matlab to arduino but code is not working. Matlab code is as:
doi = 3 ;
arduino=serial('COM5','BaudRate',9600); % create serial communication object
fopen(arduino); % initiate arduino communication
fprintf(arduino, '%s', char(doi)); % send answer variable content to arduino
fclose(arduino);
Arduino code is as:
int solenoidPin = 13; //This is the output pin on the Arduino
int doi;
void setup()
{
Serial.begin(9600);
//pinMode(13, OUTPUT);
pinMode(solenoidPin, OUTPUT); //Sets that pin as an output
}
void loop()
{
if(Serial.available()>0)
{
doi = Serial.read();
//Serial.println(doi);
//Serial.println("\n");
//doi = doi - 48;
if (doi == 1)
{
//Serial.println(1);
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
delay(1000); //Wait .15 Second
digitalWrite(solenoidPin, LOW);
}
else if (doi == 2)
{
//Serial.println(2);
digitalWrite(solenoidPin, HIGH); //Switch Solenoid OFF
delay(2000); //Wait .165 Second
digitalWrite(solenoidPin, LOW);
}
else
{
//Serial.println(3);
digitalWrite(solenoidPin, HIGH); //Switch Solenoid OFF
delay(3000); //Wait .180 Second
digitalWrite(solenoidPin, LOW);
}
}
}
I used str2num(doi) also instead of fprintf(arduino, '%s', char(doi)) but no output.
Please give suggestion to correct this.
Thanks.
  5 comentarios
Sravani Vanama
Sravani Vanama el 19 de Nov. de 2019
how to print the values send from matlab to arduino??
Walter Roberson
Walter Roberson el 20 de Nov. de 2019
There are a few possibilities for display:

Iniciar sesión para comentar.

Respuestas (3)

Walter Roberson
Walter Roberson el 21 de Feb. de 2017
"But what the problem is I do not know how to use _fopen(arduino) command between functions in GUI?"
fopen once and save the object somewhere so that you do not need to fopen again.
  1 comentario
Naseeb Gill
Naseeb Gill el 21 de Feb. de 2017
Editada: Naseeb Gill el 21 de Feb. de 2017
I already tried handles as you suggested as below:
function varargout = exam(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @exam_OpeningFcn, ...
'gui_OutputFcn', @exam_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
function exam_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = exam_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function edit1_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton1.
% pushbutton to create serial communication between matlab and arduino
function pushbutton1_Callback(hObject, eventdata, handles)
arduino=serial('COM5','BaudRate',9600); % create serial communication object on port COM4
fopen(arduino); % initiate arduino communication
pause(2);
handles.arduino = arduino;
guidata(hObject,handles);
% --- Executes on button press in pushbutton2.
% After enter value in edit box press pushutton to send data to arduino
function pushbutton2_Callback(hObject, eventdata, handles)
ard = handles.arduino;
number = str2num(char(get(handles.edit1,'String')));
fprintf(ard, '%i', number);
guidata(hObject,handles);
% --- Executes on button press in pushbutton3.
% to clear the port and close arduino communication
function pushbutton3_Callback(hObject, eventdata, handles)
fclose(handles.arduino);
delete(instrfind({'Port'},{'COM5'}));
guidata(hObject,handles);
But arduino is not working. It just blink once when I gave input through edit box. And it is not showing any error too. Can you suggest me the solution by seeing above matlab GUI code. Thanks.

Iniciar sesión para comentar.


Rouis Jihene
Rouis Jihene el 31 de Mayo de 2017
Hello, i tried the code but i don't get the value (doi) in arduino ! why? please help
  3 comentarios
Rouis Jihene
Rouis Jihene el 4 de Jun. de 2017
Hello Mr Walter Roberson, even i use the instruction that you told me but i doesn't work ! please help
Walter Roberson
Walter Roberson el 4 de Jun. de 2017
Sorry, I do not have an arduino to test with.

Iniciar sesión para comentar.


Azrg
Azrg el 23 de Oct. de 2019
The thing you want to send has to be in a while loop like
while true
fprint(something);
end
Because Matlab has to keep trying to send the information until Arduino receives it.

Categorías

Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by