You are on page 1of 3

function varargout = distance(varargin)

% DISTANCE MATLAB code for distance.fig


% DISTANCE, by itself, creates a new DISTANCE or raises the existing
% singleton*.
%
% H = DISTANCE returns the handle to a new DISTANCE or the handle to
% the existing singleton*.
%
% DISTANCE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DISTANCE.M with the given input arguments.
%
% DISTANCE('Property','Value',...) creates a new DISTANCE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before distance_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to distance_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help distance

% Last Modified by GUIDE v2.5 21-Nov-2017 03:50:34

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @distance_OpeningFcn, ...
'gui_OutputFcn', @distance_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

% --- Executes just before distance is made visible.


function distance_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to distance (see VARARGIN)

% Choose default command line output for distance


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);
% UIWAIT makes distance wait for user response (see UIRESUME)
% uiwait(handles.figure1);

delete(instrfind({'Port'},{'COM12'}))
clear a;
global a;
a = serial('COM12', 'BaudRate', 9600); % setup comport
%fopen(a); % Open comport

cla(handles.graph);
set(handles.ON_button,'BackgroundColor','green');
set(handles.OFF_button,'BackgroundColor','red');

% --- Outputs from this function are returned to the command line.
function varargout = distance_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure


varargout{1} = handles.output;
pinMode = configurePin(a,'D6');
configurePin(a,'D6','Unset');

% --- Executes on button press in ON_button.


function ON_button_Callback(hObject, eventdata, handles)
% hObject handle to ON_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global true a x y1_now
x=0;

fopen(a);

configurePin(a,'D6','DigitalInput');
pinMode = configurePin(a,'D6');

handles.stop_now = 0; %Create stop_now in the handles structure


guidata(hObject,handles); %Update the GUI data

while ~(handles.stop_now)
x=x+1;
data=fscanf(a);
[data1] = strtok(data);
y1(x)=str2num(data1);

disp_y1 = num2str(y1(x));
set(handles.distance,'string',disp_y1);

drawnow;
axes(handles.graph);
plot(y1,'b-','linewidth',2)
title('Distance');
xlabel('Time');
ylabel('Distance (cm)');
ylim([0 0.2])

pause(0.01);
handles = guidata(hObject);
end

% --- Executes on button press in OFF_button.


function OFF_button_Callback(hObject, eventdata, handles)
configurePin(a,'D6','Unset');
pinMode = configurePin(a,'D6')
% hObject handle to OFF_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cla(handles.graph);
set(handles.ON_button,'BackgroundColor','green');
set(handles.OFF_button,'BackgroundColor','red');
handles.stop_now = 1;
guidata(hObject, handles); % Update handles structure
snew = instrfind;fclose(snew)

You might also like