<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>serialWrite</title> <link rel="stylesheet" type="text/css" href="csound.css" /> <meta name="generator" content="DocBook XSL Stylesheets V1.76.1" /> <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" /> <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" /> <link rel="prev" href="serialWrite_i.html" title="serialWrite_i" /> <link rel="next" href="seqtime2.html" title="seqtime2" /> </head> <body> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr> <th colspan="3" align="center">serialWrite</th> </tr> <tr> <td width="20%" align="left"><a accesskey="p" href="serialWrite_i.html">Prev</a> </td> <th width="60%" align="center">Orchestra Opcodes and Operators</th> <td width="20%" align="right"> <a accesskey="n" href="seqtime2.html">Next</a></td> </tr> </table> <hr /> </div> <div class="refentry" title="serialWrite"> <a id="serialWrite"></a> <div class="titlepage"></div> <a id="IndexSerialWrite" class="indexterm"></a> <div class="refnamediv"> <h2> <span class="refentrytitle">serialWrite</span> </h2> <p>serialWrite — Write data to a serial port. </p> </div> <div class="refsect1" title="Description"> <a id="idp42052304"></a> <h2>Description</h2> <p> Write data to a serial port for arduino. </p> </div> <div class="refsect1" title="Syntax"> <a id="idp42052960"></a> <h2>Syntax</h2> <pre class="synopsis"> <span class="command"><strong>serialWrite</strong></span> iPort, iByte</pre> <pre class="synopsis"> <span class="command"><strong>serialWrite</strong></span> iPort, kByte</pre> <pre class="synopsis"> <span class="command"><strong>serialWrite</strong></span> iPort, SBytes</pre> </div> <div class="refsect1" title="Performance"> <a id="idp42091464"></a> <h2>Performance</h2> <p> <span class="emphasis"><em>iPort</em></span> -- port number optained from a <span class="emphasis"><em>serialBegin</em></span> opcode. </p> <p> <span class="emphasis"><em>iByte</em></span> -- a byte of data to write. </p> </div> <div class="refsect1" title="Examples"> <a id="idp42092984"></a> <h2>Examples</h2> <p> Here is an example of the serialWrite opcode. It uses the file <a class="ulink" href="examples/serialWrite.csd" target="_top"><em class="citetitle">serialWrite.csd</em></a>. </p> <div class="example"> <a id="idp42093976"></a> <p class="title"> <strong>Example 756. Example of the serialWrite opcode.</strong> </p> <div class="example-contents"> <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p> <pre class="programlisting"> <span class="csdtag"><CsoundSynthesizer></span> <span class="csdtag"><CsOptions></span> <span class="comment">; Select audio/midi flags here according to platform</span> -n <span class="comment">;;;no output</span> <span class="comment">;-iadc ;;;uncomment -iadc if realtime audio input is needed too</span> <span class="comment">; For Non-realtime ouput leave only the line below:</span> <span class="comment">; -o serialWrite.wav -W ;;; for file output any platform</span> <span class="csdtag"></CsOptions></span> <span class="csdtag"><CsInstruments></span> <span class="ohdr">sr</span> <span class="op">=</span> 44100 <span class="ohdr">ksmps</span> <span class="op">=</span> 500 <span class="comment">; the default krate can be too fast for the arduino to handle</span> <span class="ohdr">nchnls</span> <span class="op">=</span> 2 <span class="ohdr">0dbfs</span> <span class="op">=</span> 1 <span class="oblock">instr</span> 1 iPort <span class="opc">serialBegin</span> "<span class="op">/</span>dev<span class="op">/</span>cu.usbmodemfa131", 9600 <span class="comment">;connect to the arduino with baudrate = 9600</span> kGain <span class="opc">init</span> 16 <span class="comment">;read our knob value</span> kVal <span class="opc">serialRead</span> iPort <span class="octrl">if</span> (kVal <span class="op">!=</span> <span class="op">-</span>1) then kGain <span class="op">=</span> kVal<span class="op">/</span>128 <span class="octrl">endif</span> aSig <span class="opc">in</span> <span class="comment">;get our audio input and get its rms</span> kRms <span class="opc">rms</span> aSig<span class="op">*</span>kGain kRms <span class="op">=</span> kRms<span class="op">*</span>kRms<span class="op">*</span>255 <span class="comment">;scale the rms to a good value for the LED and send it out</span> <span class="opc">serialWrite</span> iPort, (kRms <span class="op"><</span> 255 <span class="op">?</span> kRms <span class="op">:</span> 255) <span class="comment">;must be in range: 0-255</span> <span class="oblock">endin</span> <span class="csdtag"></CsInstruments></span> <span class="csdtag"><CsScore></span> <span class="stamnt">f</span> 1 0 1024 10 1 1 1 1 1 1 <span class="stamnt">i</span> 1 0 200 <span class="stamnt">e</span> <span class="csdtag"></CsScore></span> <span class="csdtag"></CsoundSynthesizer></span> </pre> </div> </div> <p><br class="example-break" /> This is the matching Arduino code : </p> <pre class="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) } } .....</pre> <p> </p> </div> <div class="refsect1" title="See Also"> <a id="idp42097224"></a> <h2>See Also</h2> <p> <a class="link" href="serialBegin.html" title="serialBegin"><em class="citetitle"></em>serialBegin</a>, <a class="link" href="serialEnd.html" title="serialEnd"><em class="citetitle"></em>serialEnd</a>, <a class="link" href="serialWrite_i.html" title="serialWrite_i"><em class="citetitle"></em>serialWrite_i</a>, <a class="link" href="serialRead.html" title="serialRead"><em class="citetitle"></em>serialRead</a>, <a class="link" href="serialPrint.html" title="serialPrint"><em class="citetitle"></em>serialPrint</a>, <a class="link" href="serialFlush.html" title="serialFlush"><em class="citetitle"></em>serialFlush</a>. </p> </div> <div class="refsect1" title="Credits"> <a id="idp42100704"></a> <h2>Credits</h2> <p> </p> <table border="0" summary="Simple list" class="simplelist"> <tr> <td>Author: Matt Ingalls</td> </tr> <tr> <td>2011</td> </tr> </table> <p> </p> <p>New in version 5.14</p> </div> </div> <div class="navfooter"> <hr /> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"><a accesskey="p" href="serialWrite_i.html">Prev</a> </td> <td width="20%" align="center"> <a accesskey="u" href="OpcodesTop.html">Up</a> </td> <td width="40%" align="right"> <a accesskey="n" href="seqtime2.html">Next</a></td> </tr> <tr> <td width="40%" align="left" valign="top">serialWrite_i </td> <td width="20%" align="center"> <a accesskey="h" href="index.html">Home</a> </td> <td width="40%" align="right" valign="top"> seqtime2</td> </tr> </table> </div> </body> </html>