Sending values from Matlab to arduino using serial communication

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

I would probably use
fwrite(arduino, uint8(doi));
Thanks Sir for your response. But the problem is when I used
fopen(arduino)
matlab set up a communication with arduino and this process take some time like 2 seconds. So, if I used
pause(2)
after fopen(arduino), code works well. But the real problem for me here is that my doi value changes every time and I cannot used fopen(arduino) every time as it takes much time. Actually, I'm using Matlab GUI in which doi value is input through edit text box and then this value send to arduino and arduino code run according to send value. What I'm thinking is to avoid fopen(arduino) repeatedly, I can open it once by pushbutton and then send doi values to it and arduino code work as coded. To close arduino, I can used fclose(arduino)_in other pushbutton. But what the problem is I do not know how to use _fopen(arduino) command between functions in GUI? Can you please help me to solve this? Thanks.
how do you know that it takes 2 sec to open?
how to print the values send from matlab to arduino??
There are a few possibilities for display:

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

답변 (3개)

Walter Roberson
Walter Roberson 2017년 2월 21일

0 개 추천

"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

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일

0 개 추천

Hello, i tried the code but i don't get the value (doi) in arduino ! why? please help

댓글 수: 3

On the arduino side, you are probably trying to read the data as text instead of as binary. The replacement for
fprintf(arduino, '%s', char(doi)); % send answer variable content to arduino
should be
fprintf(arduino, '%d\n', doi); % send answer variable content to arduino
Hello Mr Walter Roberson, even i use the instruction that you told me but i doesn't work ! please help
Sorry, I do not have an arduino to test with.

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

Azrg
Azrg 2019년 10월 23일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

제품

질문:

2017년 2월 18일

댓글:

2019년 11월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by