Guest User

Untitled

a guest
Mar 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. % --- Executes on button press in read_signal.
  2. function read_signal_Callback(hObject, eventdata, handles)
  3. % hObject handle to read_signal (see GCBO)
  4. % eventdata reserved - to be defined in a future version of MATLAB
  5. % handles structure with handles and user data (see GUIDATA)
  6. cla reset
  7.  
  8. fs=1;
  9.  
  10. % ROUTINE FOR REAL TIME READING
  11. % create arduino object
  12. delete(instrfind)
  13. a = serial('COM5'); %define serial port
  14. a.BaudRate=115200; %define baud rate
  15.  
  16. %open serial port
  17. fopen(a);
  18.  
  19. k = 0; %index
  20. v = 0; %voltage
  21. t = 0; %time
  22.  
  23. axes(handles.axes1)
  24. plotGraph=plot(t,v);
  25. xlabel ('Time (s)'), ylabel('Voltage');
  26. axis([0 30 0 5]);
  27.  
  28. scrollWidth=30; %window width
  29. delay=1/fs; %distance between two samples
  30.  
  31. tic % Start timer
  32. while(1)
  33. threshold_im=str2double(get(handles.threshold,'String'));
  34. time_im= str2double(get(handles.time,'String'));
  35.  
  36. k=k+1;
  37. v_read=fscanf(a,'%s'); %read arduino
  38.  
  39. t(k) = toc; %Extract Elapsed Time
  40. v(k)=str2double(v_read);
  41. th_y(k)=threshold_im;
  42. date=clock;
  43.  
  44. A=[date v(k) time_im threshold_im];%A is vector with date (minute resolution), voltage amplitude (V), time over threshold imposed and threshold
  45.  
  46. % Writing the signal value with data attached in a file
  47. fileID=fopen('pastsignal.txt','a');
  48. 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));
  49. fclose(fileID);
  50.  
  51. if(scrollWidth > 0)
  52. plotGraph=plot(t(t > t(k)-scrollWidth),v(t > t(k)-scrollWidth),'b'); hold on
  53. plot(t(t > t(k)-scrollWidth),th_y(t > t(k)-scrollWidth),'r')
  54. axis([t(k)-scrollWidth t(k) 0 5]);
  55. else
  56. plotGraph=plot(t,v,'b'); hold on
  57. plot(t,th_y,'r')
  58. axis([0 t(k) 0 5]);
  59. end
  60. %Allow MATLAB to Update Plot
  61. pause(delay);
  62.  
  63. end
  64. % close the serial port!
  65. fclose(a);
Add Comment
Please, Sign In to add comment