-
Notifications
You must be signed in to change notification settings - Fork 1
/
sensor1.m
66 lines (60 loc) · 1.82 KB
/
sensor1.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
%
% open a serial port, read data from it and plot it
%
% data format:
% 543054 2048
% where 543054 is time (microseconds)
% and 2048 is the value to be plotted
%
%
% open a serial port, on baudrate 115200, with a laaarge input buffer
mysensor = serial('/dev/ttyUSB0')
mysensor.BaudRate = 115200
mysensor.InputBufferSize = 2^15
% just to be sure, close and open it (crude fix)
fclose(mysensor)
fopen(mysensor)
% make a new plot
figure(1)
axis
hold on
% here all the read data will be kept for later processing
data_storage = [];
% you can use a finite loop here:
% for k=1:20000
while(1)
% if there is some data waiting in the buffer
if mysensor.BytesAvailable > 0
% read all the available data as raw values
rawdata = fread(mysensor, mysensor.BytesAvailable);
% arduino sends it as text, so make a string out of it:
% (by default the char(rawdata) is vertical, so transpose it)
datatext = char(rawdata)';
% the data can be broken, so find the first newline you can:
for i=1:length(datatext)
if double(datatext(i))==10
i = i+1;
break
end
end
% and from the end do the same:
for j=length(datatext):-1:1
if double(datatext(j))==13
j = j-1;
break
end
end
% now that the datatext(i:j) is a nice twocolumn text full of values we can simply
% make a matrix out of it
val = str2num( datatext(i:j) );
% store it for later
data_storage = [ data_storage ; val(:,:) ];
% plot - time, values
plot( val(:,1), val(:,2),'.' )
% sync the plot, you can use pause(0.01) if you want here
drawnow
end
end
% if the loop is a finite one:
% fclose(czujnik)
% if not, close the port after ctrl-c