<?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>(Baltimore) Harford Hackerspace &#187; Microcontroller</title>
	<atom:link href="http://baltimorehackerspace.com/tag/microcontroller/feed/" rel="self" type="application/rss+xml" />
	<link>http://baltimorehackerspace.com</link>
	<description>Maryland Electronics, Software, Science and Robotics Club</description>
	<lastBuildDate>Tue, 24 Jan 2012 20:46:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Netduino Début with Color Sensor</title>
		<link>http://baltimorehackerspace.com/2010/07/netduino-debut/</link>
		<comments>http://baltimorehackerspace.com/2010/07/netduino-debut/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 17:43:45 +0000</pubDate>
		<dc:creator>Squintz</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Microcontroller]]></category>
		<category><![CDATA[Netduino]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://harfordhackerspace.org/?p=905</guid>
		<description><![CDATA[Netduino Over the last few weeks Harford Hackerspace has had the pleasure of beta testing the Netduino. The Netduino is a development board with a form factor similar to the Arduino. Care was taken when designing the Netduino to ensure compatibility with most of the existing Arduino shields. That&#8217;s about where the similarities of the two devices [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_1020" class="wp-caption alignleft" style="width: 210px"><a href="http://harfordhackerspace.org/wp-content/uploads/2010/07/CW0001_3.jpg"><img src="http://harfordhackerspace.org/wp-content/uploads/2010/07/CW0001_3-300x218.jpg" alt="Netduino" title="Netduino" width="200" height="118" class="size-medium wp-image-1020" /></a><p class="wp-caption-text">Netduino</p></div><strong>Netduino</strong></p>
<p>Over the last few weeks Harford Hackerspace has had the pleasure of beta testing the <a title="Netduino" href="http://www.netduino.com" target="_blank">Netduino</a>. The Netduino is a development board with a form factor similar to the <a title="Arduino" href="http://www.arduino.cc/" target="_blank">Arduino</a>. Care was taken when designing the Netduino to ensure compatibility with most of the existing Arduino shields. That&#8217;s about where the similarities of the two devices ends.</p>
<p>The Netduino uses Microsoft&#8217;s Open Source .net Micro Framework SDK along with Visual Studio C# (or VS C# Express) as the primary development environment. C# application developers will be able to quickly adapt to the .net Micro Framework and start programming microcontrollers in a matter of minutes. However, this does not let them off the hook for learning the basics of electronics.<br />
<span id="more-905"></span><br />
After downloading and installing <a title="Visual Studio Express" href="http://www.microsoft.com/express/Windows/" target="_blank">Visual Studio C# Express 2010</a>, the <a title=".net Microframework 4.1 SDK" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=cff5a7b7-c21c-4127-ac65-5516384da3a0&amp;displayLang=en" target="_blank">.net Micro Framework 4.1 SDK</a> , and the Secret Labs Netduino SDK we started with the &#8220;Hello World&#8221; of microcontrollers and blinked the on-board LED. That was easy&#8230;! You&#8217;ll be able to get details to do this yourself from the Netduino website.</p>
<p><strong>SparkFun Color Sensor</strong><br />
<div id="attachment_909" class="wp-caption alignright" style="width: 247px"><a href="http://harfordhackerspace.org/wp-content/uploads/2010/07/08663-04-L.jpg"><img src="http://harfordhackerspace.org/wp-content/uploads/2010/07/08663-04-L-300x300.jpg" alt="Sparkfun Color Sensor" title="Sparkfun Color Sensor" width="227" height="227" class="size-medium wp-image-909" /></a><p class="wp-caption-text">ADJD-S371-Q999</p></div><br />
I purchased the ADJD-S371 color sensor(right) from SparkFun several months ago with the intention of connecting it to a PIC microcontroller and creating a device that will print the HTML color name (i.e. DarkRed) of an object that it sees. I&#8217;m particularly interested in this project because I happen to be Color Blind. There are several off the shelf products available but where is the fun in that?</p>
<p>The ADJD-S371 is a 4 channel RGB+Clear sensor and uses the 2-wire I2C protocol to communicate with the microcontroller.  It&#8217;s a complex little device that will test your puzzle solving skills. What I mean is that the device does not simply give you the correct color when you ask for it. You&#8217;ll have to play around with the Gain calibration algorithm and come up with something that works best for your application. However, for simple testing purposes you can use the code described in this tutorial. Here&#8217;s a quick video to demonstrate the project:</p>
<p><object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/uprZ1NBShAk&amp;hl=en_US&amp;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/uprZ1NBShAk&amp;hl=en_US&amp;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="545" height="385"></embed></object></p>
<p>The code was originally created by Nathan Seidle for the PIC microcontroller and was ported and modified for the Arduino by Marcus from <a title="Interactive Matter Tutorial" href="http://interactive-matter.org/2008/08/tinkering-with-adjd-s371-q999/" target="_blank">Interactive Matter</a>. Paul King and I ported it once again to the C# Language for the .net Micro Framework.</p>
<p><div id="attachment_1076" class="wp-caption alignright" style="width: 310px"><a href="http://harfordhackerspace.org/wp-content/uploads/2010/07/Colorimeter_Schematic_Image1.png"><img src="http://harfordhackerspace.org/wp-content/uploads/2010/07/Colorimeter_Schematic_Image1-300x204.png" alt="Colorimeter Schematic" title="Colorimeter Schematic" width="300" height="204" class="size-medium wp-image-1076" /></a><p class="wp-caption-text">Click to Enlarge</p></div>The schematic to the right shows how we hooked up the Netduino. There are a few things to note. First, we are using an external LED for our light source so there is no need to connect the LED pin on the ADJD-S371-Q999. Also, note that we skip a pin when we hooked up the RGB LED. This is because our protoshield was broke and that pin was not working. You can use any of the Digital IO Pins. You just need to change a little code when setting up the Output Ports. Finally, the RGB LED could be hooked to 3.3v or 5v. You may need to adjust the resistor sizes for your RGB LED anyways.</p>
<p><strong>C# Code</strong></p>
<div class="codecolorer-container csharp 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 /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.IO.Ports</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Microsoft.SPOT</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Microsoft.SPOT.Hardware</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">SecretLabs.NETMF.Hardware.Netduino</span><span style="color: #008000;">;</span></div></td></tr></tbody></table></div>
<p>Lines 1 through 6 define a namespace. This allows you to use classes or types without having to type out the full namespace where those types exist. For example, instead of typing:<br />
<code class="codecolorer csharp default"><span class="csharp">SecretLabs<span style="color: #008000;">.</span><span style="color: #0000FF;">NETMF</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Hardware</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Netduino</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Pins</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ONBOARD_LED</span></span></code><br />
We can simply type:<br />
<code class="codecolorer csharp default"><span class="csharp">Pins<span style="color: #008000;">.</span><span style="color: #0000FF;">ONBOARD_LED</span></span></code></p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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>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 /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">namespace</span> Squintz<span style="color: #008000;">.</span><span style="color: #0000FF;">Colorimeter</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Program<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> I2C_ADDRESS <span style="color: #008000;">=</span> 0x74<span style="color: #008000;">;</span> &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// 7bit</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_CAP_RED &nbsp; &nbsp; &nbsp;<span style="color: #008000;">=</span>0x06<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_CAP_GREEN &nbsp; &nbsp;<span style="color: #008000;">=</span>0x07<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_CAP_BLUE &nbsp; &nbsp; <span style="color: #008000;">=</span>0x08<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_CAP_CLEAR &nbsp; &nbsp;<span style="color: #008000;">=</span>0x09<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_INT_RED_LO &nbsp; <span style="color: #008000;">=</span>0x0A<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_INT_RED_HI &nbsp; <span style="color: #008000;">=</span>0x0B<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_INT_GREEN_LO <span style="color: #008000;">=</span>0x0C<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_INT_GREEN_HI <span style="color: #008000;">=</span>0x0D<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_INT_BLUE_LO &nbsp;<span style="color: #008000;">=</span>0x0E<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_INT_BLUE_HI &nbsp;<span style="color: #008000;">=</span>0x0F<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_INT_CLEAR_LO <span style="color: #008000;">=</span>0x10<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_INT_CLEAR_HI <span style="color: #008000;">=</span>0x11<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_DATA_RED_LO &nbsp;<span style="color: #008000;">=</span>0x40<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_DATA_RED_HI &nbsp;<span style="color: #008000;">=</span>0x41<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_DATA_GREEN_LO<span style="color: #008000;">=</span>0x42<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_DATA_GREEN_HI<span style="color: #008000;">=</span>0x43<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_DATA_BLUE_LO <span style="color: #008000;">=</span>0x44<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_DATA_BLUE_HI <span style="color: #008000;">=</span>0x45<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_DATA_CLEAR_LO<span style="color: #008000;">=</span>0x46<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> REG_DATA_CLEAR_HI<span style="color: #008000;">=</span>0x47<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> I2CDevice rtc <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> I2CDevice<span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> I2CDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">Configuration</span><span style="color: #008000;">&#40;</span>I2C_ADDRESS, <span style="color: #FF0000;">100</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> readBuffer <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span></div></td></tr></tbody></table></div>
<p>Lines 7 through 33 are constants defined in the Color Sensors documentation. Line 35 creates a new I<sup>2</sup>C device object named rtc. We use this object to actually send the data from the Netduino to the Color Sensor. I<sup>2</sup>C is a bus protocol which means that multiple devices can share the same wires. So the I2CDevice object requires us to give it a slave address. We defined this as the constant I2C_Address on line 11. The I2CDevice object also requires us to specify a rate in Kilohertz(Khz). 100Khz seemed to do that trick. We tried other speeds without much luck.</p>
<p>Line 37 creates a variable of type byte array. Note that we are only initializing the size of the array to 1. The color  sensor does not actually need an array of bytes but the I2CDevice.CreateReadTransaction requires a byte array as one of its parameters and does not have an overloaded method for using just a byte.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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>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 /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OutputPort redpin <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> OutputPort<span style="color: #008000;">&#40;</span>Pins<span style="color: #008000;">.</span><span style="color: #0000FF;">GPIO_PIN_D10</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OutputPort greenpin <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> OutputPort<span style="color: #008000;">&#40;</span>Pins<span style="color: #008000;">.</span><span style="color: #0000FF;">GPIO_PIN_D9</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OutputPort bluepin <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> OutputPort<span style="color: #008000;">&#40;</span>Pins<span style="color: #008000;">.</span><span style="color: #0000FF;">GPIO_PIN_D7</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> cc <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> red <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> green <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> blue <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> clearGain<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> colorGain<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> num_samples<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> i<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clearGain <span style="color: #008000;">=</span> getClearGain<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set_gain<span style="color: #008000;">&#40;</span>REG_INT_CLEAR_LO, clearGain<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colorGain <span style="color: #008000;">=</span> getColorGain<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set_gain<span style="color: #008000;">&#40;</span>REG_INT_RED_LO, colorGain<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set_gain<span style="color: #008000;">&#40;</span>REG_INT_GREEN_LO, colorGain<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set_gain<span style="color: #008000;">&#40;</span>REG_INT_BLUE_LO, colorGain<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_samples <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span>i<span style="color: #008000;">=</span><span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i<span style="color: #008000;">&lt;</span>num_samples <span style="color: #008000;">;</span>i <span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Program<span style="color: #008000;">.</span><span style="color: #0000FF;">performMeasurement</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cc <span style="color: #008000;">+=</span>get_readout<span style="color: #008000;">&#40;</span>REG_DATA_CLEAR_LO<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; red <span style="color: #008000;">+=</span>get_readout<span style="color: #008000;">&#40;</span>REG_DATA_RED_LO<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; green <span style="color: #008000;">+=</span>get_readout<span style="color: #008000;">&#40;</span>REG_DATA_GREEN_LO<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blue <span style="color: #008000;">+=</span>get_readout<span style="color: #008000;">&#40;</span>REG_DATA_BLUE_LO<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cc<span style="color: #008000;">/=</span>num_samples<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; red <span style="color: #008000;">/=</span>num_samples<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; green <span style="color: #008000;">/=</span>num_samples<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blue <span style="color: #008000;">/=</span>num_samples<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; redpin<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; greenpin<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bluepin<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>red <span style="color: #008000;">&gt;</span> green <span style="color: #008000;">&amp;&amp;</span> red <span style="color: #008000;">&gt;</span> blue<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; redpin<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>green <span style="color: #008000;">&gt;</span> red <span style="color: #008000;">&amp;&amp;</span> green <span style="color: #008000;">&gt;</span> blue<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; greenpin<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>blue <span style="color: #008000;">&gt;</span> red <span style="color: #008000;">&amp;&amp;</span> blue <span style="color: #008000;">&gt;</span> green<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bluepin<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug<span style="color: #008000;">.</span><span style="color: #0000FF;">Print</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;C: &quot;</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>cc <span style="color: #008000;">&gt;&gt;</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;R:&quot;</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>red <span style="color: #008000;">&gt;&gt;</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;G:&quot;</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>green <span style="color: #008000;">&gt;&gt;</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;B:&quot;</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>blue <span style="color: #008000;">&gt;&gt;</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread<span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">500</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Lines 38 is the main entry point for our program. On lines 40 through 42 we setup 3 pins which we are using to control an RGB LED. By adding the SecretLabs.NETMF.Hardware.Netduino.dll to our list of resources and declaring it with the using statement on line 6 we don&#8217;t have to remember the actual microcontrollers pin numbers. SecretLabs has already looked all this up for us and they allow us to use Pins.xxxxx. You notice that the intellisense aka code complete feature of Visual Studio automatically pops up a context menu allowing you to select from a list of available pins.</p>
<p><span style="color: #1F0000;"><em>[*UPDATE: If you use the SecretLabs SDK Installer you can start a new "Netduino Application" which removes the need for manually adding the dll's to your resource list and also removes the need to manually add the SecretLabs using statements.]</em></span></p>
<p>On line 56 we create an infinite loop so that our program never ends. Without this our program would only run once and then terminate.</p>
<p>Lines 58 through 64 is where you could get creative. The <a href="http://www.sparkfun.com/datasheets/Sensors/Imaging/AV02-0314EN.pdf">color sensors datasheet</a> and <a href="http://www.sparkfun.com/datasheets/Sensors/Imaging/AV02-0359EN.pdf">application note</a> are a little fuzzy on explaining the proper way to calibrate and set the gain of your sensor. However, you <strong>MUST</strong> set the gain for each color before you request the color. If you fail to set the gain then you will get low results. We learned this the hard way. We just could not figure out why we only got numbers from 0 to 3!</p>
<p>Lines 65 through 78 is where we sample the colors through the use of a few helper methods which you&#8217;ll read about below. The key thing to note here is that the more samples you take the more stable your numbers will appear. We are taking 10 samples and adding the results together and dividing by 10 to get the average value. </p>
<p>Lines 80 through 93 is where we set the color of our RGB LED. We are using a very simple algorithm to determine the most dominate color and turning on just that leg of the RGB LED. We experimented with mixing colors but our LED was not diffused enough to mix the colors together. We were able to see the individual colors which did not have a good effect.</p>
<p>On Line 95 we call Debug.Print and output the values to Visual Studios output window. The clear value indicates an overall light intensity which the Red, Green, and Blue values are the intensity as seen through a light filter.</p>
<div class="codecolorer-container csharp 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>99<br />100<br />101<br />102<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> performMeasurement<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set_register<span style="color: #008000;">&#40;</span>0x00,0x01<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// start sensing</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now we start getting deep into the color sensors protocol. To tell the color sensor to take a sample we set the register 0&#215;00 to the value of 0&#215;01. The color sensor then takes a sample and stores it.</p>
<div class="codecolorer-container csharp 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>103<br />104<br />105<br />106<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">int</span> get_readout<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span> readRegister<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> read_register<span style="color: #008000;">&#40;</span>readRegister<span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span> read_register<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>readRegister <span style="color: #008000;">+</span> 0x01<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&lt;&lt;</span><span style="color: #FF0000;">8</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>The color sensor stores its color values in two 8 bit registers. So. on lines 103 through 106, get_readout() takes the first register as a parameter and assumes the next register is just 1 more address above that register. So we read the value of the first register and then read the value of the second register and then shift the value of the second register 8 bits to the left leaving us with the full 16 bit integer. However, only 10 bits are actually put into the integer because the High registers only return the two least significant bits and the Low register returns 7 Bits.</p>
<div class="codecolorer-container csharp 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>107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> set_gain<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span> gainRegister, <span style="color: #6666cc; font-weight: bold;">int</span> gain<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>gain <span style="color: #008000;">&lt;</span><span style="color: #FF0000;">4096</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">byte</span> hi <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>gain <span style="color: #008000;">&gt;&gt;</span> <span style="color: #FF0000;">8</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">byte</span> lo <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>gain<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set_register<span style="color: #008000;">&#40;</span>gainRegister, lo<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set_register<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>gainRegister<span style="color: #008000;">+</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>, hi<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Each color has two of its own gain registers. We defined these in our constants in the first few lines. Here we again assume that since we know the register address of the lower register, we can just add 1 to that address to get the higher register address. Then we assign the gain values through the help of set_register().</p>
<div class="codecolorer-container csharp 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>118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> set_register<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span> r, <span style="color: #6666cc; font-weight: bold;">byte</span> v<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rtc<span style="color: #008000;">.</span><span style="color: #0000FF;">Execute</span><span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> I2CDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">I2CTransaction</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; I2CDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateWriteTransaction</span><span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span>r, v<span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">&#125;</span>, <span style="color: #FF0000;">5000</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>set_register is where the magic happens. Here we create a new byte array which contains two values. The first is r for register and the second is v for value. We pass this byte array to a new I2CDevice.CreateWriteTransaction object and then pass that object to the rtc.Execute method. You should recall that rtc is our I2CDevice object which knows about the slave address of our color sensor. So, in a nutshell our set_register function is where we package everything up and send it on its way.</p>
<div class="codecolorer-container csharp 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>127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span> read_register<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span> r<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rtc<span style="color: #008000;">.</span><span style="color: #0000FF;">Execute</span><span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> I2CDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">I2CTransaction</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; I2CDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateWriteTransaction</span><span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span>r<span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; I2CDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateReadTransaction</span><span style="color: #008000;">&#40;</span>readBuffer<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span>, <span style="color: #FF0000;">5000</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> readBuffer<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>On Lines 127 through 136 we are sending a request for our color values. We pass it &#8216;r&#8217; which is the color we want to read. Remember that this happens twice for each color in order to get the full value. We wait 5000 milliseconds or 5 seconds for the return before we timeout.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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>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 />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">int</span> getClearGain<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> gainFound <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> upperBox <span style="color: #008000;">=</span> <span style="color: #FF0000;">4096</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> lowerBox <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> half <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> halfValue<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>gainFound <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; half <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>upperBox <span style="color: #008000;">-</span> lowerBox<span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> lowerBox<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//no further halfing possbile</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>half <span style="color: #008000;">==</span> lowerBox<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gainFound <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set_gain<span style="color: #008000;">&#40;</span>REG_INT_CLEAR_LO, half<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; performMeasurement<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; halfValue <span style="color: #008000;">=</span> get_readout<span style="color: #008000;">&#40;</span>REG_DATA_CLEAR_LO<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>halfValue <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1000</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upperBox <span style="color: #008000;">=</span> half<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>halfValue <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">1000</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lowerBox <span style="color: #008000;">=</span> half<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gainFound <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> half<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Lines 137 to 174 is where you can again play around to perfect your results. You&#8217;ll need to derive a way to obtain a gain value which compensates for the ambient light and all the other environmental factors which may change the sensitivity. I&#8217;ll be honest. I&#8217;m not too sure what is going on here. You should check out <a title="Interactive Matter Tutorial" href="http://interactive-matter.org/2008/08/tinkering-with-adjd-s371-q999/" target="_blank">Interactive Matter</a> for more information about this method.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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>175<br />176<br />177<br />178<br />179<br />180<br />181<br />182<br />183<br />184<br />185<br />186<br />187<br />188<br />189<br />190<br />191<br />192<br />193<br />194<br />195<br />196<br />197<br />198<br />199<br />200<br />201<br />202<br />203<br />204<br />205<br />206<br />207<br />208<br />209<br />210<br />211<br />212<br />213<br />214<br />215<br />216<br />217<br />218<br />219<br />220<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">int</span> getColorGain<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> gainFound <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> upperBox <span style="color: #008000;">=</span> <span style="color: #FF0000;">4096</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> lowerBox <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> half <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> halfValue<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>gainFound <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; half <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>upperBox <span style="color: #008000;">-</span> lowerBox<span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> lowerBox<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//no further halfing possbile</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>half <span style="color: #008000;">==</span> lowerBox<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gainFound <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set_gain<span style="color: #008000;">&#40;</span>REG_INT_RED_LO, half<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set_gain<span style="color: #008000;">&#40;</span>REG_INT_GREEN_LO, half<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set_gain<span style="color: #008000;">&#40;</span>REG_INT_BLUE_LO, half<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; performMeasurement<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; halfValue <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; halfValue <span style="color: #008000;">=</span> <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Max</span><span style="color: #008000;">&#40;</span>halfValue, get_readout<span style="color: #008000;">&#40;</span>REG_DATA_RED_LO<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; halfValue <span style="color: #008000;">=</span> <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Max</span><span style="color: #008000;">&#40;</span>halfValue, get_readout<span style="color: #008000;">&#40;</span>REG_DATA_GREEN_LO<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; halfValue <span style="color: #008000;">=</span> <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Max</span><span style="color: #008000;">&#40;</span>halfValue, get_readout<span style="color: #008000;">&#40;</span>REG_DATA_BLUE_LO<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>halfValue <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1000</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upperBox <span style="color: #008000;">=</span> half<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>halfValue <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">1000</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lowerBox <span style="color: #008000;">=</span> half<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gainFound <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> half<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Finally, lines 175 to 220 tries to establish a suitable gain for each color. This will be called 3 times because each color has a different sensitivity. You can get creative here and come up with your own algorithm which suits your specific application. If you had a controlled environment then in theory this would be very simple. Make sure you post back here and let us know if you stumble across a better way to determine the gain values.</p>
]]></content:encoded>
			<wfw:commentRss>http://baltimorehackerspace.com/2010/07/netduino-debut/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>LED Fireflies in a Jar (v1.0)</title>
		<link>http://baltimorehackerspace.com/2010/07/led-fireflies-in-a-jar-v1-0/</link>
		<comments>http://baltimorehackerspace.com/2010/07/led-fireflies-in-a-jar-v1-0/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 15:37:42 +0000</pubDate>
		<dc:creator>superfro</dc:creator>
				<category><![CDATA[How-Tos]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Soup]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[CCS]]></category>
		<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Microcontroller]]></category>
		<category><![CDATA[PIC]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://harfordhackerspace.org/?p=858</guid>
		<description><![CDATA[Me and my wife recently had our first child.  I&#8217;ve been trying to come up with some projects I can do for the kid.  Well, around this time of the year the fireflies go crazy around my house, so we came up with the idea to do led fireflies in a jar.  It should be [...]]]></description>
			<content:encoded><![CDATA[<p>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="480" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/TBUS7_ADqwI&amp;hl=en_US&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="480" height="385" src="http://www.youtube.com/v/TBUS7_ADqwI&amp;hl=en_US&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object>
</p>
<p>Me and my wife recently had our first child.  I&#8217;ve been trying to come up with some projects I can do for the kid.  Well, around this time of the year the fireflies go crazy around my house, so we came up with the idea to do led fireflies in a jar.  It should be something cool for him to stare at and is easy to build.</p>
<p>Schematics, C source code and hex file are available <a href="http://wiki.harfordhackerspace.org/index.php?title=Firefly_jar_v1">here on our wiki</a>.  If you want to build one it should only take about an hour.</p>
<p>I&#8217;m planning on making a version 2 with some changes, stay tuned for that one.</p>

<div class="ngg-galleryoverview" id="ngg-gallery-17-858">

	<!-- Slideshow link -->
	<div class="slideshowlink">
		<a class="slideshowlink" href="http://baltimorehackerspace.com/2010/07/led-fireflies-in-a-jar-v1-0/?show=slide">
			[Show as slideshow]		</a>
	</div>

	
	<!-- Thumbnails -->
		
	<div id="ngg-image-268" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/448px-deadbug_soldered_firefly_jar.jpg" title=" " class="shutterset_set_17" >
								<img title="448px-deadbug_soldered_firefly_jar" alt="448px-deadbug_soldered_firefly_jar" src="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/thumbs/thumbs_448px-deadbug_soldered_firefly_jar.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-269" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/448px-firefly_lid.jpg" title=" " class="shutterset_set_17" >
								<img title="448px-firefly_lid" alt="448px-firefly_lid" src="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/thumbs/thumbs_448px-firefly_lid.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-270" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/448px-fully_assembled_firefly_v1_jar.jpg" title=" " class="shutterset_set_17" >
								<img title="448px-fully_assembled_firefly_v1_jar" alt="448px-fully_assembled_firefly_v1_jar" src="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/thumbs/thumbs_448px-fully_assembled_firefly_v1_jar.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-271" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/448px-mounted_inside_components.jpg" title=" " class="shutterset_set_17" >
								<img title="448px-mounted_inside_components" alt="448px-mounted_inside_components" src="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/thumbs/thumbs_448px-mounted_inside_components.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-272" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/579px-firefly_jar_schematic.png" title=" " class="shutterset_set_17" >
								<img title="579px-firefly_jar_schematic" alt="579px-firefly_jar_schematic" src="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/thumbs/thumbs_579px-firefly_jar_schematic.png" width="88" height="75" />
							</a>
		</div>
	</div>
	
		
 	 	
	<!-- Pagination -->
 	<div class='ngg-clear'></div>
 	
</div>


]]></content:encoded>
			<wfw:commentRss>http://baltimorehackerspace.com/2010/07/led-fireflies-in-a-jar-v1-0/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using the PICkit 2 : As a Programmer</title>
		<link>http://baltimorehackerspace.com/2010/04/using-the-pickit-2-as-a-programmer/</link>
		<comments>http://baltimorehackerspace.com/2010/04/using-the-pickit-2-as-a-programmer/#comments</comments>
		<pubDate>Sun, 11 Apr 2010 17:11:07 +0000</pubDate>
		<dc:creator>superfro</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Microcontroller]]></category>
		<category><![CDATA[PIC]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://harfordhackerspace.org/?p=695</guid>
		<description><![CDATA[Around the space, we have been using the PICkit 2 Debug Express quite a bit lately.  This has allowed us to get pretty cozy with the functions of this handy little programmer and I thought I would share some of these features with you. I&#8217;m sure you&#8217;ve read the previous tutorial &#8216;Programming PIC Microcontrollers in [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_535" class="wp-caption alignright" style="width: 155px"><a href="http://harfordhackerspace.org/wp-content/uploads/2010/02/PICKit2.jpg"><img class="size-full wp-image-535 " style="margin: 10px;" title="PICKit2" src="http://harfordhackerspace.org/wp-content/uploads/2010/02/PICKit2.jpg" alt="PicKit2 Debug Express" width="145" height="145" /></a><p class="wp-caption-text">PicKit2 Debug Express</p></div>
<p>Around the space, we have been using the <a href="http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&amp;nodeId=1406&amp;dDocName=en023805">PICkit 2 Debug Express</a> quite a bit lately.  This has allowed us to get pretty cozy with the functions of this handy little programmer and I thought I would share some of these features with you.</p>
<p>I&#8217;m sure you&#8217;ve read the previous tutorial &#8216;<a href="http://harfordhackerspace.org/2010/02/programming-pic-microcontrollers-in-c/">Programming PIC Microcontrollers in C</a>&#8216;, if you haven&#8217;t, you should go familiarize yourself with it now.  Continuing on from there, after we have a successful build of a led blinking we can use the PICkit 2 to program our chip via the ICSP header.</p>
<p><span id="more-695"></span></p>
<p>Our Debug Express Kit&#8217;s came with a Demo Board which has a 44 pin PIC16F887 microchip. The microcontroller is connected to a 6 Pin Male header called the ICSP (In-Circuit Serial Programmer) header.  All PIC microcontrollers with the letter &#8220;F&#8221; in their name can be programmed this way, even if you are using a breadboard and wires to make the connections. The &#8220;F&#8221; stands for Flash.  Maybe in a future article we will show you how to construct the ICSP header from the data sheet, but we will just assume you are using the 44 Pin Demo Board for now.</p>
<p><a href="http://harfordhackerspace.org/wp-content/uploads/2010/04/selectprogrammer.png"><img class="alignleft size-medium  wp-image-696" style="margin: 10px;" title="selectprogrammer" src="http://harfordhackerspace.org/wp-content/uploads/2010/04/selectprogrammer-300x249.png" alt="" width="300" height="249" /></a></p>
<p><br class="spacer_" /></p>
<p>You should already have the PICkit 2 connected to your PC via the USB port, so after a successful build of our program, we can program the chip by selecting the &#8216;Programmer&#8217; menu from within MPLAB, and then selecting &#8216;Select Programmer&#8217;, then &#8216;PICkit 2&#8242;.</p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><a href="http://harfordhackerspace.org/wp-content/uploads/2010/04/connected.png"><img class="size-medium wp-image-698 alignright" style="margin: 10px;" title="connected" src="http://harfordhackerspace.org/wp-content/uploads/2010/04/connected-300x155.png" alt="" width="300" height="155" /></a></p>
<p><br class="spacer_" /></p>
<p>At that point below in the debug window you should see a message such as &#8216;Found PICkit 2 &#8211; Operating System Version 2.32.0 &#8230;&#8217; If you do not, select &#8216;Programmer&#8217; then &#8216;Connect&#8217; from MPLAB.</p>
<p><br class="spacer_" /></p>
<p><a href="http://harfordhackerspace.org/wp-content/uploads/2010/04/program.png"><img class="alignleft size-medium wp-image-725" style="margin: 10px;" title="program" src="http://harfordhackerspace.org/wp-content/uploads/2010/04/program-159x300.png" alt="" width="159" height="300" /></a></p>
<p><br class="spacer_" /></p>
<p>Finally we select &#8216;Programmer&#8217; then &#8216;Program&#8217;, and it should start erasing and programming the chip.</p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><a href="http://harfordhackerspace.org/wp-content/uploads/2010/04/settings1.png"><img class="alignright size-medium wp-image-737" style="margin: 10px;" title="settings" src="http://harfordhackerspace.org/wp-content/uploads/2010/04/settings1-251x300.png" alt="" width="251" height="300" /></a></p>
<p><br class="spacer_" /></p>
<p>Lastly, one tip that speeds up the programming process is to go to the &#8216;Programmer&#8217; menu and then click &#8216;settings&#8217;. Then check the boxes &#8216;Program after a successful build&#8217; and &#8216;Run after a successful program&#8217;.  Once you do that you can just hit F10 to compile and flash the pic over and over as you make small adjustments to your code.</p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p>In the end, this simple task is actually quite convenient. Being able to program a chip in-circuit, instead of taking the chip out of the socket, putting it in a programmer, programming it, taking it out, putting it back into the other socket, turning on, etc.</p>
<p><br class="spacer_" /></p>
<p>Finally, an often overlooked feature is that the PICkit 2 will power your chip if it doesn&#8217;t detect any voltage, which for me personally is the case before I have a power supply set up.  I wouldn&#8217;t overdue it, you can power small things, led&#8217;s, tiny motors but don&#8217;t try to draw too much current from it.  The maximum power draw is 200-300mA less than the USB port puts out, which in most cases is around 500mA leaving you with 200-300mA.  So don&#8217;t try to overdue it, if you&#8217;ve got a lot of power draw in your circuit, set up your own power source and the PICkit 2 will detect the voltage during program and wont try to power it.</p>
<p><br class="spacer_" /></p>
<p>Happy Hacking!</p>
]]></content:encoded>
			<wfw:commentRss>http://baltimorehackerspace.com/2010/04/using-the-pickit-2-as-a-programmer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Talking Dirty to MicroControllers!</title>
		<link>http://baltimorehackerspace.com/2010/02/talking-dirty-to-microcontrollers/</link>
		<comments>http://baltimorehackerspace.com/2010/02/talking-dirty-to-microcontrollers/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 03:52:19 +0000</pubDate>
		<dc:creator>solovus</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Microcontroller]]></category>
		<category><![CDATA[PIC]]></category>

		<guid isPermaLink="false">http://harfordhackerspace.org/?p=504</guid>
		<description><![CDATA[Wednesday was the day we finally got together and started the tedious process of mastering microcontrollers.  What began with just some simple LED illumination evolved and progressed throughout the evening.  In the end we had individually, but collectively, written programs for interpreting analog inputs, displaying binary values, and performing counting and reset operations.  The evening [...]]]></description>
			<content:encoded><![CDATA[<p>Wednesday was the day we finally got together and started the tedious process of mastering microcontrollers.  What began with just some simple LED illumination evolved and progressed throughout the evening.  In the end we had individually, but collectively, written programs for interpreting analog inputs, displaying binary values, and performing counting and reset operations.  The evening was pretty fulfilling for some less experienced members and refreshing for the remainder.  Another microcontroller experiementation session is being planned for the near future.</p>

<div class="ngg-galleryoverview" id="ngg-gallery-14-504">

	<!-- Slideshow link -->
	<div class="slideshowlink">
		<a class="slideshowlink" href="http://baltimorehackerspace.com/2010/02/talking-dirty-to-microcontrollers/?show=slide">
			[Show as slideshow]		</a>
	</div>

	
	<!-- Thumbnails -->
		
	<div id="ngg-image-233" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/3feb2010-microcontroller-session/dsc05421.jpg" title=" " class="shutterset_set_14" >
								<img title="dsc05421" alt="dsc05421" src="http://baltimorehackerspace.com/wp-content/gallery/3feb2010-microcontroller-session/thumbs/thumbs_dsc05421.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-234" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/3feb2010-microcontroller-session/dsc05426.jpg" title=" " class="shutterset_set_14" >
								<img title="dsc05426" alt="dsc05426" src="http://baltimorehackerspace.com/wp-content/gallery/3feb2010-microcontroller-session/thumbs/thumbs_dsc05426.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-235" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/3feb2010-microcontroller-session/8_bit_counter_screenshot.jpg" title=" " class="shutterset_set_14" >
								<img title="8_bit_counter_screenshot" alt="8_bit_counter_screenshot" src="http://baltimorehackerspace.com/wp-content/gallery/3feb2010-microcontroller-session/thumbs/thumbs_8_bit_counter_screenshot.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 	 	
	<!-- Pagination -->
 	<div class='ngg-clear'></div>
 	
</div>


]]></content:encoded>
			<wfw:commentRss>http://baltimorehackerspace.com/2010/02/talking-dirty-to-microcontrollers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

