Sending values from Matlab to arduino using serial communication

조회 수: 14 (최근 30일)
Naseeb Gill
Naseeb Gill 2017년 2월 18일
댓글: Walter Roberson 2019년 11월 20일
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
Sravani Vanama
Sravani Vanama 2019년 11월 19일
how to print the values send from matlab to arduino??
Walter Roberson
Walter Roberson 2019년 11월 20일
There are a few possibilities for display:

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

답변 (3개)

Walter Roberson
Walter Roberson 2017년 2월 21일
"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
Naseeb Gill
Naseeb Gill 2017년 2월 21일
편집: Naseeb Gill 2017년 2월 21일
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.

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


Rouis Jihene
Rouis Jihene 2017년 5월 31일
Hello, i tried the code but i don't get the value (doi) in arduino ! why? please help
  댓글 수: 3
Rouis Jihene
Rouis Jihene 2017년 6월 4일
Hello Mr Walter Roberson, even i use the instruction that you told me but i doesn't work ! please help
Walter Roberson
Walter Roberson 2017년 6월 4일
Sorry, I do not have an arduino to test with.

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


Azrg
Azrg 2019년 10월 23일
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.

카테고리

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