-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino.m
2330 lines (1809 loc) · 96.7 KB
/
arduino.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef arduino < handle
% This class defines an "arduino" object
% Giampiero Campa, Aug 2013, Copyright 2013 The MathWorks, Inc.
properties (SetAccess=private,GetAccess=private)
aser % Serial Connection
pins % Pin Status Vector
srvs % Servo Status Vector
mspd % DC Motors Speed Status
sspd % Stepper Motors Speed Status
encs % Encoders Status
sktc % Motor Server Running on the Arduino Board
end
properties (Hidden=true)
chks = false; % Checks serial connection before every operation
chkp = true; % Checks parameters before every operation
end
methods
% constructor, connects to the board and creates an arduino object
function a=arduino(comPort)
% check nargin
if nargin<1,
comPort='DEMO';
disp('Note: a DEMO connection will be created');
disp('Use a the com port, e.g. ''COM5'' as input argument to connect to the real board');
end
% check port
if ~ischar(comPort),
error('The input argument must be a string, e.g. ''COM8'' ');
end
% check if we are already connected
if isa(a.aser,'serial') && isvalid(a.aser) && strcmpi(get(a.aser,'Status'),'open'),
disp(['It looks like a board is already connected to port ' comPort ]);
disp('Delete the object to force disconnection');
disp('before attempting a connection to a different port.');
return;
end
% check whether serial port is currently used by MATLAB
if ~isempty(instrfind({'Port'},{comPort})),
disp(['The port ' comPort ' is already used by MATLAB']);
disp(['If you are sure that the board is connected to ' comPort]);
disp('then delete the object, execute:');
disp([' delete(instrfind({''Port''},{''' comPort '''}))']);
disp('to delete the port, disconnect the cable, reconnect it,');
disp('and then create a new arduino object');
error(['Port ' comPort ' already used by MATLAB']);
end
% define serial object
a.aser=serial(comPort,'BaudRate',115200);
% connection
if strcmpi(get(a.aser,'Port'),'DEMO'),
% handle demo mode
fprintf(1,'Demo mode connection .');
for i=1:6,
pause(1);
fprintf(1,'.');
end
fprintf(1,'\n');
% chk is equal to 4, (more general server running)
a.sktc=4;
else
% actual connection
% open port
try
fopen(a.aser);
catch ME,
disp(ME.message)
delete(a);
error(['Could not open port: ' comPort]);
end
% it takes several seconds before any operation could be attempted
fprintf(1,'Attempting connection .');
for i=1:12,
pause(1);
fprintf(1,'.');
end
fprintf(1,'\n');
% flush serial buffer before sending anything
flush(a);
% query sketch type
fwrite(a.aser,[57 57],'uchar');
a.sktc=fscanf(a.aser,'%d');
% exit if there was no answer
if isempty(a.sktc)
delete(a);
error('Connection unsuccessful, please make sure that the board is powered on, running a sketch provided with the package, and connected to the indicated serial port. You might also try to unplug and re-plug the USB cable before attempting a reconnection.');
end
end
% check returned value
if a.sktc==0,
disp('Basic Analog and Digital I/O (adio.pde) sketch detected !');
elseif a.sktc==1,
disp('Analog & Digital I/O + Encoders (adioe.pde) sketch detected !');
elseif a.sktc==2,
disp('Analog & Digital I/O + Encoders + Servos (adioes.pde) sketch detected !');
elseif a.sktc==3,
disp('Motor Shield V1 (plus adioes.pde functions) sketch detected !');
elseif a.sktc==4,
disp('Motor Shield V2 (plus adioes.pde functions) sketch detected !');
else
delete(a);
error('Unknown sketch. Please make sure that a sketch provided with the package is running on the board');
end
% set a.aser tag
a.aser.Tag='ok';
% initialize pin vector (-1 is unassigned, 0 is input, 1 is output)
a.pins=-1*ones(1,69);
% initialize servo vector (0 is detached, 1 is attached)
a.srvs=0*ones(1,69);
% initialize encoder vector (0 is detached, 1 is attached)
a.encs=0*ones(1,3);
% initialize motor vector (0 to 255 is the speed)
a.mspd=0*ones(1,4);
% initialize stepper vector (0 to 255 is the speed)
a.sspd=0*ones(1,2);
% notify successful installation
disp('Arduino successfully connected !');
end % arduino
% distructor, deletes the object
function delete(a)
% Use delete(a) or a.delete to delete the arduino object
% if it is a serial, valid and open then close it
if isa(a.aser,'serial') && isvalid(a.aser) && strcmpi(get(a.aser,'Status'),'open'),
if ~isempty(a.aser.Tag),
try
% trying to leave it in a known unharmful state
for i=2:69,
a.pinMode(i,'output');
a.digitalWrite(i,0);
a.pinMode(i,'input');
end
catch ME
% disp but proceed anyway
disp(ME.message);
disp('Proceeding to deletion anyway');
end
end
fclose(a.aser);
end
% if it's an object delete it
if isobject(a.aser),
delete(a.aser);
end
end % delete
% disp, displays the object
function disp(a)
% disp(a) or a.disp, displays the arduino object properties
% The first and only argument is the arduino object, there is no
% output, but the basic information and properties of the arduino
% object are displayed on the screen.
% This function is called when just the name of the arduino object
% is typed on the command line, followed by enter. The command
% str=evalc('a.disp'), (or str=evalc('a')), can be used to capture
% the output in the string 'str'.
if isvalid(a),
if isa(a.aser,'serial') && isvalid(a.aser),
disp(['<a href="matlab:help arduino">arduino</a> object connected to ' a.aser.port ' port']);
if a.sktc==4,
disp('Motor Shield sketch V2 (plus adioes.pde functions) running on the board');
disp(' ');
a.servoStatus
disp(' ');
disp('Servo Methods: <a href="matlab:help servoStatus">servoStatus</a> <a href="matlab:help servoAttach">servoAttach</a> <a href="matlab:help servoDetach">servoDetach</a> <a href="matlab:help servoRead">servoRead</a> <a href="matlab:help servoWrite">servoWrite</a>');
disp(' ');
a.encoderStatus
disp(' ');
disp('Encoder Methods: <a href="matlab:help encoderStatus">encoderStatus</a> <a href="matlab:help encoderAttach">encoderAttach</a> <a href="matlab:help encoderDetach">encoderDetach</a> <a href="matlab:help encoderRead">encoderRead</a> <a href="matlab:help encoderReset">encoderReset</a>');
disp(' ');
a.motorSpeed
a.stepperSpeed
disp(' ');
disp('DC Motor and Steppers Methods: <a href="matlab:help motorSpeed">motorSpeed</a> <a href="matlab:help motorRun">motorRun</a> <a href="matlab:help stepperSpeed">stepperSpeed</a> <a href="matlab:help stepperStep">stepperStep</a>');
disp(' ');
disp('Serial port and other Methods: <a href="matlab:help serial">serial</a> <a href="matlab:help flush">flush</a> <a href="matlab:help roundTrip">roundTrip</a>');
elseif a.sktc==3,
disp('Motor Shield sketch V1 (plus adioes.pde functions) running on the board');
disp(' ');
a.servoStatus
disp(' ');
disp('Servo Methods: <a href="matlab:help servoStatus">servoStatus</a> <a href="matlab:help servoAttach">servoAttach</a> <a href="matlab:help servoDetach">servoDetach</a> <a href="matlab:help servoRead">servoRead</a> <a href="matlab:help servoWrite">servoWrite</a>');
disp(' ');
a.encoderStatus
disp(' ');
disp('Encoder Methods: <a href="matlab:help encoderStatus">encoderStatus</a> <a href="matlab:help encoderAttach">encoderAttach</a> <a href="matlab:help encoderDetach">encoderDetach</a> <a href="matlab:help encoderRead">encoderRead</a> <a href="matlab:help encoderReset">encoderReset</a>');
disp(' ');
a.motorSpeed
a.stepperSpeed
disp(' ');
disp('DC Motor and Steppers Methods: <a href="matlab:help motorSpeed">motorSpeed</a> <a href="matlab:help motorRun">motorRun</a> <a href="matlab:help stepperSpeed">stepperSpeed</a> <a href="matlab:help stepperStep">stepperStep</a>');
disp(' ');
disp('Serial port and other Methods: <a href="matlab:help serial">serial</a> <a href="matlab:help flush">flush</a> <a href="matlab:help roundTrip">roundTrip</a>');
elseif a.sktc==2,
disp('Analog & Digital I/O + Encoders + Servos (adioes.pde) sketch running on the board');
disp(' ');
a.pinMode
disp(' ');
disp('Pin IO Methods: <a href="matlab:help pinMode">pinMode</a> <a href="matlab:help digitalRead">digitalRead</a> <a href="matlab:help digitalWrite">digitalWrite</a> <a href="matlab:help analogRead">analogRead</a> <a href="matlab:help analogWrite">analogWrite</a> <a href="matlab:help analogReference">analogReference</a>');
disp(' ');
a.servoStatus
disp(' ');
disp('Servo Methods: <a href="matlab:help servoStatus">servoStatus</a> <a href="matlab:help servoAttach">servoAttach</a> <a href="matlab:help servoDetach">servoDetach</a> <a href="matlab:help servoRead">servoRead</a> <a href="matlab:help servoWrite">servoWrite</a>');
disp(' ');
a.encoderStatus
disp(' ');
disp('Encoder Methods: <a href="matlab:help encoderStatus">encoderStatus</a> <a href="matlab:help encoderAttach">encoderAttach</a> <a href="matlab:help encoderDetach">encoderDetach</a> <a href="matlab:help encoderRead">encoderRead</a> <a href="matlab:help encoderReset">encoderReset</a>');
disp(' ');
disp('Serial port and other Methods: <a href="matlab:help serial">serial</a> <a href="matlab:help flush">flush</a> <a href="matlab:help roundTrip">roundTrip</a>');
elseif a.sktc==1,
disp('Analog & Digital I/O + Encoders (adioe.pde) sketch running on the board');
disp(' ');
a.pinMode
disp(' ');
disp('Pin IO Methods: <a href="matlab:help pinMode">pinMode</a> <a href="matlab:help digitalRead">digitalRead</a> <a href="matlab:help digitalWrite">digitalWrite</a> <a href="matlab:help analogRead">analogRead</a> <a href="matlab:help analogWrite">analogWrite</a> <a href="matlab:help analogReference">analogReference</a>');
disp(' ');
a.encoderStatus
disp(' ');
disp('Encoder Methods: <a href="matlab:help encoderStatus">encoderStatus</a> <a href="matlab:help encoderAttach">encoderAttach</a> <a href="matlab:help encoderDetach">encoderDetach</a> <a href="matlab:help encoderRead">encoderRead</a> <a href="matlab:help encoderReset">encoderReset</a>');
disp(' ');
disp('Serial port and other Methods: <a href="matlab:help serial">serial</a> <a href="matlab:help flush">flush</a> <a href="matlab:help roundTrip">roundTrip</a>');
else
disp('Basic Analog & Digital I/O sketch (adio.pde) running on the board');
disp(' ');
a.pinMode
disp(' ');
disp('Pin IO Methods: <a href="matlab:help pinMode">pinMode</a> <a href="matlab:help digitalRead">digitalRead</a> <a href="matlab:help digitalWrite">digitalWrite</a> <a href="matlab:help analogRead">analogRead</a> <a href="matlab:help analogWrite">analogWrite</a> <a href="matlab:help analogReference">analogReference</a>');
disp(' ');
disp('Serial port and other Methods: <a href="matlab:help serial">serial</a> <a href="matlab:help flush">flush</a> <a href="matlab:help roundTrip">roundTrip</a>');
end
disp(' ');
else
disp('<a href="matlab:help arduino">arduino</a> object connected to an invalid serial port');
disp('Please delete the arduino object');
disp(' ');
end
else
disp('Invalid <a href="matlab:help arduino">arduino</a> object');
disp('Please clear the object and instantiate another one');
disp(' ');
end
end
% serial, returns the serial port
function str=serial(a)
% serial(a) (or a.serial), returns the name of the serial port
% The first and only argument is the arduino object, the output
% is a string containing the name of the serial port to which
% the arduino board is connected (e.g. 'COM9', 'DEMO', or
% '/dev/ttyS101'). The string 'Invalid' is returned if
% the serial port is invalid
if isvalid(a.aser),
str=a.aser.port;
else
str='Invalid';
end
end % serial
% flush, clears the pc's serial port buffer
function val=flush(a)
% val=flush(a) (or val=a.flush) reads all the bytes available
% (if any) in the computer's serial port buffer, therefore
% clearing said buffer.
% The first and only argument is the arduino object, the
% output is a vector of bytes that were still in the buffer.
% The value '-1' is returned if the buffer was already empty.
val=-1;
if a.aser.BytesAvailable>0,
val=fread(a.aser,a.aser.BytesAvailable);
end
end % flush
% pin mode, changes pin mode
function pinMode(a,pin,str)
% pinMode(a,pin,str); reads or sets the I/O mode of a digital pin.
% The first argument, a, is the arduino object.
% The second argument, pin, is the number of the digital pin (2 to 69).
% The third argument, str, is a string that can be 'input' or 'output',
% Called as pinMode(a,pin) it returns the mode of the digital pin,
% called as pinMode(a), it prints the mode of all the digital pins.
% Note that in the Arduino Uno board the digital pins from 0 to 13
% are located on the upper right part of the board,
% while the digital pins from 14 to 19 are better known as
% "analog input" pins (in fact are often referred to with an
% analog pin number from 0 to 5) and are located in the lower
% right corner of the board.
%
% Examples:
% pinMode(a,11,'output') % sets digital pin #11 as output
% pinMode(a,10,'input') % sets digital pin #10 as input
% a.pinMode(10,'input') % same as pinMode(a,10,'input')
% val=pinMode(a,10); % returns the status of digital pin #10
% pinMode(a,5); % prints the status of digital pin #5
% pinMode(a); % prints the status of all pins
%
%%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check arguments if a.chkp is true
if a.chkp,
% check nargin
if nargin>3,
error('This function cannot have more than 3 arguments, object, pin and str');
end
% if pin argument is there check it
if nargin>1,
errstr=arduino.checknum(pin,'pin number',2:69);
if ~isempty(errstr), error(errstr); end
end
% if str argument is there check it
if nargin>2,
errstr=arduino.checkstr(str,'pin mode',{'input','output'});
if ~isempty(errstr), error(errstr); end
end
end
% perform the requested action
if nargin==3,
% check a.aser for validity if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'valid');
if ~isempty(errstr), error(errstr); end
end
%%%%%%%%%%%%%%%%%%%%%%%%% CHANGE PIN MODE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% assign value
if lower(str(1))=='o', val=1; else val=0; end
if strcmpi(get(a.aser,'Port'),'DEMO'),
% handle demo mode here
% minimum digital output delay
pause(0.0014);
else
% do the actual action here
% check a.aser for openness if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'open');
if ~isempty(errstr), error(errstr); end
end
% send mode, pin and value
fwrite(a.aser,[48 97+pin 48+val],'uchar');
end
% silently detach servos on the correspinding pin
tmp=a.chkp;
a.chkp=0;
a.servoDetach(pin);
a.chkp=tmp;
% store 0 for input and 1 for output
a.pins(pin)=val;
elseif nargin==2,
% print pin mode for the requested pin
mode={'UNASSIGNED','set as INPUT','set as OUTPUT'};
disp(['Digital Pin ' num2str(pin) ' is currently ' mode{2+a.pins(pin)}]);
else
% print pin mode for each pin
mode={'UNASSIGNED','set as INPUT','set as OUTPUT'};
for i=2:69;
disp(['Digital Pin ' num2str(i,'%02d') ' is currently ' mode{2+a.pins(i)}]);
end
end
end % pinmode
% digital read
function val=digitalRead(a,pin)
% val=digitalRead(a,pin); performs digital input on a given arduino pin.
% The first argument, a, is the arduino object.
% The second argument, pin, is the number of the digital pin (2 to 69)
% where the digital input needs to be performed. On the Arduino Uno
% the digital pins from 0 to 13 are located on the upper right part
% while the digital pins from 14 to 19 are better known as "analog input"
% pins and are located in the lower right corner of the board
% (in fact are often referred to as "analog pins from 0 to 5").
%
% Examples:
% val=digitalRead(a,4); % reads pin #4
% val=a.digitalRead(4); % just as above (reads pin #4)
%
%%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check arguments if a.chkp is true
if a.chkp,
% check nargin
if nargin~=2,
error('Function must have the "pin" argument');
end
% check pin
errstr=arduino.checknum(pin,'pin number',2:69);
if ~isempty(errstr), error(errstr); end
end
% check a.aser for validity if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'valid');
if ~isempty(errstr), error(errstr); end
end
%%%%%%%%%%%%%%%%%%%%%%%%% PERFORM DIGITAL INPUT %%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmpi(get(a.aser,'Port'),'DEMO'),
% handle demo mode
% minimum digital input delay
pause(0.0074);
% output 0 or 1 randomly
val=round(rand);
else
% check a.aser for openness if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'open');
if ~isempty(errstr), error(errstr); end
end
% send mode and pin
fwrite(a.aser,[49 97+pin],'uchar');
% get value
val=fscanf(a.aser,'%d');
end
end % digitalread
% digital write
function digitalWrite(a,pin,val)
% digitalWrite(a,pin,val); performs digital output on a given pin.
% The first argument, a, is the arduino object.
% The second argument, pin, is the number of the digital pin
% (2 to 69) where the digital output value needs to be written.
% The third argument, val, is the output value (either 0 or 1).
% On the Arduino Uno the digital pins from 0 to 13 are located
% on the upper right part of the board, while the digital pins
% from 14 to 19 are better known as "analog input" pins and are
% located in the lower right corner of the board (in fact are
% often referred to as "analog pins from 0 to 5").
%
% Examples:
% digitalWrite(a,13,1); % sets pin #13 high
% digitalWrite(a,13,0); % sets pin #13 low
% a.digitalWrite(13,0); % just as above (sets pin #13 to low)
%
%%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check arguments if a.chkp is true
if a.chkp,
% check nargin
if nargin~=3,
error('Function must have the "pin" and "val" arguments');
end
% check pin
errstr=arduino.checknum(pin,'pin number',2:69);
if ~isempty(errstr), error(errstr); end
% check val
errstr=arduino.checknum(val,'value',0:1);
if ~isempty(errstr), error(errstr); end
% get object name
if isempty(inputname(1)), name='object'; else name=inputname(1); end
% pin should be configured as output
if a.pins(pin)~=1,
warning('MATLAB:Arduino:digitalWrite',['If digital pin ' num2str(pin) ' is set as input, digital output takes place only after using ' name' '.pinMode(' num2str(pin) ',''output''); ']);
end
end
% check a.aser for validity if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'valid');
if ~isempty(errstr), error(errstr); end
end
%%%%%%%%%%%%%%%%%%%%%%%%% PERFORM DIGITAL OUTPUT %%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmpi(get(a.aser,'Port'),'DEMO'),
% handle demo mode
% minimum digital output delay
pause(0.0014);
else
% check a.aser for openness if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'open');
if ~isempty(errstr), error(errstr); end
end
% send mode, pin and value
fwrite(a.aser,[50 97+pin 48+val],'uchar');
end
end % digitalwrite
% analog read
function val=analogRead(a,pin)
% val=analogRead(a,pin); Performs analog input on a given arduino pin.
% The first argument, a, is the arduino object. The second argument,
% pin, is the number of the analog input pin (0 to 15) from which the
% analog value needs to be read. The returned value, val, ranges from
% 0 to 1023, with 0 corresponding to an input voltage of 0 volts,
% and 1023 to a reference value that is typically 5 volts (this voltage can
% be set up by the analogReference function). Therefore, assuming a range
% from 0 to 5 V the resolution is .0049 volts (4.9 mV) per unit.
% Note that in the Arduino Uno board the analog input pins 0 to 5 are also
% the digital pins from 14 to 19, and are located on the lower right corner.
% Specifically, analog input pin 0 corresponds to digital pin 14, and analog
% input pin 5 corresponds to digital pin 19. Performing analog input does
% not affect the digital state (high, low, digital input) of the pin.
%
% Examples:
% val=analogRead(a,0); % reads analog input pin # 0
% val=a.analogRead(0); % just as above, reads analog input pin # 0
%
%%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check arguments if a.chkp is true
if a.chkp,
% check nargin
if nargin~=2,
error('Function must have the "pin" argument');
end
% check pin
errstr=arduino.checknum(pin,'analog input pin number',0:15);
if ~isempty(errstr), error(errstr); end
end
% check a.aser for validity if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'valid');
if ~isempty(errstr), error(errstr); end
end
%%%%%%%%%%%%%%%%%%%%%%%%% PERFORM ANALOG INPUT %%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmpi(get(a.aser,'Port'),'DEMO'),
% handle demo mode
% minimum analog input delay
pause(0.0074);
% output a random value between 0 and 1023
val=round(1023*rand);
else
% check a.aser for openness if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'open');
if ~isempty(errstr), error(errstr); end
end
% send mode and pin
fwrite(a.aser,[51 97+pin],'uchar');
% get value
val=fscanf(a.aser,'%d');
end
end % analogread
% function analog write
function analogWrite(a,pin,val)
% analogWrite(a,pin,val); Performs analog output on a given arduino pin.
% The first argument, a, is the arduino object. The second argument,
% pin, is the number of the DIGITAL pin where the analog (PWM) output
% needs to be performed. Allowed pins for AO on the Mega board
% are 2 to 13 and 44 to 46, (3,5,6,9,10,11 on the Uno board).
% The second argument, val, is the value from 0 to 255 for the level of
% analog output. Note that the digital pins from 0 to 13 are located on the
% upper right part of the board.
%
% Examples:
% analogWrite(a,11,90); % sets pin #11 to 90/255
% a.analogWrite(3,10); % sets pin #3 to 10/255
%
%%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check arguments if a.chkp is true
if a.chkp,
% check nargin
if nargin~=3,
error('Function must have the "pin" and "val" arguments');
end
% check pin
errstr=arduino.checknum(pin,'pwm pin number',[2:13 44:46]);
if ~isempty(errstr), error(errstr); end
% check val
errstr=arduino.checknum(val,'analog output level',0:255);
if ~isempty(errstr), error(errstr); end
end
% check a.aser for validity if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'valid');
if ~isempty(errstr), error(errstr); end
end
%%%%%%%%%%%%%%%%%%%%%%%%% PERFORM ANALOG OUTPUT %%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmpi(get(a.aser,'Port'),'DEMO'),
% handle demo mode
% minimum analog output delay
pause(0.0014);
else
% check a.aser for openness if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'open');
if ~isempty(errstr), error(errstr); end
end
% send mode, pin and value
fwrite(a.aser,[52 97+pin val],'uchar');
end
end % analogwrite
% function analog reference
function analogReference(a,str)
% analogReference(a,str); Changes voltage reference on analog input pins
% The first argument, a, is the arduino object. The second argument,
% str, is one of these strings: 'default', 'internal' or 'external'.
% This sets the reference voltage used at the top of the input ranges.
%
% Examples:
% analogReference(a,'default'); % sets default reference
% analogReference(a,'internal'); % sets internal reference
% analogReference(a,'external'); % sets external reference
% a.analogReference('external'); % just as above (sets external reference)
%
%%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check arguments if a.chkp is true
if a.chkp,
% check nargin
if nargin~=2,
error('Function must have the "reference" argument');
end
% check val
errstr=arduino.checkstr(str,'reference',{'default','internal','external'});
if ~isempty(errstr), error(errstr); end
end
% check a.aser for validity if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'valid');
if ~isempty(errstr), error(errstr); end
end
%%%%%%%%%%%%%%%%%%%% CHANGE ANALOG INPUT REFERENCE %%%%%%%%%%%%%%%%%%%%%%%%%
if strcmpi(get(a.aser,'Port'),'DEMO'),
% handle demo mode
% minimum analog output delay
pause(0.0014);
else
% check a.aser for openness if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'open');
if ~isempty(errstr), error(errstr); end
end
if lower(str(1))=='e', num=2;
elseif lower(str(1))=='i', num=1;
else num=0;
end
% send mode, pin and value
fwrite(a.aser,[82 48+num],'uchar');
end
end % analogreference
% servo attach
function servoAttach(a,pin)
% servoAttach(a,pin); or a.servoAttach(pin); attaches a servo to a pin.
% The first argument, a, is the arduino object. The second argument,
% pin, is the number of the pwm pin where the servo must be attached
% (a servo has three pins, a central red one which goes to +5V,
% a black or brown one which goes to ground, and a white or orange one
% which must be connected to an analog output (PWM) pin on the Arduino).
%
% Earlier versions of both Arduino boards and Arduino IO package supported
% only pwm pins 9 and 10, now up to 12 different pins are supported on most
% Arduino boards, and 48 on the Mega. Note that on boards other than the Mega,
% use of the servos disables analogWrite() functionality on pins 9 and 10,
% whether or not there is a Servo on those pins. On the Mega, up to 12 servos
% can be used without interfering with PWM; use of 12 to 23 servos will
% disable PWM on pins 11 and 12.
%
% Note that motor shields typically have their own servo connectors,
% For example the Adafruit Motor Shield V1 has 2 servo connector on
% its top left corner, the one labeled "SER1" uses pin #10,
% while the one labeled "SERVO_2" uses the pin #9.
%
% Examples:
% servoAttach(a,10); % attaches servo on pin #10
% a.servoAttach(10); % same as above (attaches servo on pin #10)
%
%%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check arguments if a.chkp is true
if a.chkp,
% check nargin
if nargin~=2,
error('Function must have the "pin" argument');
end
% check pin
errstr=arduino.checknum(pin,'servo number',2:69);
if ~isempty(errstr), error(errstr); end
end
% check a.aser for validity if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'valid');
if ~isempty(errstr), error(errstr); end
end
% warning if sketch is inadequate and a.chkp is true
if a.chkp && a.sktc<2
disp('Warning: the sketch running on the Arduino does not support servos');
disp('No operation will be performed on the Arduino board');
end
%%%%%%%%%%%%%%%%%%%%%%%%% ATTACH SERVO %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmpi(get(a.aser,'Port'),'DEMO') || a.sktc<2,
% handle demo mode
% minimum digital output delay
pause(0.0014);
else
% check a.aser for openness if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'open');
if ~isempty(errstr), error(errstr); end
end
% send mode, pin and value (1 for attach)
fwrite(a.aser,[54 97+pin 48+1],'uchar');
end
% store the servo status
a.srvs(pin)=1;
% update pin status to unassigned
a.pins(pin)=-1;
end % servoattach
% servo detach
function servoDetach(a,pin)
% servoDetach(a,pin); detaches a servo from its corresponding pwm pin.
% The first argument, a, is the arduino object. The second argument,
% pin, is the number of the pin where the servo is attached
% Earlier versions supported only pins 9 and 10, now up to 12 different
% pins are supported on most Arduino boards, and 48 on the Mega.
% Note that on boards other than the Mega, using the servos disables
% analogWrite() functionality on pins 9 and 10, whether or not there
% is a Servo on those pins. On the Mega, up to 12 servos can be used
% without interfering with PWM; use of 12 to 23 servos will
% disable PWM on pins 11 and 12.
%
% Examples:
% servoDetach(a,10); % detaches a servo attached on pin 10
% a.servoDetach(10); % detaches a servo attached on pin 10
%
%%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check arguments if a.chkp is true
if a.chkp,
% check nargin
if nargin~=2,
error('Function must have the "pin" argument');
end
% check servo number
errstr=arduino.checknum(pin,'servo number',2:69);
if ~isempty(errstr), error(errstr); end
end
% check a.aser for validity if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'valid');
if ~isempty(errstr), error(errstr); end
end
% warning if sketch is inadequate and a.chkp is true
if a.chkp && a.sktc<2
disp('Warning: the sketch running on the Arduino does not support servos');
disp('No operation will be performed on the Arduino board');
end
%%%%%%%%%%%%%%%%%%%%%%%%% DETACH SERVO %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmpi(get(a.aser,'Port'),'DEMO') || a.sktc<2,
% handle demo mode
% minimum digital output delay
pause(0.0014);
else
% check a.aser for openness if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'open');
if ~isempty(errstr), error(errstr); end
end
% send mode, pin and value (0 for detach)
fwrite(a.aser,[54 97+pin 48+0],'uchar');
end
a.srvs(pin)=0;
end % servodetach
% servo status
function val=servoStatus(a,pin)
% servoStatus(a,pin); Reads the status of a servo (attached/detached).
% The first argument, a, is the arduino object. The second argument,
% pin, is the number of the pin where the servo is attached
% Earlier versions supported only pins 9 and 10, now up to 12 different
% pins are supported on most Arduino boards, and 48 on the Mega.
% The returned value is either 1 (servo attached) or 0 (servo detached).
% Called without output arguments, the function prints a string specifying
% the status of the servo. Called without input arguments, the function
% either returns the status vector or prints the status of each servo.
%
% Examples:
% val=servoStatus(a,10); % return the status of servo on pin #10
% servoStatus(a,10); % prints the status of servo on pin #10
% servoStatus(a); % prints the status of all servos
% a.servoStatus; % prints the status of all servos
%
%%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check nargin if a.chkp is true
if a.chkp,
if nargin>2,
error('Function cannot have more than one argument (servo number) beyond the object name');
end
end
% with no arguments calls itself recursively for all servos
if nargin==1,
if nargout>0,
val=zeros(69,1);
for i=2:69,
val(i)=a.servoStatus(i);
end
return
else
for i=2:69,
a.servoStatus(i);
end
return
end
end
% check servo number if a.chkp is true
if a.chkp,
errstr=arduino.checknum(pin,'servo number',2:69);
if ~isempty(errstr), error(errstr); end
end
% check a.aser for validity if a.chks is true
if a.chks,
errstr=arduino.checkser(a.aser,'valid');
if ~isempty(errstr), error(errstr); end
end
% warning if sketch is inadequate and a.chkp is true
if a.chkp && a.sktc<2
disp('Warning: the sketch running on the Arduino does not support servos');
disp('No operation will be performed on the Arduino board');
end
%%%%%%%%%%%%%%%%%%%%%%%%% ASK SERVO STATUS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmpi(get(a.aser,'Port'),'DEMO') || a.sktc<2,
% handle demo mode
% minimum digital input delay