Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % --- Executes on button press in read_signal.
- function read_signal_Callback(hObject, eventdata, handles)
- % hObject handle to read_signal (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- cla reset
- fs=1;
- % ROUTINE FOR REAL TIME READING
- % create arduino object
- delete(instrfind)
- a = serial('COM5'); %define serial port
- a.BaudRate=115200; %define baud rate
- %open serial port
- fopen(a);
- k = 0; %index
- v = 0; %voltage
- t = 0; %time
- axes(handles.axes1)
- plotGraph=plot(t,v);
- xlabel ('Time (s)'), ylabel('Voltage');
- axis([0 30 0 5]);
- scrollWidth=30; %window width
- delay=1/fs; %distance between two samples
- tic % Start timer
- while(1)
- threshold_im=str2double(get(handles.threshold,'String'));
- time_im= str2double(get(handles.time,'String'));
- k=k+1;
- v_read=fscanf(a,'%s'); %read arduino
- t(k) = toc; %Extract Elapsed Time
- v(k)=str2double(v_read);
- th_y(k)=threshold_im;
- date=clock;
- A=[date v(k) time_im threshold_im];%A is vector with date (minute resolution), voltage amplitude (V), time over threshold imposed and threshold
- % Writing the signal value with data attached in a file
- fileID=fopen('pastsignal.txt','a');
- fprintf(fileID,'n%.0f %.0f %.0f %.0f %.0f %.0f %.2f %.2f %.2frn', A(1),A(2),A(3),A(4),A(5),A(6),A(7),A(8),A(9));
- fclose(fileID);
- if(scrollWidth > 0)
- plotGraph=plot(t(t > t(k)-scrollWidth),v(t > t(k)-scrollWidth),'b'); hold on
- plot(t(t > t(k)-scrollWidth),th_y(t > t(k)-scrollWidth),'r')
- axis([t(k)-scrollWidth t(k) 0 5]);
- else
- plotGraph=plot(t,v,'b'); hold on
- plot(t,th_y,'r')
- axis([0 t(k) 0 5]);
- end
- %Allow MATLAB to Update Plot
- pause(delay);
- end
- % close the serial port!
- fclose(a);
Add Comment
Please, Sign In to add comment