<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>kasperkamperman.com : creative media technology&#187; Arduino  &#8211; kasperkamperman.com : creative media technology</title>
	<atom:link href="http://www.kasperkamperman.com/blog/arduino/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kasperkamperman.com</link>
	<description>Kasper Kamperman designs and creates interactive and responsive media applications for exhibitions, interior and events. He works as a teacher in the field of Art and Technology</description>
	<lastBuildDate>Thu, 02 Sep 2010 12:14:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Arduino Flash communication AS3 &#8211; Messenger</title>
		<link>http://www.kasperkamperman.com/blog/arduino-flash-communication-as3-messenger/</link>
		<comments>http://www.kasperkamperman.com/blog/arduino-flash-communication-as3-messenger/#comments</comments>
		<pubDate>Thu, 13 May 2010 21:52:41 +0000</pubDate>
		<dc:creator>kasperkamperman</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.kasperkamperman.com/?p=857</guid>
		<description><![CDATA[Example how to setup serial communication between Flash and Arduino in combination with the Messenger library for Arduino. This is usefull if you want to use Arduino in a different way than as a sensorbox (if you want that check out the As3glue Firmata combination in my other post).


Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS2'>Arduino Flash communication AS2</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication-as3/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS3 &#8211; As3Glue bundle'>Arduino Flash communication AS3 &#8211; As3Glue bundle</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Example how to setup serial communication between Flash and Arduino in combination with the Messenger library for Arduino. You can send whatever you want between Arduino and Flash (if you only want to receive data from inputs and set outputs it better to use <a href="http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication-as3/">Firmata / As3Glue combination</a>). </p>
<p>For receiving of data on the Arduino I&#8217;ll choose the <a href="http://www.arduino.cc/playground/Code/Messenger">Messenger library</a>. It has some nice simple functions and gives you the opportunity to check if a string is fully received (a &#8216;carriage return&#8217; is send by Flash at the end of a serial message in the SerialPort class) by Arduino. Of course you can write your own serial protocol. You can find some thoughts about it at the Todbot blog : <a href="http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/">Arduino serial protocol design patterns</a>.</p>
<p>You can send data to the serial port with the <a href="http://arduino.cc/en/Serial/Print">Serial.print()</a> and <a href="http://arduino.cc/en/Serial/Println">Serial.println()</a> commands.</p>
<p>Included is the SerialPort class for Flash that makes it easy to send and receive serial data from/to the Arduino. I&#8217;ve modified the original (from <a href="http://www.tinker.it/blog/">tinker.it</a>) so it supports start and stop characters. As a stop character &#8216;carriage return&#8217; is used (&#8216;\r&#8217;, or char(13)) that is send when you do Serial.println(); from Arduino. </p>
<p>I advise using a start character (not used in the example below) as well, because sometimes (especially when sending on a high datarate) characters at the beginning if the string can get lost. </p>
<p>The download contains SerialPort.as and example_messenger.fla (CS3).  </p>
<p>Download : <a class="download" href='http://www.kasperkamperman.com/wordpress_kk/wordpress_uploads/2010/05/flash-arduino-as3-messenger.zip'>flash-arduino-as3-messenger</a></p>
<h2>Step by step guide for Messenger library communication</h2>
<ul>
<li>Download the <a href="http://www.arduino.cc/playground/Code/Messenger">Messenger library</a> and install it in your <em>Arduino/libraries/</em> folder ( you can find it by default in your documents folder ). Restart Arduino if it was running. </li>
<li>
Go to <em>File > Examples > Messenger > basic_communication</em>.</li>
<li>Change
<div class="codecolorer-container arduino default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="arduino codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066;">Serial</span>.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">115200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>to</p>
<div class="codecolorer-container arduino default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="arduino codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066;">Serial</span>.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">57600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>if you use my Serialproxy setup instructions from my tutorial ( see <a href="http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication-as3/">Arduino &#8211; Flash communication AS3 &#8211; AS3Glue bundle</a> ). </li>
<li>Change
<div class="codecolorer-container arduino default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="arduino codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> interval <span style="color: #339933;">=</span> <span style="color: #0000dd;">20</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>to</p>
<div class="codecolorer-container arduino default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="arduino codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> interval <span style="color: #339933;">=</span> <span style="color: #0000dd;">40</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>for more stability</li>
<li>Upload the Sketch to the Arduino.</li>
<li>Configure Serialproxy for your Arduino board and run it.</li>
<li>Open example_messenger.fla and run it.</li>
<li>If everything went fine you&#8217;ll see the led on pin 13 blinking and 6 balls, controlled by the analog inputs, moving on the screen.</li>
</ul>
<h2>Example_messenger code</h2>
<pre>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> receivedValues<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Arduino connection</span><br />
<br />
<span style="color: #3f5fbf;">/* SerialPort(localhost, netport, start character, stop character)<br />
&nbsp; &nbsp;for more stability use a startcharacter so you can check if a message is complete<br />
&nbsp; &nbsp;default stop character is carriage return '\r' <br />
&nbsp; &nbsp;A carriage return is send by the Arduino command Serial.println();<br />
*/</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> arduino<span style="color: #000066; font-weight: bold;">:</span>SerialPort = <span style="color: #0033ff; font-weight: bold;">new</span> SerialPort<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;127.0.0.1&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">5331</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
arduino<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">DataEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">DATA</span><span style="color: #000066; font-weight: bold;">,</span> onArduinoData <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
arduino<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">connect</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Setup a timer to send data to the Arduino</span><br />
<span style="color: #009900; font-style: italic;">// The timer object calls the timerEvent function 20 times a second (every 50ms)</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">timer</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Timer</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">500</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #004993;">timer</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;timer&quot;</span><span style="color: #000066; font-weight: bold;">,</span> timerEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #004993;">timer</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// toggle variable that is switched by the timer from true to false and back</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> toggle<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>= <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> timerEvent<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <br />
<span style="color: #000000;">&#123;</span> &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// change toggle boolean to the opposite</span><br />
&nbsp; &nbsp; toggle = <span style="color: #000066; font-weight: bold;">!</span>toggle<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>toggle == <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span> <span style="color: #009900; font-style: italic;">// make ledPin 13 HIGH - onboard led on </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//arduino.send(&quot;on&quot;); // For Messenger checkString example</span><br />
&nbsp; &nbsp; &nbsp; arduino<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">send</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;13 1&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// For Messenger basic_communication example</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">else</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span> <span style="color: #009900; font-style: italic;">// make ledPin 13 LOW - onboard led off</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//arduino.send(&quot;off&quot;); // For Messenger checkString example</span><br />
&nbsp; &nbsp; &nbsp; arduino<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">send</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;13 0&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> &nbsp;<span style="color: #009900; font-style: italic;">// For Messenger basic_communication example</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #3f5fbf;">/*<br />
&nbsp; &nbsp; When you have a certain amount of values it will be smart to keep them<br />
&nbsp; &nbsp; in an array (so you only modify the array indexes that change.<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; You can convert an array to a character seperated string (space character for <br />
&nbsp; &nbsp; Messenger library) with the join function.<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; Example :<br />
&nbsp; &nbsp; var sendValues:Array = new Array(2,4,8,16,32,64,128);<br />
&nbsp; &nbsp; arduino.send(sendValues.join(&quot; &quot;));<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; */</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// add some balls to the stage to display incoming analog values</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> ballArray = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>i<span style="color: #000066; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">6</span><span style="color: #000066; font-weight: bold;">;</span>i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span> &nbsp; <br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> ball<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Ball<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; ballArray<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = ball<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; ball<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> = ball<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> = <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; ball<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; ball<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">+</span><span style="color: #000000;">&#40;</span>i<span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">60</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>ball<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Called when data is received from the Arduino</span><br />
<span style="color: #339966; font-weight: bold;">function</span> onArduinoData<span style="color: #000000;">&#40;</span> event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">DataEvent</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><br />
<span style="color: #000000;">&#123;</span> &nbsp; <br />
&nbsp; &nbsp; <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;received string : &quot;</span><span style="color: #000066; font-weight: bold;">,</span> event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">data</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #3f5fbf;">/* The values from Arduino Messenger are space seperated values. We will <br />
&nbsp; &nbsp; &nbsp; &nbsp;store each value in the receivedValues array. &nbsp; &nbsp;<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; receivedValues = event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">split</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// change the y position of the balls on stage </span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>i<span style="color: #000066; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">6</span><span style="color: #000066; font-weight: bold;">;</span>i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span> <span style="color: #6699cc; font-weight: bold;">var</span> mc = ballArray<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; mc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = receivedValues<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">350</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">1023</span><span style="color: #000066; font-weight: bold;">;</span>&nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span> &nbsp; <br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
</pre>


<p>Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS2'>Arduino Flash communication AS2</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication-as3/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS3 &#8211; As3Glue bundle'>Arduino Flash communication AS3 &#8211; As3Glue bundle</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.kasperkamperman.com/blog/arduino-flash-communication-as3-messenger/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Arduino &#8211; Unity serial communication</title>
		<link>http://www.kasperkamperman.com/blog/arduino-unity-serial-communication/</link>
		<comments>http://www.kasperkamperman.com/blog/arduino-unity-serial-communication/#comments</comments>
		<pubDate>Wed, 12 May 2010 21:45:43 +0000</pubDate>
		<dc:creator>kasperkamperman</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.kasperkamperman.com/?p=850</guid>
		<description><![CDATA[Control parameters in Unity with sensors connected to the Arduino and control actuators connected to Arduino from Unity.


Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication-as3/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS3 &#8211; As3Glue bundle'>Arduino Flash communication AS3 &#8211; As3Glue bundle</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino-flash-communication-as3-messenger/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS3 &#8211; Messenger'>Arduino Flash communication AS3 &#8211; Messenger</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Art &#038; Technology student Tiuri de Jong did a project on Arduino and Unity communication. The goal was (and still is) to create an easy workflow and tutorial on how to connect Arduino with Unity (free version). Due the serial implementation in the Mac OSX version of Unity, this part still doesn&#8217;t work. Suggestions and contributions are welcome on the <a href="http://code.google.com/p/unity-arduino-serial-connection/">project page</a>. </p>
<blockquote><h2>Project: unity-arduino with serial connection</h2>
<p>This project uses Unity (which uses C# and JavaScript scripting), the Arduino and/or SerialProxy to connect the Arduino to Unity. Thus allowing custom hardware to be linked to Unity (in real-time).</p>
<p>The link is established in Windows using the Mono implementation of System.IO.Ports.</p>
<p>For Mac/OSX users, I&#8217;ve made an implementation that uses SerialProxy because direct serial connection doesn&#8217;t work on the mac. The problem is, though, that the SerialProxy implementation doesn&#8217;t seem to work either. </p></blockquote>
<p>Links:</p>
<ul>
<li><a href="http://code.google.com/p/unity-arduino-serial-connection/">Unity-Arduino serial-connection</a>  on Google code</li>
<li><a href="http://unity3d.com/">Unity : Game Development Tool</a></li>
<li>
<a href="http://www.arduino.cc">Arduino</a></li>
</ul>


<p>Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication-as3/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS3 &#8211; As3Glue bundle'>Arduino Flash communication AS3 &#8211; As3Glue bundle</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino-flash-communication-as3-messenger/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS3 &#8211; Messenger'>Arduino Flash communication AS3 &#8211; Messenger</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.kasperkamperman.com/blog/arduino-unity-serial-communication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arduino Flash communication AS3 &#8211; As3Glue bundle</title>
		<link>http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication-as3/</link>
		<comments>http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication-as3/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 16:36:18 +0000</pubDate>
		<dc:creator>kasperkamperman</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.kasperkamperman.com/?p=789</guid>
		<description><![CDATA[Video tutorial on how to setup communication between Arduino and Flash ( Actionscript 3.0 ) with Firmata, As3Glue and Serialproxy. You can download a bundle with examples and the libraries/software. 


Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS2'>Arduino Flash communication AS2</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino-flash-communication-as3-messenger/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS3 &#8211; Messenger'>Arduino Flash communication AS3 &#8211; Messenger</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication-as3/"><em>Click here to view the embedded video.</em></a></p>
<p>I&#8217;ve created a Flash monitor application with As3Glue to make it easier to start with Flash Arduino communication.</p>
<p>Glue is an ActionScript 3.0 library that enables communication between Flash and Arduino boards with Firmata. Since Flash cannot interface the serial port directly we&#8217;ll use Serialproxy in between. Serialproxy converts incoming serial data the XMLsocket data. </p>
<p>The monitor is useful to check if all the connections are working good. You can monitor directly all the sensors and actuators connected to your Arduino board. </p>
<p>The example code file is a start point to program your own things controlled by Arduino. </p>
<p>Included files :</p>
<ul>
<li><a href="http://code.google.com/p/as3glue/ ">AS3Glue</a> files ( .net.eriksjodin.arduino )</li>
<li><a href="http://code.google.com/p/tinkerit/wiki/TinkerProxy">Tinkerproxy osx</a> ( renamed to serproxy )</li>
<li><a href="http://cote.cc/blog/serialproxy-v014-can-use-com-ports-above-9">Serialproxy windows</a> ( renamed to serproxy )</li>
<li>as3glue_standalone_monitor.fla</li>
<li>as3glue_program-example.fla</li>
</ul>
<p>The Firmata library is included with Arduino itself. Make sure you use Arduino 018, because the Firmata library included in version 017 is not stable. </p>
<p>Update (12-05-10, v1.3): I&#8217;ve updated the code for the support of two servo motors (pin 9 and pin 10). </p>
<p>Download the zip-file : <a class="download" href='http://www.kasperkamperman.com/wordpress_kk/wordpress_uploads/2010/05/arduino_as3glue_bundle_v14.zip'>arduino_as3glue_bundle_v1.4</a></p>
<p><a href="http://www.kasperkamperman.com/wordpress_kk/wordpress_uploads/2010/03/screenshot_as3-example.jpg">
<img src="http://www.kasperkamperman.com/wordpress_kk/wordpress_uploads/2010/03/screenshot_as3-example.jpg" alt="" title="screenshot_as3-example" width="550" height="671" class="alignnone size-full wp-image-793" /></a></p>


<p>Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS2'>Arduino Flash communication AS2</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino-flash-communication-as3-messenger/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS3 &#8211; Messenger'>Arduino Flash communication AS3 &#8211; Messenger</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication-as3/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Arduino programming &#8211; Manual and links</title>
		<link>http://www.kasperkamperman.com/blog/arduino/arduino-programming-manual/</link>
		<comments>http://www.kasperkamperman.com/blog/arduino/arduino-programming-manual/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 09:29:58 +0000</pubDate>
		<dc:creator>kasperkamperman</dc:creator>
				<category><![CDATA[Arduino]]></category>

		<guid isPermaLink="false">http://www.kasperkamperman.com/?p=634</guid>
		<description><![CDATA[Download the Arduino programming notebook. 


Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-map-and-smooth/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; Map and smooth values'>Arduino Programming &#8211; Map and smooth values</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-interval/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; Interval'>Arduino Programming &#8211; Interval</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>To get started with programming for the Arduino you can download or buy a print version of the excellent programming notebook at lulu.com. There is also a Dutch translation that you can find on <a href="http://www.arduino.nu">www.arduino.nu</a>. </p>
<p>Download the &#8220;Arduino programming notebook&#8221; by Brian Evans at lulu.com :
<a href="http://www.lulu.com/product/download/arduino-programming-notebook/3524028">Arduino programming notebook</a></p>
<p>Download de &#8220;Arduino programmeer manual&#8221;
Nederlandse vertaling door A. Companje van <a href="http://www.arduino.nu">arduino.nu</a> :
<a href="http://www.arduino.nu/download/Arduino%20manual%201_0%20NL.pdf">Arduino programmeer manual</a></p>
<p>The Arduino website has good information to help you get started. See
<a href="http://arduino.cc/en/Guide/HomePage">Getting started with Arduino</a>.</p>
<p>Next to that watch use the code examples in the Arduino IDE and use the <a href="http://arduino.cc/en/Reference/HomePage">Arduino reference</a>.
The Arduino playground has more advanced information  and the <a href="http://www.arduino.cc/playground/">Arduino playground</a> at <a href="http://www.arduino.cc/">arduino.cc</a>. </p>
<p>General programming tips :</p>
<ol>
<li>Learn by doing</li>
<li>Make simple things</li>
<li>Combine simple things to make complex things</li>
<li>Print code for a better overview</li>
</ol>


<p>Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-map-and-smooth/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; Map and smooth values'>Arduino Programming &#8211; Map and smooth values</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-interval/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; Interval'>Arduino Programming &#8211; Interval</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.kasperkamperman.com/blog/arduino/arduino-programming-manual/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arduino Programming &#8211; HSB to RGB</title>
		<link>http://www.kasperkamperman.com/blog/arduino/arduino-programming-hsb-to-rgb/</link>
		<comments>http://www.kasperkamperman.com/blog/arduino/arduino-programming-hsb-to-rgb/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 00:48:52 +0000</pubDate>
		<dc:creator>kasperkamperman</dc:creator>
				<category><![CDATA[Arduino]]></category>

		<guid isPermaLink="false">http://www.kasperkamperman.com/?p=623</guid>
		<description><![CDATA[Control a RGB led with Hue, Saturation and Brightness (HSB / HSV ).


Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-state-change/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; State change'>Arduino Programming &#8211; State change</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-interval/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; Interval'>Arduino Programming &#8211; Interval</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Control a RGB led with Hue, Saturation and Brightness (HSB / HSV ). </p>
<p>Level : beginner with Arduino. ( Basic knowledge of programming principles like if/else and variables  ). </p>
<p>Download the Arduino ( .pde ) files : <a class="download" href='http://www.kasperkamperman.com/wordpress_kk/wordpress_uploads/2010/01/arduino_HSB_to_RGB.zip'>arduino_HSB_to_RGB.zip</a></p>
<p>HSB_to_RGB.pde :</p>
<pre>
<div class="codecolorer-container arduino default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br /></div></td><td><div class="arduino codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/* &nbsp;<br />
&nbsp; Control a RGB led with Hue, Saturation and Brightness (HSB / HSV )<br />
<br />
&nbsp; Hue is change by an analog input. <br />
&nbsp; Brightness is changed by a fading function. <br />
&nbsp; Saturation stays constant at 255<br />
<br />
&nbsp; getRGB() function based on &lt;http://www.codeproject.com/miscctrl/CPicker.asp&gt; &nbsp;<br />
&nbsp; dim_curve idea by Jims<br />
<br />
&nbsp; created 05-01-2010 by kasperkamperman.com<br />
*/</span><br />
<br />
<span style="color: #808080; font-style: italic;">/* <br />
&nbsp; dim_curve 'lookup table' to compensate for the nonlinearity of human vision.<br />
&nbsp; Used in the getRGB function on saturation and brightness to make 'dimming' look more natural. <br />
&nbsp; Exponential function used to create values below : <br />
&nbsp; x from 0 - 255 : y = round(pow( 2.0, x+64/40.0) - 1) &nbsp; <br />
*/</span><br />
<br />
<span style="color: #993333;">const</span> <span style="color: #993333;">byte</span> dim_curve<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">5</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">5</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">5</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">5</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">5</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">5</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">5</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">5</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">5</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">5</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">6</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">6</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">6</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">6</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">6</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">6</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">6</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">6</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">7</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">7</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">7</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">7</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">7</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">7</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">7</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">8</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">8</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">8</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">8</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">8</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">8</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">9</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">9</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">9</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">9</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">9</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">9</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #0000dd;">10</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">10</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">10</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">10</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">10</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">11</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">11</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">11</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">11</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">11</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">12</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">12</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">12</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">12</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">12</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">13</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">13</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">13</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">13</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">14</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">14</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">14</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">14</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">15</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">15</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">15</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">16</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">16</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">16</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">16</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">17</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">17</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">17</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">18</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">18</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">18</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">19</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">19</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">19</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">20</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">20</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">20</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">21</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">21</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">22</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">22</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">22</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">23</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">23</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">24</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">24</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">25</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">25</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">25</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">26</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">26</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">27</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">27</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">28</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">28</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">29</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">29</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">30</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">30</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">31</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">32</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">32</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">33</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">33</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">34</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">35</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">35</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">36</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">36</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">37</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">38</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">38</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">39</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">40</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">40</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">41</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">42</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">43</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">43</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">44</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">45</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">46</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">47</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">48</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">48</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">49</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">50</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">51</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">52</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">53</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">54</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">55</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">56</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">57</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">58</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">59</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">60</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">61</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">62</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">63</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">64</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">65</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">66</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">68</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">69</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">70</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">71</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">73</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">74</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">75</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">76</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">78</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">79</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">81</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">82</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">83</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">85</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">86</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">88</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">90</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">91</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">93</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">94</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">96</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">98</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">99</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000dd;">101</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">103</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">105</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">107</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">109</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">110</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">112</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">114</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">116</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">118</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">121</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">123</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">125</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">127</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">129</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">132</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">134</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">136</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">139</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">141</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">144</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">146</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">149</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">151</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">154</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">157</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">159</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">162</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">165</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">168</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">171</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">174</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">177</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">180</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">183</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">186</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">190</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">193</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">196</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">200</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">203</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">207</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">211</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">214</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">218</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">222</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">226</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">230</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">234</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">238</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">242</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">248</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">255</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> sensorPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #666666; font-style: italic;">// pin the potmeter is attached too</span><br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> ledPinR &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">9</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #666666; font-style: italic;">// pwm pin with red led</span><br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> ledPinG &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// pwm pin with green led</span><br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> ledPinB &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">11</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// pwm pin with blue led</span><br />
<br />
<span style="color: #993333;">int</span> sensorVal <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// store the value coming from the sensor</span><br />
<span style="color: #993333;">int</span> fadeVal &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// value that changes between 0-255</span><br />
<span style="color: #993333;">int</span> fadeSpeed <span style="color: #339933;">=</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 'speed' of fading</span><br />
<br />
<span style="color: #666666; font-style: italic;">// getRGB function stores RGB values in this array</span><br />
<span style="color: #666666; font-style: italic;">// use these values for the red, blue, green led. </span><br />
<span style="color: #993333;">int</span> rgb_colors<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <br />
<br />
<span style="color: #993333;">int</span> hue<span style="color: #339933;">;</span><br />
<span style="color: #993333;">int</span> saturation<span style="color: #339933;">;</span><br />
<span style="color: #993333;">int</span> brightness<span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; <span style="color: #000066;">pinMode</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">9</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">OUTPUT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066;">pinMode</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">10</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">OUTPUT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066;">pinMode</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">11</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">OUTPUT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">57600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #009900;">&#123;</span> <br />
<br />
&nbsp; sensorVal <span style="color: #339933;">=</span> <span style="color: #000066;">analogRead</span><span style="color: #009900;">&#40;</span>sensorPin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// fade from 0 - 255 and back with a certain speed &nbsp; </span><br />
&nbsp; fadeVal <span style="color: #339933;">=</span> fadeVal <span style="color: #339933;">+</span> fadeSpeed<span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// change fadeVal by speed</span><br />
&nbsp; fadeVal <span style="color: #339933;">=</span> <span style="color: #000066;">constrain</span><span style="color: #009900;">&#40;</span>fadeVal<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #666666; font-style: italic;">// keep fadeVal between 0 and 255</span><br />
<br />
&nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>fadeVal<span style="color: #339933;">==</span><span style="color: #0000dd;">255</span> <span style="color: #339933;">||</span> fadeVal<span style="color: #339933;">==</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// change from up&gt;down or down-up (negative/positive)</span><br />
&nbsp; <span style="color: #009900;">&#123;</span> fadeSpeed <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>fadeSpeed<span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// set HSB values</span><br />
&nbsp; hue &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #000066;">map</span><span style="color: #009900;">&#40;</span>sensorVal<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1023</span><span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">359</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// hue is a number between 0 and 360</span><br />
&nbsp; saturation <span style="color: #339933;">=</span> <span style="color: #0000dd;">255</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// saturation is a number between 0 - 255</span><br />
&nbsp; brightness <span style="color: #339933;">=</span> fadeVal<span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// value is a number between 0 - 255</span><br />
<br />
&nbsp; getRGB<span style="color: #009900;">&#40;</span>hue<span style="color: #339933;">,</span>saturation<span style="color: #339933;">,</span>brightness<span style="color: #339933;">,</span>rgb_colors<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <span style="color: #666666; font-style: italic;">// converts HSB to RGB</span><br />
<br />
&nbsp; <span style="color: #000066;">analogWrite</span><span style="color: #009900;">&#40;</span>ledPinR<span style="color: #339933;">,</span> rgb_colors<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// red value in index 0 of rgb_colors array</span><br />
&nbsp; <span style="color: #000066;">analogWrite</span><span style="color: #009900;">&#40;</span>ledPinG<span style="color: #339933;">,</span> rgb_colors<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// green value in index 1 of rgb_colors array</span><br />
&nbsp; <span style="color: #000066;">analogWrite</span><span style="color: #009900;">&#40;</span>ledPinB<span style="color: #339933;">,</span> rgb_colors<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// blue value in index 2 of rgb_colors array</span><br />
<br />
&nbsp; <span style="color: #000066;">delay</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// delay to slow down fading</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> getRGB<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> hue<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> sat<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> val<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> colors<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; <span style="color: #808080; font-style: italic;">/* convert hue, saturation and brightness ( HSB/HSV ) to RGB<br />
&nbsp; &nbsp; &nbsp;The dim_curve is used only on brightness/value and on saturation (inverted).<br />
&nbsp; &nbsp; &nbsp;This looks the most natural. &nbsp; &nbsp; &nbsp;<br />
&nbsp; */</span><br />
<br />
&nbsp; val <span style="color: #339933;">=</span> dim_curve<span style="color: #009900;">&#91;</span>val<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; sat <span style="color: #339933;">=</span> <span style="color: #0000dd;">255</span><span style="color: #339933;">-</span>dim_curve<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">255</span><span style="color: #339933;">-</span>sat<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #993333;">int</span> r<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">int</span> g<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">int</span> b<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">int</span> base<span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>sat <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Acromatic color (gray). Hue doesn't mind.</span><br />
&nbsp; &nbsp; colors<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>val<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; colors<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>val<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; colors<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>val<span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> &nbsp;<span style="color: #009900;">&#123;</span> <br />
<br />
&nbsp; &nbsp; base <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">255</span> <span style="color: #339933;">-</span> sat<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> val<span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;&gt;</span><span style="color: #0000dd;">8</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>hue<span style="color: #339933;">/</span><span style="color: #0000dd;">60</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; r <span style="color: #339933;">=</span> val<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; g <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>val<span style="color: #339933;">-</span>base<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>hue<span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000dd;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>base<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; b <span style="color: #339933;">=</span> base<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; r <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>val<span style="color: #339933;">-</span>base<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">60</span><span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>hue<span style="color: #339933;">%</span><span style="color:#800080;">60</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000dd;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>base<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; g <span style="color: #339933;">=</span> val<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; b <span style="color: #339933;">=</span> base<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; r <span style="color: #339933;">=</span> base<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; g <span style="color: #339933;">=</span> val<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; b <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>val<span style="color: #339933;">-</span>base<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>hue<span style="color: #339933;">%</span><span style="color:#800080;">60</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000dd;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>base<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; r <span style="color: #339933;">=</span> base<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; g <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>val<span style="color: #339933;">-</span>base<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">60</span><span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>hue<span style="color: #339933;">%</span><span style="color:#800080;">60</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000dd;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>base<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; b <span style="color: #339933;">=</span> val<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; r <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>val<span style="color: #339933;">-</span>base<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>hue<span style="color: #339933;">%</span><span style="color:#800080;">60</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000dd;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>base<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; g <span style="color: #339933;">=</span> base<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; b <span style="color: #339933;">=</span> val<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">5</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; r <span style="color: #339933;">=</span> val<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; g <span style="color: #339933;">=</span> base<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; b <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>val<span style="color: #339933;">-</span>base<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">60</span><span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>hue<span style="color: #339933;">%</span><span style="color:#800080;">60</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000dd;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>base<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; colors<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>r<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; colors<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>g<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; colors<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>b<span style="color: #339933;">;</span> <br />
&nbsp; <span style="color: #009900;">&#125;</span> &nbsp; <br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</pre>


<p>Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-state-change/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; State change'>Arduino Programming &#8211; State change</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-interval/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; Interval'>Arduino Programming &#8211; Interval</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.kasperkamperman.com/blog/arduino/arduino-programming-hsb-to-rgb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arduino Programming &#8211; Map and smooth values</title>
		<link>http://www.kasperkamperman.com/blog/arduino/arduino-programming-map-and-smooth/</link>
		<comments>http://www.kasperkamperman.com/blog/arduino/arduino-programming-map-and-smooth/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 00:40:58 +0000</pubDate>
		<dc:creator>kasperkamperman</dc:creator>
				<category><![CDATA[Arduino]]></category>

		<guid isPermaLink="false">http://www.kasperkamperman.com/?p=619</guid>
		<description><![CDATA[Re-map a value from one range to another and smooth analog input values. 


Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-hsb-to-rgb/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; HSB to RGB'>Arduino Programming &#8211; HSB to RGB</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-state-change/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; State change'>Arduino Programming &#8211; State change</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Re-map a value from one range to another and smooth analog input values. </p>
<p>Level : beginner with Arduino. ( Basic knowledge of programming principles like if/else and variables  ). </p>
<p>Download the Arduino ( .pde ) files : <a class="download" href='http://www.kasperkamperman.com/wordpress_kk/wordpress_uploads/2010/01/arduino_tutorial_map-and-smooth.zip'>arduino_tutorial_map-and-smooth.zip</a></p>
<p>Map.pde :</p>
<pre>
<div class="codecolorer-container arduino default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br /></div></td><td><div class="arduino codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/* <br />
&nbsp; Mapping example<br />
<br />
&nbsp; Re-map a number from one range to another. <br />
<br />
&nbsp; created 01-12-2009 by kasperkamperman.com<br />
*/</span><br />
<br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> sensorPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// the pin that the potmeter is attached to</span><br />
<span style="color: #993333;">int</span> sensorValue &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// store the value coming from the sensor</span><br />
<span style="color: #993333;">int</span> mappedValue<span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// store the new value</span><br />
<br />
<span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// opens serial port, sets data rate to 57600 bits per second</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">57600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// read the value from the sensor</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// the range from an analog input is 0 - 1023</span><br />
&nbsp; sensorValue <span style="color: #339933;">=</span> <span style="color: #000066;">analogRead</span><span style="color: #009900;">&#40;</span>sensorPin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// print the value</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;sensorValue &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>sensorValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// map(value, fromLow, fromHigh, toLow, toHigh)</span><br />
&nbsp; mappedValue <span style="color: #339933;">=</span> <span style="color: #000066;">map</span><span style="color: #009900;">&#40;</span>sensorValue<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1023</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;map to 0 - 255 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>mappedValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// invert a value is possible too </span><br />
&nbsp; mappedValue <span style="color: #339933;">=</span> <span style="color: #000066;">map</span><span style="color: #009900;">&#40;</span>sensorValue<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1023</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">255</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;map to 255 - 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>mappedValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// or negative values</span><br />
&nbsp; mappedValue <span style="color: #339933;">=</span> <span style="color: #000066;">map</span><span style="color: #009900;">&#40;</span>sensorValue<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1023</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">127</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">128</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;map to -127 - 128 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>mappedValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// only watch a certain part of the sensor values</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// for example when using a sensor that doesn't cover the whole</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// range</span><br />
&nbsp; mappedValue <span style="color: #339933;">=</span> <span style="color: #000066;">map</span><span style="color: #009900;">&#40;</span>sensorValue<span style="color: #339933;">,</span> <span style="color: #0000dd;">512</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">712</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;map 512 - 712 to 0 - 100 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>mappedValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// constraining the sensorValue can be usefull to prevent negative values</span><br />
&nbsp; sensorValue <span style="color: #339933;">=</span> <span style="color: #000066;">constrain</span><span style="color: #009900;">&#40;</span>sensorValue<span style="color: #339933;">,</span> <span style="color: #0000dd;">512</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">712</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;constrained sensorValue &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>sensorValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; mappedValue <span style="color: #339933;">=</span> <span style="color: #000066;">map</span><span style="color: #009900;">&#40;</span>sensorValue<span style="color: #339933;">,</span> <span style="color: #0000dd;">512</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">712</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;map 512 - 712 to 0 - 100 ( with constrain ) : &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>mappedValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// division between next read</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;------------------------------------------------&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// wait for 1000 milliseconds ( 1 second ) to read every second</span><br />
&nbsp; <span style="color: #000066;">delay</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</pre>
<p>Analog_smooth.pde :</p>
<pre>
<div class="codecolorer-container arduino default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br /></div></td><td><div class="arduino codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/* <br />
&nbsp; Smoothing example<br />
<br />
&nbsp; Read the analog input and smooth out the result by averaging<br />
&nbsp; the result with past values of the input.<br />
<br />
&nbsp; The more samples to longer it takes to reach the final value. <br />
&nbsp; 4/5 samples give a smooth and reactive enough result. <br />
<br />
&nbsp; Simpler algorithm than the smoothing included with the Arduino <br />
&nbsp; examples.<br />
<br />
&nbsp; based on : Analog Smoothing Algorithm by Tom Igoe <br />
&nbsp; &lt;http://www.tigoe.net/pcomp/code/category/arduinowiring/41&gt;<br />
<br />
&nbsp; created 05-01-2010 by kasperkamperman.com<br />
*/</span><br />
<br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> sensorPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// the pin that the potmeter is attached to</span><br />
<br />
<span style="color: #993333;">int</span> sensorVal &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// store the value coming from the sensor</span><br />
<span style="color: #993333;">int</span> smoothedVal &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// smoothed result</span><br />
<span style="color: #993333;">int</span> samples &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// amount of samples</span><br />
<br />
<span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">57600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// read the value from the sensor</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// the range from an analog input is 0 - 1023</span><br />
&nbsp; sensorVal <span style="color: #339933;">=</span> <span style="color: #000066;">analogRead</span><span style="color: #009900;">&#40;</span>sensorPin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<br />
&nbsp; smoothedVal <span style="color: #339933;">=</span> smoothedVal <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>sensorVal <span style="color: #339933;">-</span> smoothedVal<span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span>samples<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// print the original value and the smoothedValue</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>sensorVal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;, &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>smoothedVal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// slow down amount of readings and prints</span><br />
&nbsp; <span style="color: #000066;">delay</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</pre>


<p>Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-hsb-to-rgb/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; HSB to RGB'>Arduino Programming &#8211; HSB to RGB</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-state-change/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; State change'>Arduino Programming &#8211; State change</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.kasperkamperman.com/blog/arduino/arduino-programming-map-and-smooth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arduino Programming &#8211; Interval</title>
		<link>http://www.kasperkamperman.com/blog/arduino/arduino-programming-interval/</link>
		<comments>http://www.kasperkamperman.com/blog/arduino/arduino-programming-interval/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 23:04:18 +0000</pubDate>
		<dc:creator>kasperkamperman</dc:creator>
				<category><![CDATA[Arduino]]></category>

		<guid isPermaLink="false">http://www.kasperkamperman.com/blog/arduino-programming-interval/</guid>
		<description><![CDATA[How to write a program that fires actions with different intervals without using the delay() function. Configuration of the serial port and printing  values is also explained. 


Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-map-and-smooth/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; Map and smooth values'>Arduino Programming &#8211; Map and smooth values</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino-flash-communication-as3-messenger/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS3 &#8211; Messenger'>Arduino Flash communication AS3 &#8211; Messenger</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.kasperkamperman.com/blog/arduino/arduino-programming-interval/"><em>Click here to view the embedded video.</em></a></p>
<p>How to write a program that fires actions with different intervals without using the delay() function. Configuration of the serial port and printing  values is also explained. </p>
<p>Level : beginner with Arduino. ( Basic knowledge of programming principles like if/else and variables  ). </p>
<p>Download the Arduino ( .pde ) files : <a class="download" href='http://www.kasperkamperman.com/wordpress_kk/wordpress_uploads/2009/12/arduino_tutorial_interval.zip'>arduino_tutorial_interval.zip</a></p>
<p>Interval_basic.pde : </p>
<pre>
<div class="codecolorer-container arduino default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br /></div></td><td><div class="arduino codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/*<br />
&nbsp; Print serial text with an interval.<br />
&nbsp; Basic example. &nbsp; <br />
&nbsp; <br />
&nbsp; created 01-12-2009 by kasperkamperman.com<br />
*/</span><br />
<br />
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> previousMillis &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> currentMillis &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">int</span> interval <span style="color: #339933;">=</span> <span style="color: #0000dd;">1000</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// opens serial port, sets data rate to 57600 bits per second</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">57600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>&nbsp; &nbsp;<br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// save number of milliseconds since the program started</span><br />
&nbsp; currentMillis <span style="color: #339933;">=</span> <span style="color: #000066;">millis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #666666; font-style: italic;">// check to see if the interval time is passed. </span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>currentMillis <span style="color: #339933;">-</span> previousMillis <span style="color: #339933;">&gt;=</span> interval <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;this program runs now for : &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>currentMillis<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; milliseconds&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// save the time when we displayed the string for the last time</span><br />
&nbsp; &nbsp; &nbsp; previousMillis <span style="color: #339933;">=</span> currentMillis<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</pre>
<p><br/>
Interval_for_loop.pde :<br/></p>
<pre>
<div class="codecolorer-container arduino default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br /></div></td><td><div class="arduino codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/*<br />
&nbsp; Print serial text with different intervals. &nbsp; <br />
&nbsp; Check the array with a for-loop.<br />
&nbsp; <br />
&nbsp; created 01-12-2009 by kasperkamperman.com<br />
*/</span><br />
<br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> amountOfIntervals <span style="color: #339933;">=</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> previousMillis<span style="color: #009900;">&#91;</span>amountOfIntervals<span style="color: #009900;">&#93;</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> currentMillis &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">int</span> intervals<span style="color: #009900;">&#91;</span>amountOfIntervals<span style="color: #009900;">&#93;</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000dd;">1000</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2500</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">5000</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// opens serial port, sets data rate to 57600 bits per second</span><br />
&nbsp; <span style="color: #000066;">Serial</span>.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">57600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>&nbsp; &nbsp;<br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// save number of milliseconds since the program started</span><br />
&nbsp; currentMillis <span style="color: #339933;">=</span> <span style="color: #000066;">millis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #666666; font-style: italic;">// check the 3 intervals one by one with a for-loop. </span><br />
&nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>amountOfIntervals<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// check to see if the interval time is passed. &nbsp;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>currentMillis <span style="color: #339933;">-</span> previousMillis<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;=</span> intervals<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">==</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000066;">Serial</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;- &nbsp; every 1 second&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">==</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #000066;">Serial</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; - &nbsp;every 2.5 second&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">==</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #000066;">Serial</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; &nbsp;- every 5 seconds&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// save the time when we displayed the string for the last time</span><br />
&nbsp; &nbsp; &nbsp; previousMillis<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> currentMillis<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</pre>


<p>Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-map-and-smooth/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; Map and smooth values'>Arduino Programming &#8211; Map and smooth values</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino-flash-communication-as3-messenger/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS3 &#8211; Messenger'>Arduino Flash communication AS3 &#8211; Messenger</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.kasperkamperman.com/blog/arduino/arduino-programming-interval/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arduino Programming &#8211; State change</title>
		<link>http://www.kasperkamperman.com/blog/arduino/arduino-programming-state-change/</link>
		<comments>http://www.kasperkamperman.com/blog/arduino/arduino-programming-state-change/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 23:01:46 +0000</pubDate>
		<dc:creator>kasperkamperman</dc:creator>
				<category><![CDATA[Arduino]]></category>

		<guid isPermaLink="false">http://www.kasperkamperman.com/blog/arduino/arduino-programming-state-change/</guid>
		<description><![CDATA[How to program a state change functionality. 'State change detection' is a method to see when a button is pressed or released. You can use it to fire a different action each press of a button. 
In this tutorial the basic code structure ( setup, loop ) of Arduino script is also explained.


Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-hsb-to-rgb/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; HSB to RGB'>Arduino Programming &#8211; HSB to RGB</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-map-and-smooth/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; Map and smooth values'>Arduino Programming &#8211; Map and smooth values</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.kasperkamperman.com/blog/arduino/arduino-programming-state-change/"><em>Click here to view the embedded video.</em></a></p>
<p>How to program a state change functionality. &#8216;State change detection&#8217; is a method to see when a button is pressed or released. You can use it to fire a different action each press of a button.</p>
<p>For example the &#8220;Play/Pause&#8221; button on a cd-player behaves like that. The first press is play, the next press is pause, the next play again etc. Depending the state the action is different ( play &gt; pause, pause &gt; play ).</p>
<p>In this tutorial I explain also the basic code structure ( setup, loop ) of Arduino script.</p>
<p>Level : beginner with Arduino. ( Basic knowledge of programming principles like if/else and variables  ).</p>
<p>Download the Arduino ( .pde ) files :
<a class="download" href="http://www.kasperkamperman.com/wordpress_kk/wordpress_uploads/2009/12/arduino_tutorial_statechange.zip">arduino_tutorial_statechange.zip</a></p>
<p>OnOff.pde :</p>
<pre>
<div class="codecolorer-container arduino default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br /></div></td><td><div class="arduino codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/*<br />
&nbsp; LED on when you press a button.<br />
&nbsp; LED off when you release a button. &nbsp;<br />
&nbsp; <br />
&nbsp; created 01-12-2009 by kasperkamperman.com<br />
*/</span><br />
<br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> buttonPin <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// the pin that the pushbutton is attached to</span><br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> ledPin &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000dd;">13</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// the pin that the LED is attached to</span><br />
<br />
<span style="color: #993333;">int</span> buttonState &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// current state of the button</span><br />
<br />
<span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066;">pinMode</span><span style="color: #009900;">&#40;</span>buttonPin<span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">INPUT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #666666; font-style: italic;">// initialize the button pin as a input</span><br />
&nbsp; <span style="color: #000066;">pinMode</span><span style="color: #009900;">&#40;</span>ledPin<span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">OUTPUT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// initialize the button pin as a output</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// read the pushbutton input pin</span><br />
&nbsp; buttonState <span style="color: #339933;">=</span> <span style="color: #000066;">digitalRead</span><span style="color: #009900;">&#40;</span>buttonPin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// turns LED on if the buttonState=HIGH or off if the buttonState=LOW</span><br />
&nbsp; <span style="color: #000066;">digitalWrite</span><span style="color: #009900;">&#40;</span>ledPin<span style="color: #339933;">,</span> buttonState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</pre>
<p>State_change.pde :</p>
<pre>
<div class="codecolorer-container arduino default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br /></div></td><td><div class="arduino codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/*<br />
&nbsp; Set a state of a variable when you press a pushbutton ( the <br />
&nbsp; button went from off to on ). <br />
&nbsp; <br />
&nbsp; created 01-12-2009 by kasperkamperman.com<br />
&nbsp; based on example 'State change detection' by Tom Igoe<br />
*/</span><br />
<br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> buttonPin &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// the pin that the pushbutton is attached to</span><br />
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> ledPin &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">13</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// the pin that the LED is attached to</span><br />
<br />
<span style="color: #993333;">int</span> buttonState &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// current state of the button</span><br />
<span style="color: #993333;">int</span> lastButtonState &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// previous state of the button</span><br />
<span style="color: #993333;">int</span> ledState &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// remember current led state</span><br />
<br />
<span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066;">pinMode</span><span style="color: #009900;">&#40;</span>buttonPin<span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">INPUT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #666666; font-style: italic;">// initialize the button pin as a input</span><br />
&nbsp; <span style="color: #000066;">pinMode</span><span style="color: #009900;">&#40;</span>ledPin<span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">OUTPUT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// initialize the button pin as a output</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// read the pushbutton input pin</span><br />
&nbsp; buttonState <span style="color: #339933;">=</span> <span style="color: #000066;">digitalRead</span><span style="color: #009900;">&#40;</span>buttonPin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// check if the button is pressed or released</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// by comparing the buttonState to its previous state </span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>buttonState <span style="color: #339933;">!=</span> lastButtonState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// change the state of the led when someone pressed the button</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>buttonState <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>ledState<span style="color: #339933;">==</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> ledState<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ledState<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// remember the current state of the button</span><br />
&nbsp; &nbsp; lastButtonState <span style="color: #339933;">=</span> buttonState<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #666666; font-style: italic;">// turns LED on if the ledState=1 or off if the ledState=0</span><br />
&nbsp; <span style="color: #000066;">digitalWrite</span><span style="color: #009900;">&#40;</span>ledPin<span style="color: #339933;">,</span> ledState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #666666; font-style: italic;">// adding a small delay prevents reading the buttonState to fast</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// ( debouncing )</span><br />
&nbsp; <span style="color: #000066;">delay</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</pre>
<p>If you like to work with more than two states, do something like below. And use a switch or if statement later to perform some action on a certain stateNum value. </p>
<pre>
<div class="codecolorer-container arduino default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="arduino codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>buttonState <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; stateNum<span style="color: #339933;">++;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>stateNum<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> stateNum<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp;<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</pre>


<p>Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-hsb-to-rgb/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; HSB to RGB'>Arduino Programming &#8211; HSB to RGB</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-programming-map-and-smooth/' rel='bookmark' title='Permanent Link: Arduino Programming &#8211; Map and smooth values'>Arduino Programming &#8211; Map and smooth values</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.kasperkamperman.com/blog/arduino/arduino-programming-state-change/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arduino Flash communication AS2</title>
		<link>http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication/</link>
		<comments>http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 09:50:54 +0000</pubDate>
		<dc:creator>kasperkamperman</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[arduino flash communition]]></category>
		<category><![CDATA[art & technology]]></category>
		<category><![CDATA[as2]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[kunst & techniek]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[playground]]></category>
		<category><![CDATA[saxion]]></category>
		<category><![CDATA[serial proxy]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://127.0.0.1/?p=60</guid>
		<description><![CDATA[In this video tutorial I explain how to setup communication between the Arduino microcontroller board and Flash ( Actionscript 2). Example files are included.


Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication-as3/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS3 &#8211; As3Glue bundle'>Arduino Flash communication AS3 &#8211; As3Glue bundle</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino-flash-communication-as3-messenger/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS3 &#8211; Messenger'>Arduino Flash communication AS3 &#8211; Messenger</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<object width="554" height="312"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=1907962&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=1907962&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=0d616c&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="554" height="312"></embed></object>
<p>Tutorial that explains how to setup communication between Arduino and Flash with provided example files. </p>
<p>Update : See <a href="http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication-as3/">Arduino Flash communication AS3 &#8211; As3Glue bundle</a> for an Actionscript 3 tutorial. </p>
<p>You can read sensors connected to the Arduino in Flash and send output information to actuators connected to the Arduino. Communication between Flash and Arduino is setup with the Serialproxy program that converts serial strings in to XML socket data.</p>
<p>The zip package can be download from :
<a href="http://arduino.cc/playground/uploads/Interfacing/flash_arduino_io_2.zip">arduino.cc/playground/uploads/Interfacing/flash_arduino_io_2.zip</a></p>
<p><strong>UPDATE :</strong>
<em>Serialproxy doesn&#8217;t work anymore under Snow Leopard ( osx 10.6 ) without installing Rosetta.
However you can also use Tinkerproxy2. That works in the same way as Serialproxy.
Just rename your serproxy.cfg to tinkerproxy2.cfg or you can rename the program tinkerproxy2 to serproxy.</em>
<a href="http://code.google.com/p/tinkerit/downloads/detail?name=tinkerproxy-2_0.zip">tinkerproxy-2_0.zip</a></p>
<p>See the Playground in Arduino for more Flash implementations :
<a href="http://arduino.cc/playground/Interfacing/Flash/">arduino.cc/playground/Interfacing/Flash/</a></p>


<p>Related posts:<ol><li><a href='http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication-as3/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS3 &#8211; As3Glue bundle'>Arduino Flash communication AS3 &#8211; As3Glue bundle</a></li>
<li><a href='http://www.kasperkamperman.com/blog/arduino-flash-communication-as3-messenger/' rel='bookmark' title='Permanent Link: Arduino Flash communication AS3 &#8211; Messenger'>Arduino Flash communication AS3 &#8211; Messenger</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.kasperkamperman.com/blog/arduino/arduino-flash-communication/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
