-
Notifications
You must be signed in to change notification settings - Fork 2
/
meetup-wk5.html
823 lines (713 loc) · 32.9 KB
/
meetup-wk5.html
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Arduino introduction and interfacing with Processing and SuperCollider</title>
<!-- 2016-11-09 Τετ 16:13 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="generator" content="Org-mode" />
<meta name="author" content="Georgios Diapoulis" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center; }
.todo { font-family: monospace; color: red; }
.done { color: green; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.right { margin-left: auto; margin-right: 0px; text-align: right; }
.left { margin-left: 0px; margin-right: auto; text-align: left; }
.center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.right { text-align: center; }
th.left { text-align: center; }
th.center { text-align: center; }
td.right { text-align: right; }
td.left { text-align: left; }
td.center { text-align: center; }
dt { font-weight: bold; }
.footpara:nth-child(2) { display: inline; }
.footpara { display: block; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<link rel="stylesheet" type="text/css"
href="http://sachachua.com/blog/wp-content/themes/sacha-v3/foundation/css/foundation.min.css"></link>
<link rel="stylesheet" type="text/css" href="http://sachachua.com/org-export.css"></link>
<link rel="stylesheet" type="text/css" href="http://sachachua.com/blog/wp-content/themes/sacha-v3/style.css"></link>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="preamble" class="status">
<a name="top" id="top"></a>
</div>
<div id="content">
<h1 class="title">Arduino introduction and interfacing with Processing and SuperCollider</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1. Arduino Basics</a>
<ul>
<li><a href="#sec-1-1">1.1. Arduino <b>important links</b></a></li>
<li><a href="#sec-1-2">1.2. Serial communication</a></li>
<li><a href="#sec-1-3">1.3. Analog and digital pins</a></li>
</ul>
</li>
<li><a href="#sec-2">2. Analog read</a>
<ul>
<li><a href="#sec-2-1">2.1. Analog read - PWM output</a></li>
<li><a href="#sec-2-2">2.2. Analog read - binary output</a></li>
</ul>
</li>
<li><a href="#sec-3">3. Motor control</a>
<ul>
<li><a href="#sec-3-1">3.1. Servo control using a potentiometer</a></li>
<li><a href="#sec-3-2">3.2. Stepper control</a></li>
</ul>
</li>
<li><a href="#sec-4">4. Arduino and Processing</a>
<ul>
<li><a href="#sec-4-1">4.1. Read integer number</a></li>
<li><a href="#sec-4-2">4.2. Analog input to serial</a></li>
</ul>
</li>
<li><a href="#sec-5">5. Arduino and SuperCollider</a>
<ul>
<li><a href="#sec-5-1">5.1. Analog write - Dimmer</a></li>
<li><a href="#sec-5-2">5.2. Digital write - GUI with buttons</a>
<ul>
<li><a href="#sec-5-2-1">5.2.1. Using port registers</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#sec-6">6. Appendix</a>
<ul>
<li><a href="#sec-6-1">6.1. Resistor color codes</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1"><span class="section-number-2">1</span> Arduino Basics</h2>
<div class="outline-text-2" id="text-1">
<p>
Similar to Processing language <a href="http://arduino.cc/">Arduino</a> has two main structures, a <code>void setup()</code> and a <code>void loop()</code>. In contrast to processing which uses <code>frameRate()</code> function to update the <code>void loop()</code> function, Arduino's "framerate" depends on the program that you have uploaded to your board (see online topic <a href="http://forum.arduino.cc/index.php?topic=42695.0">here</a>).
</p>
</div>
<div id="outline-container-sec-1-1" class="outline-3">
<h3 id="sec-1-1"><span class="section-number-3">1.1</span> Arduino <b>important links</b></h3>
<div class="outline-text-3" id="text-1-1">
<dl class="org-dl">
<dt> Language Reference </dt><dd><a href="https://www.arduino.cc/en/Reference/HomePage">https://www.arduino.cc/en/Reference/HomePage</a>
</dd>
<dt> Tutorial Foundations </dt><dd><a href="https://www.arduino.cc/en/Tutorial/Foundations">https://www.arduino.cc/en/Tutorial/Foundations</a>
</dd>
</dl>
</div>
</div>
<div id="outline-container-sec-1-2" class="outline-3">
<h3 id="sec-1-2"><span class="section-number-3">1.2</span> Serial communication</h3>
<div class="outline-text-3" id="text-1-2">
<p>
To initialize serial communication of an Arduino board via USB you have to define <code>Serial.begin()</code>, which takes a single argument in bits per second (or baud) that represents how fast your computer sends and receives data from an arduino board.
</p>
<p>
Serial communication involves pins 0 and 1, also known as TX0 and RX0 for transmitter and receiver respectively. There are two on-board LEDs TX, RX are activated when the board sends or receives serial data. TX and RX are below the on-board LED (L) which is attached to pin 13.
</p>
<div class="figure">
<p><img src="https://www.arduino.cc/en/uploads/Tutorial/ArduinoUNO_bb.png" alt="ArduinoUNO_bb.png" />
</p>
</div>
</div>
</div>
<div id="outline-container-sec-1-3" class="outline-3">
<h3 id="sec-1-3"><span class="section-number-3">1.3</span> Analog and digital pins</h3>
<div class="outline-text-3" id="text-1-3">
<p>
An Arduino has both <a href="https://www.arduino.cc/en/Tutorial/AnalogInputPins">analog</a> and <a href="https://www.arduino.cc/en/Tutorial/DigitalPins">digital</a> pins. A standard Arduino board, like UNO, Duemilanove etc, has <b>6 analog inputs</b> and <b>14 digital inputs/outputs</b>.
</p>
<p>
<code>pinMode()</code> function is used to declare if a pin is an input or an output. By default Arduino's (Atmega) pins have been set to inputs. If a pin is defined as INPUT a good value for <b>pullup</b> and <b>pulldown</b> resistor is <b>10KΩ</b>. Pin 13 has an LED and a resistor on board, which makes it harder to be handle as an input.
</p>
<p>
<b>Output pins</b> may provide up to <b>40mA</b> of current, which is enough for LEDs and ICs, but not enough for relays and motors. Typical resistor values that should be used to output pins in order to protect your board are 470Ω and 1kΩ.
</p>
<p>
Analog input pins use a 10 bit resolution (0-1023 integers), and they can be used as digital pins as well (GPIO). To do so you have to map analog pins using the aliases <code>A0</code> for analog pin 0 (A1 for pin 1 and so forth).
</p>
<div class="org-src-container">
<pre class="src src-java">pinMode(A0, OUTPUT);
digitalWrite(A0, HIGH); <span class="org-comment-delimiter">// </span><span class="org-comment">set pullup on analog pin 0</span>
</pre>
</div>
<dl class="org-dl">
<dt> WARNING </dt><dd>By turning on a pullup will affect the values reported by <code>analogRead()</code>.
</dd>
</dl>
<p>
If you would like to simulate analog output use the function <code>analogWrite()</code>, which is based on <a href="https://www.arduino.cc/en/Tutorial/PWM">Pulse Width Modulation (PWM)</a>. <code>analogWrite()</code> range of values is 0-255.
</p>
</div>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2"><span class="section-number-2">2</span> Analog read</h2>
<div class="outline-text-2" id="text-2">
</div><div id="outline-container-sec-2-1" class="outline-3">
<h3 id="sec-2-1"><span class="section-number-3">2.1</span> Analog read - PWM output</h3>
<div class="outline-text-3" id="text-2-1">
<p>
A simple example using a photoresistor for analog input and an LED for binary output. Original article below:
</p>
<dl class="org-dl">
<dt> Online example </dt><dd><a href="https://www.arduino.cc/en/Tutorial/AnalogInput">https://www.arduino.cc/en/Tutorial/AnalogInput</a>
</dd>
<dt> Requirements </dt><dd><ul class="org-ul">
<li>photoresistor
</li>
<li>10kΩ resistor
</li>
</ul>
</dd>
</dl>
<div class="figure">
<p><img src="./img/photoresistor.png" alt="photoresistor.png" />
</p>
</div>
<div class="org-src-container">
<pre class="src src-java"><span class="org-type">int</span> <span class="org-variable-name">sensorPin</span> = A0; <span class="org-comment-delimiter">// </span><span class="org-comment">select the input pin for the potentiometer</span>
<span class="org-type">int</span> <span class="org-variable-name">ledPin</span> = 9; <span class="org-comment-delimiter">// </span><span class="org-comment">select the pin for the LED</span>
<span class="org-type">int</span> <span class="org-variable-name">sensorValue</span> = 0; <span class="org-comment-delimiter">// </span><span class="org-comment">variable to store the value coming from the sensor</span>
<span class="org-type">void</span> <span class="org-function-name">setup</span>() {
Serial.begin(9600);
<span class="org-comment-delimiter">// </span><span class="org-comment">declare the ledPin as an OUTPUT:</span>
pinMode(ledPin, OUTPUT);
}
<span class="org-type">void</span> <span class="org-function-name">loop</span>() {
<span class="org-comment-delimiter">// </span><span class="org-comment">open Serial monitor to see the values (top-right icon in menu)</span>
Serial.println(sensorValue);
<span class="org-comment-delimiter">// </span><span class="org-comment">read the value from the sensor:</span>
sensorValue = analogRead(sensorPin);
<span class="org-comment-delimiter">// </span><span class="org-comment">turn the ledPin on</span>
analogWrite(ledPin, sensorValue/4);
delay(sensorValue/4);
}
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2-2" class="outline-3">
<h3 id="sec-2-2"><span class="section-number-3">2.2</span> Analog read - binary output</h3>
<div class="outline-text-3" id="text-2-2">
<p>
A piezoelectric sensor that triggers a state transition.
</p>
<dl class="org-dl">
<dt> Online example </dt><dd><a href="https://www.arduino.cc/en/Tutorial/KnockSensor">https://www.arduino.cc/en/Tutorial/KnockSensor</a>
</dd>
<dt> Requirements </dt><dd><ul class="org-ul">
<li>piezoelectric sensor
</li>
<li>10kΩ resistor or potentiometer (depends on the sensor, see sensor's datasheet)
</li>
<li>LED
</li>
</ul>
</dd>
</dl>
<div class="figure">
<p><img src="./img/piezo-knock_bb.png" alt="piezo-knock_bb.png" />
</p>
</div>
<div class="org-src-container">
<pre class="src src-java"><span class="org-type">int</span> <span class="org-variable-name">ledPin</span> = 13;
<span class="org-type">int</span> <span class="org-variable-name">knockSensor</span> = A0;
<span class="org-type">byte</span> <span class="org-variable-name">val</span> = 0;
<span class="org-type">int</span> <span class="org-variable-name">statePin</span> = LOW;
<span class="org-type">int</span> <span class="org-variable-name">THRESHOLD</span> = 100;
<span class="org-type">void</span> <span class="org-function-name">setup</span>()
{
pinMode(ledPin, OUTPUT);
}
<span class="org-type">void</span> <span class="org-function-name">loop</span>()
{
val = analogRead(knockSensor);
<span class="org-keyword">if</span> (val >= THRESHOLD) {
statePin = <span class="org-negation-char">!</span>statePin;
digitalWrite(ledPin, statePin);
}
delay(100); <span class="org-comment-delimiter">// </span><span class="org-comment">we have to make a delay to avoid overloading the serial port</span>
}
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3"><span class="section-number-2">3</span> Motor control</h2>
<div class="outline-text-2" id="text-3">
<ul class="org-ul">
<li><a href="http://www.tigoe.net/pcomp/code/circuits/motors/stepper-motors/">http://www.tigoe.net/pcomp/code/circuits/motors/stepper-motors/</a>
</li>
</ul>
</div>
<div id="outline-container-sec-3-1" class="outline-3">
<h3 id="sec-3-1"><span class="section-number-3">3.1</span> Servo control using a potentiometer</h3>
<div class="outline-text-3" id="text-3-1">
<ul class="org-ul">
<li><a href="https://www.arduino.cc/en/Tutorial/Knob">https://www.arduino.cc/en/Tutorial/Knob</a>
</li>
</ul>
<div class="figure">
<p><img src="https://www.arduino.cc/en/uploads/Tutorial/knob_BB.png" alt="knob_BB.png" />
</p>
</div>
<div class="org-src-container">
<pre class="src src-java">#<span class="org-type">include</span> <Servo.<span class="org-type">h</span>>
Servo myservo; <span class="org-comment-delimiter">// </span><span class="org-comment">create servo object to control a servo</span>
<span class="org-type">int</span> <span class="org-variable-name">potpin</span> = 0; <span class="org-comment-delimiter">// </span><span class="org-comment">analog pin used to connect the potentiometer</span>
<span class="org-type">int</span> <span class="org-variable-name">val</span>; <span class="org-comment-delimiter">// </span><span class="org-comment">variable to read the value from the analog pin</span>
<span class="org-type">void</span> <span class="org-function-name">setup</span>() {
myservo.attach(9); <span class="org-comment-delimiter">// </span><span class="org-comment">attaches the servo on pin 9 to the servo object</span>
}
<span class="org-type">void</span> <span class="org-function-name">loop</span>() {
val = analogRead(potpin); <span class="org-comment-delimiter">// </span><span class="org-comment">reads the value of the potentiometer (</span><span class="org-comment"><span class="org-whitespace-line">value between 0 and 1023)</span></span>
val = map(val, 0, 1023, 0, 180); <span class="org-comment-delimiter">// </span><span class="org-comment">scale it to use it with the servo (val</span><span class="org-comment"><span class="org-whitespace-line">ue between 0 and 180)</span></span>
myservo.write(val); <span class="org-comment-delimiter">// </span><span class="org-comment">sets the servo position according to t</span><span class="org-comment"><span class="org-whitespace-line">he scaled value</span></span>
delay(15); <span class="org-comment-delimiter">// </span><span class="org-comment">waits for the servo to get there</span>
}
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-3-2" class="outline-3">
<h3 id="sec-3-2"><span class="section-number-3">3.2</span> Stepper control</h3>
</div>
</div>
<div id="outline-container-sec-4" class="outline-2">
<h2 id="sec-4"><span class="section-number-2">4</span> Arduino and Processing</h2>
<div class="outline-text-2" id="text-4">
<ul class="org-ul">
<li><a href="http://playground.arduino.cc/Interfacing/ProcesssHackForFirmata">http://playground.arduino.cc/Interfacing/ProcesssHackForFirmata</a>
</li>
</ul>
</div>
<div id="outline-container-sec-4-1" class="outline-3">
<h3 id="sec-4-1"><span class="section-number-3">4.1</span> Read integer number</h3>
<div class="outline-text-3" id="text-4-1">
<ul class="org-ul">
<li><b>Processing → Arduino</b>
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-4-2" class="outline-3">
<h3 id="sec-4-2"><span class="section-number-3">4.2</span> Analog input to serial</h3>
<div class="outline-text-3" id="text-4-2">
<ul class="org-ul">
<li><b>Arduino → Processing</b>
</li>
</ul>
<p>
We are going to use a potentiometer to make graphics in Processing. Use a potentiometer to send your analog input to processing. See tutorial <a href="https://www.arduino.cc/en/Tutorial/Graph">Graph</a>. Make sure to adjust your serial port properly (see Processing script below <code>Serial.list()[4]</code>).
</p>
<dl class="org-dl">
<dt> Link to Arduino script </dt><dd><a href="./src/potensiometerGraph.ino">./src/potensiometerGraph.ino</a>
</dd>
<dt> (no term) </dt><dd><a href="https://www.arduino.cc/en/Tutorial/Graph">https://www.arduino.cc/en/Tutorial/Graph</a>
</dd>
<dt> (no term) </dt><dd><a href="http://playground.arduino.cc/Interfacing/Processing">http://playground.arduino.cc/Interfacing/Processing</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-java"><span class="org-type">void</span> <span class="org-function-name">setup</span>()
{
<span class="org-comment-delimiter">// </span><span class="org-comment">initialize the serial communication:</span>
Serial.begin(9600);
}
<span class="org-type">void</span> <span class="org-function-name">loop</span>()
{
<span class="org-comment-delimiter">// </span><span class="org-comment">send the value of analog input 0:</span>
Serial.println(analogRead(A0));
<span class="org-comment-delimiter">// </span><span class="org-comment">wait a bit for the analog-to-digital converter</span>
<span class="org-comment-delimiter">// </span><span class="org-comment">to stabilize after the last reading:</span>
delay(2);
}
</pre>
</div>
<dl class="org-dl">
<dt> Link to Processing script </dt><dd><a href="./src/potensiometerGraph.pde">./src/potensiometerGraph.pde</a>
</dd>
</dl>
<p>
Make sure to have a look at <a href="https://processing.org/reference/libraries/serial/Serial.html">Serial</a> class, <a href="https://www.processing.org/reference/libraries/serial/serialEvent_.html">serialEvent()</a> and <a href="https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html">readStringUntil()</a> functions.
</p>
<div class="org-src-container">
<pre class="src src-java"><span class="org-keyword">import</span> <span class="org-constant">processing</span>.<span class="org-constant">serial</span>.*;
<span class="org-type">Serial</span> <span class="org-variable-name">myPort</span>; <span class="org-comment-delimiter">// </span><span class="org-comment">The serial port</span>
<span class="org-type">int</span> <span class="org-variable-name">xPos</span> = 1; <span class="org-comment-delimiter">// </span><span class="org-comment">horizontal position of the graph</span>
<span class="org-type">float</span> <span class="org-variable-name">inByte</span> = 0;
<span class="org-type">void</span> <span class="org-function-name">setup</span> ()
{
<span class="org-comment-delimiter">// </span><span class="org-comment">set the window size:</span>
size(400, 300);
<span class="org-comment-delimiter">// </span><span class="org-comment">List all the available serial ports</span>
println(Serial.list());
<span class="org-comment-delimiter">// </span><span class="org-comment">select your serial port</span>
myPort = <span class="org-keyword">new</span> <span class="org-type">Serial</span>(<span class="org-keyword">this</span>, Serial.list()[4], 9600);
<span class="org-comment-delimiter">// </span><span class="org-comment">don't generate a serialEvent() unless you get a newline character:</span>
myPort.bufferUntil(<span class="org-string">'\n'</span>);
<span class="org-comment-delimiter">// </span><span class="org-comment">set inital background:</span>
background(0);
}
<span class="org-type">void</span> <span class="org-function-name">draw</span>()
{
<span class="org-comment-delimiter">// </span><span class="org-comment">draw the line:</span>
stroke(127, 34, 255);
line(xPos, height, xPos, height - inByte);
<span class="org-comment-delimiter">// </span><span class="org-comment">at the edge of the screen, go back to the beginning:</span>
<span class="org-keyword">if</span> (xPos >= width)
{
xPos = 0;
background(0);
}
<span class="org-keyword">else</span>
{
<span class="org-comment-delimiter">// </span><span class="org-comment">increment the horizontal position:</span>
xPos++;
}
}
<span class="org-comment-delimiter">// </span><span class="org-comment">a serial event is called when data is available</span>
<span class="org-type">void</span> <span class="org-function-name">serialEvent</span> (<span class="org-type">Serial</span> <span class="org-variable-name">myPort</span>)
{
<span class="org-comment-delimiter">// </span><span class="org-comment">get the ASCII string:</span>
<span class="org-type">String</span> <span class="org-variable-name">inString</span> = myPort.readStringUntil(<span class="org-string">'\n'</span>);
<span class="org-keyword">if</span> (inString != <span class="org-constant">null</span>)
{
<span class="org-comment-delimiter">// </span><span class="org-comment">trim off any whitespace:</span>
inString = trim(inString);
<span class="org-comment-delimiter">// </span><span class="org-comment">convert to an int and map to the screen height:</span>
inByte = <span class="org-type">float</span>(inString);
println(inByte);
inByte = map(inByte, 0, 1023, 0, height);
}
}
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-5" class="outline-2">
<h2 id="sec-5"><span class="section-number-2">5</span> Arduino and SuperCollider</h2>
<div class="outline-text-2" id="text-5">
</div>
<div id="outline-container-sec-5-1" class="outline-3">
<h3 id="sec-5-1"><span class="section-number-3">5.1</span> Analog write - Dimmer</h3>
<div class="outline-text-3" id="text-5-1">
<ul class="org-ul">
<li><b>SuperCollider → Arduino</b>
</li>
<li id="Link to Arduino script"><a href="./src/analogWriteDimmer.ino">./src/analogWriteDimmer.ino</a>
</li>
</ul>
<div class="org-src-container">
<pre class="src src-java"><span class="org-keyword">const</span> <span class="org-type">int</span> <span class="org-variable-name">ledPin</span> = 9; <span class="org-comment-delimiter">// </span><span class="org-comment">the pin that the LED is attached to</span>
<span class="org-type">void</span> <span class="org-function-name">setup</span>()
{
<span class="org-comment-delimiter">// </span><span class="org-comment">initialize the serial communication:</span>
Serial.begin(9600);
<span class="org-comment-delimiter">// </span><span class="org-comment">initialize the ledPin as an output:</span>
pinMode(ledPin, OUTPUT);
}
<span class="org-type">void</span> <span class="org-function-name">loop</span>()
{
<span class="org-type">byte</span> <span class="org-variable-name">brightness</span>;
<span class="org-comment-delimiter">// </span><span class="org-comment">check if data has been sent from the computer:</span>
<span class="org-keyword">if</span> (Serial.available())
{
<span class="org-comment-delimiter">// </span><span class="org-comment">read the most recent byte (which will be from 0 to 255):</span>
brightness = Serial.read();
<span class="org-comment-delimiter">// </span><span class="org-comment">set the brightness of the LED:</span>
analogWrite(ledPin, brightness);
}
}
</pre>
</div>
<dl class="org-dl">
<dt> Link to SC3 script </dt><dd><a href="./src/analogWriteDimmer.scd">./src/analogWriteDimmer.scd</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-sclang"><span class="org-comment-delimiter">// </span><span class="org-comment">DIMMER</span>
(
p = <span class="org-type">SerialPort</span>(
<span class="org-string">"/dev/ttyUSB0"</span>, <span class="org-comment-delimiter">//</span><span class="org-comment">edit to match your port. SerialPort.listDevices</span>
<span class="org-variable-name">baudrate:</span> 9600, <span class="org-comment-delimiter">//</span><span class="org-comment">check that baudrate is the same as in arduino sketch</span>
<span class="org-variable-name">crtscts:</span> <span class="org-builtin">true</span>);
)
<span class="org-comment-delimiter">//</span><span class="org-comment">send serial data - slow pulsating</span>
(
r = <span class="org-type">Routine</span>({
<span class="org-builtin">inf</span>.do{|i|
p.put(i.fold(0, 100).linexp(0, 100, 1, 255).asInteger.postln);
0.02.wait;
};
}).play;
)
r.stop;
p.close;
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-5-2" class="outline-3">
<h3 id="sec-5-2"><span class="section-number-3">5.2</span> Digital write - GUI with buttons</h3>
<div class="outline-text-3" id="text-5-2">
<ul class="org-ul">
<li><b>SuperCollider → Arduino</b>
</li>
</ul>
<p>
A simple protocol for serial communication, based on the example by <a href="http://www.fredrikolofsson.com/f0blog/?q=node/605">Fredrik Olofsson</a>.
</p>
<dl class="org-dl">
<dt> Link to Arduino script </dt><dd><a href="./src/analogWriteDimmer.ino">./src/analogWriteDimmer.ino</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-java"><span class="org-type">byte</span> <span class="org-variable-name">digPins</span>[] = {8, 9, 10, 11, 12, 13};
<span class="org-type">byte</span> <span class="org-variable-name">val</span>, <span class="org-variable-name">cnt</span> = 0;
<span class="org-type">byte</span> <span class="org-variable-name">digData</span>[] = {0, 0, 0, 0, 0, 0, 0, 0};
<span class="org-type">void</span> <span class="org-function-name">setup</span>()
{
Serial.begin(38400);
<span class="org-keyword">for</span>(<span class="org-type">byte</span> <span class="org-variable-name">i</span> = 0; i < 6; i++)
{
pinMode(digPins[i], OUTPUT);
}
}
<span class="org-type">void</span> <span class="org-function-name">loop</span>()
{
<span class="org-keyword">while</span>(Serial.available())
{
val = Serial.read();
<span class="org-keyword">if</span>(cnt == 0)
{
<span class="org-keyword">if</span>(val == 10)
{
cnt = 1;
}
}
<span class="org-keyword">else</span> <span class="org-keyword">if</span>(cnt < 7)
{
digData[cnt - 1] = val;
cnt++;
}
<span class="org-keyword">else</span>
{
<span class="org-keyword">if</span>(val == 11)
{
<span class="org-keyword">for</span>(<span class="org-type">byte</span> <span class="org-variable-name">i</span> = 0; i < 6; i++)
{
digitalWrite(digPins[i], digData[i]);
}
}
<span class="org-keyword">else</span>
{
<span class="org-comment-delimiter">// </span><span class="org-comment">serial read error</span>
}
cnt = 0; <span class="org-comment-delimiter">// </span><span class="org-comment">reset counter</span>
}
}
delay(10);
}
</pre>
</div>
<dl class="org-dl">
<dt> Link to SC3 script </dt><dd><a href="./src/digitalWriteButtons.scd">./src/digitalWriteButtons.scd</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-sclang">(
<span class="org-keyword">var</span> name = <span class="org-string">"/dev/ttyUSB0"</span>; <span class="org-comment-delimiter">//</span><span class="org-comment">edit to match your serial device</span>
<span class="org-keyword">var</span> port = <span class="org-type">SerialPort</span>(name, 38400, <span class="org-variable-name">crtscts:</span> <span class="org-builtin">true</span>);
<span class="org-keyword">var</span> dig = [10, 0, 0, 0, 0, 0, 0, 11];
<span class="org-keyword">var</span> w = <span class="org-type">Window</span>(<span class="org-string">"buttons write"</span>);
<span class="org-keyword">var</span> v = <span class="org-type">HLayoutView</span>(w, <span class="org-type">Rect</span>(30, 30, 300, 300));
<span class="org-keyword">var</span> butt;
<span class="org-type">Array</span>.fill(6, { | i |
butt = <span class="org-type">Button</span>(v, <span class="org-type">Rect</span>(10, 10, 50, 100));
butt.states_([[<span class="org-string">"pin"</span>++(8+i).asString, <span class="org-type">Color</span>.black, <span class="org-type">Color</span>.white], [<span class="org-string">"pin"</span>++(8+i).asString, <span class="org-type">Color</span>.black, <span class="org-type">Color</span>.green]]);
butt.action_({|view| dig.put(i+1, (view.value).asInteger); port.putAll(dig.postln)});
});
w.front;
<span class="org-type">CmdPeriod</span>.doOnce({port.putAll([10, 0, 0, 0, 0, 0, 0, 11]); port.close; w.close});
)
</pre>
</div>
</div>
<div id="outline-container-sec-5-2-1" class="outline-4">
<h4 id="sec-5-2-1"><span class="section-number-4">5.2.1</span> Using port registers</h4>
<div class="outline-text-4" id="text-5-2-1">
<ul class="org-ul">
<li><a href="https://www.arduino.cc/en/Reference/PortManipulation">https://www.arduino.cc/en/Reference/PortManipulation</a>
</li>
<li><a href="http://playground.arduino.cc/Code/BitMath#introduction">http://playground.arduino.cc/Code/BitMath#introduction</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-6" class="outline-2">
<h2 id="sec-6"><span class="section-number-2">6</span> Appendix</h2>
<div class="outline-text-2" id="text-6">
</div><div id="outline-container-sec-6-1" class="outline-3">
<h3 id="sec-6-1"><span class="section-number-3">6.1</span> Resistor color codes</h3>
<div class="outline-text-3" id="text-6-1">
<dl class="org-dl">
<dt> If you don't have a multimeter to measure the value of a resistor you can either use an online application or calculate the Ohms of your resistor using the table in the wikipedia link below </dt><dd><a href="https://en.wikipedia.org/wiki/Electronic_color_code#Resistor_color-coding">https://en.wikipedia.org/wiki/Electronic_color_code#Resistor_color-coding</a>
</dd>
</dl>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<style type="text/css">
.back-to-top {
position: fixed;
bottom: 2em;
right: 0px;
text-decoration: none;
color: #000000;
background-color: rgba(235, 235, 235, 0.80);
font-size: 12px;
padding: 1em;
display: none;
}
.back-to-top:hover {
background-color: rgba(135, 135, 135, 0.50);
}
</style>
<div class="back-to-top">
<a href="#top">Back to top</a> | <a href="mailto:[email protected]">E-mail me</a>
<p></p>
<center>
<a href="http://users.jyu.fi/~gediapou">Georgios Diapoulis</a>
</center>
</div>
<script type="text/javascript">
var offset = 220;
var duration = 500;
jQuery(window).scroll(function() {
if (jQuery(this).scrollTop() > offset) {
jQuery('.back-to-top').fadeIn(duration);
} else {
jQuery('.back-to-top').fadeOut(duration);
}
});
</script>
</div>
</body>
</html>