-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatlabToArduino.m
63 lines (52 loc) · 2.78 KB
/
MatlabToArduino.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
%the script uses fopen and fclose to connect to arduino
%if you manually exit with ctrl-c, make sure you fclose(s), or you will
%have to start matlab over again
%input motor values here, also edit COM port for the arduino
% make sure they are the same length
pm1 = [25,25,25,25,25,25,25,25,25,25,25,25,26,26,26,26,26,27,27,27,28,28,29,29,30,30,31,31,32,32,33,34,35,35,36,37,38,39,39,40,41,42,43,44,45,46,48,49,50,51,52,53,55,56,57,59,60,61,63,64,65,67,68,70,71,72,74,75,77,78,79,81,82,84,85,86,88,89,90,92,93,94,96,97,98,99,100,101,103,104,105,106,107,107,108,109,110,111,111,112,113,113,114,114,115,115,116,116,116,116,116,117,117,117,117,117,116,116,116,116,115,115,115,114,114,113,113,112,111,111,110,109,108,107,106,105,104,103,102,101,100,99,98,97,95,94,93,91,90,89,87,86,85,83,82,80,79,78,76,75,73,72,70,69,68,66,65,63,62,61,59,58,57,55,54,53,52,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,36,35,34,33,33,32,31,31,30,30,29,29,28,28,27,27,27,26,26,26,26,26,25,25,25,25,25,25,25,25,25,25,25,25];
pm2 = [90,90,90,90,90,90,90,90,91,91,91,91,92,92,93,93,93,94,94,95,96,96,97,98,98,99,100,101,101,102,103,104,105,106,107,107,108,109,110,111,112,113,114,115,116,117,118,118,119,120,121,122,122,123,124,124,125,126,126,127,127,128,128,128,129,129,129,129,130,130,130,130,130,129,129,129,129,128,128,128,127,127,126,125,125,124,123,122,122,121,120,119,118,117,115,114,113,112,111,109,108,107,106,104,103,101,100,99,97,96,94,93,91,90,88,87,85,84,83,81,80,78,77,76,74,73,72,70,69,68,67,65,64,63,62,61,60,59,58,57,57,56,55,55,54,53,53,52,52,52,51,51,51,51,51,51,51,51,51,51,51,51,52,52,52,53,53,54,54,55,56,56,57,58,58,59,60,61,62,62,63,64,65,66,67,68,69,70,71,71,72,73,74,75,76,77,78,79,79,80,81,82,82,83,84,84,85,86,86,87,87,87,88,88,89,89,89,89,90,90,90,90,90,90,90,90];
s = serial('COM12','BaudRate',9600);
len = length(pm1)
pause on
fopen(s);
pause(2);
z = 0;
while(z == 0)
prompt = 'press t to transmit, m to move, or e to exit: ';
x = input(prompt,'s');
if(x == 'm')
fwrite(s,uint8(184)); %move command
pause(.5);
elseif (x == 'e')
z = 1;
elseif (x == 't')
transmit(s,pm1,pm2,len); %transmit function
end
end
fclose(s);
function a = transmit(s,pm1,pm2,len)
fwrite(s,uint8(181)); %transmit command
pause(.05);
fwrite(s,uint8(len)); %transmit lenght first
pause(.05);
for i = 1:len %tranmit motor 1
fwrite(s,uint8(pm1(1,i)));
if(i == len/2)
disp('25% done');
end
pause(.05);
end
disp('50% done');
fwrite(s,uint8(182)); %transmit motor 2 command
pause(.05);
for i = 1:len
fwrite(s,uint8(pm2(1,i)));
if(i == len/2)
disp('75% done');
end
pause(.05);
end
disp('100% done');
fwrite(s,uint8(183)); %tranmsit done command
pause(1);
end