blob: 7e963ef04472ebd7fea0294c77ca10d7aac96cba (
plain)
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
|
<refentry id="serialRead">
<indexterm
id="IndexSerialRead"><primary>serialRead</primary></indexterm>
<refentryinfo><title>Serial I/O</title></refentryinfo>
<refmeta>
<refentrytitle>serialRead</refentrytitle>
</refmeta>
<refnamediv>
<refname>serialRead</refname>
<refpurpose>
Read data from a serial port.
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<para>
Read data from a serial port for arduino.
</para>
</refsect1>
<refsect1>
<title>Syntax</title>
<synopsis>kByte <command>serialRead</command> iPort</synopsis>
</refsect1>
<refsect1>
<title>Performance</title>
<para>
<emphasis>iPort</emphasis> -- port number optained from a
<emphasis>serialBegin</emphasis> opcode.
</para>
<para>
<emphasis>kByte</emphasis> -- a byte of data to read.
</para>
</refsect1>
<refsect1>
<title>Examples</title>
<para>
Here is an example of the serialRead opcode. It uses the file <ulink url="examples/serialRead.csd"><citetitle>serialRead.csd</citetitle></ulink>.
<example>
<title>Example of the serialRead opcode.</title>
<para>See the sections <link linkend="UsingRealTime"><citetitle>Real-time Audio</citetitle></link> and <link linkend="CommandFlags"><citetitle>Command Line Flags</citetitle></link> for more information on using command line flags.</para>
<xi:include href="examples-xml/serialRead.csd.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
</example>
This is the matching Arduino code :
<screen>
void setup() {
// enable serial communication
Serial.begin(9600);
// declare pin 9 to be an output:
pinMode(9, OUTPUT);
}
void loop() {
// only do something if we received something (this should be at csound's k-rate)
if (Serial.available()) {
// set the brightness of LED (connected to pin 9) to our input value
int brightness = Serial.read();
analogWrite(9, brightness);
// while we are here, get our knob value and send it to csound
int sensorValue = analogRead(A0);
Serial.write(sensorValue/4); // scale to 1-byte range (0-255)
}
}
.....</screen>
</para>
</refsect1>
<refsect1>
<title>See Also</title>
<para>
<link linkend="serialBegin"><citetitle></citetitle>serialBegin</link>,
<link linkend="serialEnd"><citetitle></citetitle>serialEnd</link>,
<link linkend="serialWrite_i"><citetitle></citetitle>serialWrite_i</link>,
<link linkend="serialWrite"><citetitle></citetitle>serialWrite</link>,
<link linkend="serialPrint"><citetitle></citetitle>serialPrint</link>,
<link linkend="serialFlush"><citetitle></citetitle>serialFlush</link>.
</para>
</refsect1>
<refsect1>
<title>Credits</title>
<para>
<simplelist>
<member>Author: &namematt;</member>
<member>2011</member>
</simplelist>
</para>
<para>New in version 5.14</para>
</refsect1>
</refentry>
|