Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- close all;
- clear all;
- clc;
- delete(instrfindall); %close all opened serial ports that are not in use
- s1 = serial('COM3'); %define serial port
- s1.BaudRate=9600; %define baud rate
- fopen(s1); %open serial port
- plot(0,0) %empty plot
- hold on %
- grid on %
- xlabel('Time, [s]', 'FontName', 'Times New Roman', 'FontSize', 14);
- ylabel('Distance [cm]','FontName', 'Times New Roman', 'FontSize', 14);
- title('Ultrasonic Ranging Module HC-SR04, taking readings every 0.1 second', 'FontName', 'Times New Roman', 'FontSize', 14)
- data = []; %empty vectors for data storage
- ix = []; %
- %distance = 0;
- flushinput(s1); %clear any stored data on the serial port
- %30 minutes, ploting data every half second
- %writing the received data into the empty vector
- %plotting the data in realtime
- for i= 1 : 3600*5
- data(i) = str2double(fscanf(s1));
- ix(i) = i/10;
- plot(ix, data, 'r', 'LineWidth', 1.5)
- axis([i/10-8.5 i/10+1.5 0 300])
- pause(0.1)
- end;
- fclose(s1); %close serial port
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement