Sending values from Matlab to arduino using serial communication

11 Ansichten (letzte 30 Tage)
Naseeb Gill
Naseeb Gill am 18 Feb. 2017
Kommentiert: Walter Roberson am 20 Nov. 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 Kommentare
Sravani Vanama
Sravani Vanama am 19 Nov. 2019
how to print the values send from matlab to arduino??
Walter Roberson
Walter Roberson am 20 Nov. 2019
There are a few possibilities for display:

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Walter Roberson
Walter Roberson am 21 Feb. 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 Kommentar
Naseeb Gill
Naseeb Gill am 21 Feb. 2017
Bearbeitet: Naseeb Gill am 21 Feb. 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.

Melden Sie sich an, um zu kommentieren.


Rouis Jihene
Rouis Jihene am 31 Mai 2017
Hello, i tried the code but i don't get the value (doi) in arduino ! why? please help
  3 Kommentare
Rouis Jihene
Rouis Jihene am 4 Jun. 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 am 4 Jun. 2017
Sorry, I do not have an arduino to test with.

Melden Sie sich an, um zu kommentieren.


Azrg
Azrg am 23 Okt. 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.

Kategorien

Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by