Advertisement
Guest User

Untitled

a guest
Nov 29th, 2014
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.09 KB | None | 0 0
  1. close all;
  2. clear all;
  3. clc;
  4. delete(instrfindall);  %close all opened serial ports that are not in use
  5.  
  6. s1 = serial('COM3');   %define serial port
  7. s1.BaudRate=9600;      %define baud rate
  8. fopen(s1);             %open serial port
  9.  
  10. plot(0,0)              %empty plot
  11. hold on                %
  12. grid on                %
  13. xlabel('Time, [s]', 'FontName', 'Times New Roman', 'FontSize', 14);
  14. ylabel('Distance [cm]','FontName', 'Times New Roman', 'FontSize', 14);
  15. title('Ultrasonic Ranging Module HC-SR04, taking readings every 0.1 second', 'FontName', 'Times New Roman', 'FontSize', 14)
  16.  
  17. data = [];             %empty vectors for data storage
  18. ix = [];               %
  19.  
  20. %distance = 0;
  21.  
  22. flushinput(s1);        %clear any stored data on the serial port
  23.  
  24. %30 minutes, ploting data every half second
  25. %writing the received data into the empty vector
  26. %plotting the data in realtime
  27.  
  28. for i= 1 : 3600*5
  29.     data(i) = str2double(fscanf(s1));
  30.     ix(i) = i/10;
  31.     plot(ix, data, 'r', 'LineWidth', 1.5)
  32.     axis([i/10-8.5 i/10+1.5 0 300])
  33.     pause(0.1)
  34. end;
  35.  
  36. fclose(s1);            %close serial port
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement