<?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>rocketnumbernine &#187; projects</title>
	<atom:link href="http://www.rocketnumbernine.com/category/projects/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rocketnumbernine.com</link>
	<description>Andrew&#039;s Project Blog - hardware, software, things</description>
	<lastBuildDate>Sun, 16 May 2010 08:48:18 +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>External Time Capsule Fan</title>
		<link>http://www.rocketnumbernine.com/2009/11/22/external-time-capsule-fan/</link>
		<comments>http://www.rocketnumbernine.com/2009/11/22/external-time-capsule-fan/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 15:24:34 +0000</pubDate>
		<dc:creator>andrew</dc:creator>
				<category><![CDATA[projects]]></category>
		<category><![CDATA[fan]]></category>
		<category><![CDATA[Time Capsule]]></category>

		<guid isPermaLink="false">http://www.rocketnumbernine.com/?p=391</guid>
		<description><![CDATA[
My Apple Time Capsule runs hot &#8211; approaching 50°C to the touch &#8211; this isn&#8217;t helped by the lack of space it has and the Apple TV stacked on top (I&#8217;ve added some legs to give some space below and above for air to flow.   On large backups/restores during the summer &#8211; the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/rocketnumbernine/4124951592/" title="External Time Capsule Fan"><img src="http://farm3.static.flickr.com/2721/4124951592_86fafe62f0.jpg" width="500" height="234" alt="External Time Capsule Fan" /></a></p>
<p>My <a href="http://www.apple.com/timecapsule/">Apple Time Capsule</a> runs hot &#8211; approaching 50°C to the touch &#8211; this isn&#8217;t helped by the lack of space it has and the <a href="http://www.apple.com/appletv/">Apple TV</a> stacked on top (I&#8217;ve added some legs to give some space below and above for air to flow.   On large backups/restores during the summer &#8211; the device often went into over heat warning mode.  There&#8217;s also worrying <a href="http://www.guardian.co.uk/technology/blog/2009/nov/04/apple-time-capsule-failures-early">reports</a> of the power supply overheating and Time Capsules <a href="http://timecapsuledead.org/">dying</a>.</p>
<p>I originally thought of a standard PC fan with some kind of thermistor /thermocouple/sensor and a simple control circuit to activate the fan when temperature gets too hot, but stumbled across <a href="http://www.arctic-cooling.com/catalog/product_info.php?mID=279&#038;cPath=3_47_80">Arctic Cooling Pro TC F9 fan</a> which has a temperature sensor on a 40cm cable to turn up the fan speed when the temperature rises above 32°C.</p>
<p><a href="http://www.flickr.com/photos/rocketnumbernine/4124949764/" title="Fan with stand attached  (click for annotations)"><img src="http://farm3.static.flickr.com/2566/4124949764_f8bdb763ba.jpg" width="500" height="333" alt="Fan with stand attached" /></a></p>
<p>Replacing the plug with a 2.1mm power jack to fit a 12V wall wart PSU and creating a stand with a loop of wire coat hanger sleeved in heat shrink to give it a nice black cover to match the fans case and the unit was ready to go.  Taping the end of the temperature sensor to the top of what felt the hottest part of the Time Capsule (over the power supply next to the power inlet) reduced the temperature to less than 30°C.  I though the fan might be too low powered initially but it manages to cool all the devices on the shelf it&#8217;s on.</p>
<p>The unit is silent &#8211; at least quieter than the ambient noise and hopefully will extend the life of the Time Capsule.</p>
<p><b>References/Bibliography</b></p>
<ol>
<li><a href="http://www.arctic-cooling.com/catalog/product_info.php?mID=279&#038;cPath=3_47_80">Arctic Cooling Pro TC F9 fan</a>
<li><a href="http://timecapsuledead.org/">The Apple Time Capsule Memorial Register</a>
<li><a href="http://www.guardian.co.uk/technology/blog/2009/nov/04/apple-time-capsule-failures-early">Guardian: What&#8217;s killing Apple&#8217;s Time Capsules after 18 months?</a>
</ol>


<p>Related posts:<ol><li><a href='http://www.rocketnumbernine.com/2009/06/03/smt-table-top-reflow-oven-part-1/' rel='bookmark' title='Permanent Link: SMT Table Top Reflow Oven (part 1)'>SMT Table Top Reflow Oven (part 1)</a></li>
<li><a href='http://www.rocketnumbernine.com/2009/08/27/smt-table-top-reflow-oven-part-2/' rel='bookmark' title='Permanent Link: SMT Table Top Reflow Oven (part 2): Controlling the Heater Elements'>SMT Table Top Reflow Oven (part 2): Controlling the Heater Elements</a></li>
<li><a href='http://www.rocketnumbernine.com/2009/04/10/5v-33v-bidirectional-level-converter/' rel='bookmark' title='Permanent Link: 5V-3.3V bidirectional level converter'>5V-3.3V bidirectional level converter</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.rocketnumbernine.com/2009/11/22/external-time-capsule-fan/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SMT Table Top Reflow Oven (part 2): Controlling the Heater Elements</title>
		<link>http://www.rocketnumbernine.com/2009/08/27/smt-table-top-reflow-oven-part-2/</link>
		<comments>http://www.rocketnumbernine.com/2009/08/27/smt-table-top-reflow-oven-part-2/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 20:24:16 +0000</pubDate>
		<dc:creator>andrew</dc:creator>
				<category><![CDATA[projects]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[reflow oven]]></category>
		<category><![CDATA[relay]]></category>
		<category><![CDATA[SMT]]></category>
		<category><![CDATA[soldering]]></category>

		<guid isPermaLink="false">http://www.rocketnumbernine.com/?p=361</guid>
		<description><![CDATA[ In a previous article I described insulation of a mini/toaster oven with the aim of using it for table top reflow soldering.  Some manual tests have been encouraging.  This article documents creation of the circuitry required to turn the ovens heating elements on and off under control of a microcontroller.  I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/rocketnumbernine/3735221789/" target="images"title="Oven Heater Element Control Circuit"><img src="http://farm3.static.flickr.com/2444/3735221789_014454f8f2.jpg" width="500" height="267" alt="Oven Heater Element Control Circuit" /></a> In a <a href="http://www.rocketnumbernine.com/2009/06/03/smt-table-top-reflow-oven-part-1/">previous article</a> I described insulation of a mini/toaster oven with the aim of using it for table top reflow soldering.  Some manual <a href="http://www.rocketnumbernine.com/2009/06/13/bidirectional-level-converter-pcb/">tests</a> have been encouraging.  This article documents creation of the circuitry required to turn the ovens heating elements on and off under control of a microcontroller.  I&#8217;m still not convinced that the oven can increase its temperature fast enough to warrant attempting to recreate a &#8216;proper&#8217; reflow profile, but even if not,  using a microcontroller to log and display the temperature and turn off the heating elements when the peak is reached will still be worthwhile.</p>
<p><b>The circuitry below involves mains electricity, don&#8217;t try this unless you are sure you know what you are doing &#8211; check, test and insulate repeatedly and thoroughly.</b>  There are a number of commercial relay control boards available, for example <a href="http://www.futurlec.com/Relay_4.shtml">Futurlec</a> and <a href="http://www.elektor.com/products/e-blocks/modules/relay-board.530431.lynkx">Elector</a>.</p>
<p>The Hinari HTP033 oven has two elements and a total power rating of 630W &#8211; so each draws just over 1.3A at 240V (UK mains voltage), this obviously can&#8217;t be provided directly from a microcontroller IO pin but requires some kind of <a href="http://en.wikipedia.org/wiki/Relay">relay</a>.  There&#8217;s a huge range of relays available &#8211; I choose a <a href="http://www.digchip.com/datasheets/download_datasheet.php?id=920665&#038;part-number=SRRHN-1C-S-5VDC">IMO SRRHN-1C-S-5VDC</a>, this can operate at 5Volts, draws 100mA (I actually found it took under 75mA) and is fairly cheap (a few £).  Although a 5V device can be driven from a microcontroller IO pin, 100mA is too high a load, 40mA is the maximum for most AVR Microcontroller IO pins, although the maximum current drain for the entire device is around 200mA, so 40mA can only be driven on a few IO pins at once.</p>
<p><b>Driving the relay</b><br />
A simple transistor/resistor circuit can be used to control the relay with much lower power drain.  Basically the transistor (I used a 2N2222A) is being used as a current amplifying switch. <a href="http://www.rocketnumbernine.com/blog/wp-content/uploads/2009/07/relay-control.png"><img src="http://www.rocketnumbernine.com/blog/wp-content/uploads/2009/07/relay-control-300x224.png" alt="Relay Board for Heater Element Control" title="Relay Board for Heater Element Control" width="300" height="224" class="alignright size-medium wp-image-366" /></a> When the transistor is being used in on/off mode &#8211; only approximate mental/back of the envelope maths are needed to calculate the values of the resistors (<a href="http://www.google.co.uk/search?q=using+transistor+as+power+switch">Search Google for more background</a>). </p>
<p>From the transistors <a href="http://www.st.com/stonline/products/literature/ds/9288.pdf">datasheet</a>, when the 100mA of current required to power the relay flows from collector to emittor the transistor will provide a minimum gain of just under 100 &#8211; so atleast 1mA  needs to flow into the transistors base, if the IO pin is high at 5V and there&#8217;s a 1V voltage drop from the transistors base to emitter, a resistor of 4K will provide this (R = V/I = (5V-1V)/1mA).  To cope with less gain and a lower Vbe voltage, it&#8217;s best to provide much more current to I used a 1330R and measured 3.5mA going into the base of the transistor when on.</p>
<p>Resistor R2 connects the base to ground when the input is unconnected.  This ensures the relay will be safely turned off during microcontroller startup.  The 10K resistor ensures the current flowing through it when the input is high will be low.</p>
<p>The relay is just a coil of wire which acts as an electromagnet when current flows through it, unfortunately when the power is switched off it will act as an inductor pumping current back into the circuit &#8211; the diode protects the transistor when this happens by providing a &#8216;return path&#8217; to the 5V supply.</p>
<p><b>Installation</b><br />
The circuit (2 of them) was simple enough to build on a <a href="http://www.flickr.com/photos/rocketnumbernine/3862249901/" title="Oven Relay Box" ><img src="http://farm3.static.flickr.com/2611/3862249901_17b03ddfd3_m.jpg" class="alignleft" width="240" height="187" alt="Oven Relay Box" /></a>Prototype/Vero board (pictured above) and housed in a plastic box to protect from earthing.  I bypassed the ovens element and timer controls and passed the live mains wire to the relay box with an output to each of the elements.</p>
<p>Some brief tests with an Arduino turning on the relay control pins showed that the relevant oven element came on perfectly, with a reasuring click from the relay as it switched.<br />
Now I have more control over the ovens elements the plan is to do more extensive testing to see how controllable the temperature is.</p>
<p><b>References/Bibliography</b></p>
<ol>
<li><a href="http://www.digchip.com/datasheets/download_datasheet.php?id=920665&#038;part-number=SRRHN-1C-S-5VDC">IMO SRRHN-1C-S-5VDC</a>
<li><a href="http://www.google.co.uk/search?q=using+transistor+as+power+switch">Google Search: Using transistor as a power switch</a></p>
<li><a href="http://www.st.com/stonline/products/literature/ds/9288.pdf">2N2222A transistor datasheet</a>
<li>Commercial Relay boards: <a href="http://www.futurlec.com/Relay_4.shtml">Futurlec</a> and<a href="http://www.elektor.com/products/e-blocks/modules/relay-board.530431.lynkx">Elector</a>.
<li> Some other relay control articles on the web <a href="http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=119">SparkFun</a>
</ol>


<p>Related posts:<ol><li><a href='http://www.rocketnumbernine.com/2009/06/03/smt-table-top-reflow-oven-part-1/' rel='bookmark' title='Permanent Link: SMT Table Top Reflow Oven (part 1)'>SMT Table Top Reflow Oven (part 1)</a></li>
<li><a href='http://www.rocketnumbernine.com/2009/03/07/first-arduino-project-the-finger/' rel='bookmark' title='Permanent Link: First Arduino Project: the finger'>First Arduino Project: the finger</a></li>
<li><a href='http://www.rocketnumbernine.com/2009/03/27/xyloduino-simple-arduinopiezo-organ/' rel='bookmark' title='Permanent Link: Xyloduino: Simple Arduino/piezo organ'>Xyloduino: Simple Arduino/piezo organ</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.rocketnumbernine.com/2009/08/27/smt-table-top-reflow-oven-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Bidirectional Level Converter PCB</title>
		<link>http://www.rocketnumbernine.com/2009/06/13/bidirectional-level-converter-pcb/</link>
		<comments>http://www.rocketnumbernine.com/2009/06/13/bidirectional-level-converter-pcb/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 00:00:24 +0000</pubDate>
		<dc:creator>andrew</dc:creator>
				<category><![CDATA[projects]]></category>
		<category><![CDATA[3.3V]]></category>
		<category><![CDATA[5V]]></category>
		<category><![CDATA[BatchPCB]]></category>
		<category><![CDATA[LDO]]></category>
		<category><![CDATA[MCP1700]]></category>
		<category><![CDATA[MOSFET]]></category>
		<category><![CDATA[regulator]]></category>
		<category><![CDATA[shifter]]></category>
		<category><![CDATA[TXS01xx]]></category>
		<category><![CDATA[voltage conversion]]></category>

		<guid isPermaLink="false">http://www.rocketnumbernine.com/?p=243</guid>
		<description><![CDATA[
The Level converter PCB has finally arrived from BatchPCB.  Their service was easy to use and took about 4 weeks to arrive (as advertised).
Because the board was less than the minimum 1&#8243; square they had already doubled it up so I got 2 for $2.50 + $1.20 postage (untracked and uninsured) and $10 handling. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/rocketnumbernine/3608036805/" title="level converter pcb" target="images"><img src="http://farm4.static.flickr.com/3661/3608036805_71e9c81a87_m.jpg" width="254" height="201" class="alignleft" alt="level converter pcb" /></a></p>
<p>The <a href="http://www.rocketnumbernine.com/2009/04/10/5v-33v-bidirectional-level-converter/">Level converter</a> PCB has finally arrived from <a href="http://www.batchpcb.com/">BatchPCB</a>.  Their service was easy to use and took about 4 weeks to arrive (as advertised).</p>
<p>Because the board was less than the minimum 1&#8243; square they had already doubled it up so I got 2 for $2.50 + $1.20 postage (untracked and uninsured) and $10 handling.  With this size the service obviously dwarfs the PCB cost so its worth batching up your order to reduce the overhead.  I had ordered two of the level converter board and another very small SOIC-8 break out board and received 8 pieces for $21.20. </p>
<p>I was keen to use this to test SMT soldering in my <a href="http://www.rocketnumbernine.com/2009/06/03/smt-table-top-reflow-oven-part-1/">converted mini oven</a>.  Adding paste using a syringe was much harder than anticipated.  The paste was fairly thick and left thin tails which required cleaning up.<br />
<a href="http://www.flickr.com/photos/rocketnumbernine/3607988649/" target="images" title="bi-directional level converter board by rocketnumbernine"><img class="alignright" src="http://farm4.static.flickr.com/3369/3607988649_029d288cac_m.jpg" width="240" height="153" alt="bi-directional level converter board" /></a>Despite dozens of tests with the temperature gauge on the oven, half way through the process it stopped working so I had to play it by ear &#8211; I turned the oven off just after the solder went molten and reflowed around the components (a magical sight).  I might have left it a little late as the PCB has a slight brown tint to it.</p>
<p><a href="http://www.flickr.com/photos/rocketnumbernine/3607987821/" target="images" title="bi-directional level converter board"><img src="http://farm4.static.flickr.com/3317/3607987821_10d94e0919.jpg" width="500" height="399" alt="bi-directional level converter board" /></a></p>
<p>The excess solder that hadn&#8217;t been cleaned off the board had formed small balls which could be chipped off as can be seen below.  The pads which had too much paste formed balls around the pins of the chip.  Otherwise the solder flowed around the components surprisingly well.</p>
<p><a href="http://www.flickr.com/photos/rocketnumbernine/3607989261/" title="bi-directional level converter board closeup" target="images"><img src="http://farm3.static.flickr.com/2473/3607989261_f6b97a4cb6_m.jpg" class="alignleft" width="240" height="199" alt="bi-directional level converter board closeup" /></a>I had made two screw ups with the PCB layout: first the the labels are too small to read, secondly more serious I mixed up the pin layout on the MCP1700 LDO, which resulted in it burning out when connected to 5V &#8211; luckily without harming any other components.</p>
<p><a href="http://www.flickr.com/photos/rocketnumbernine/3621298527/" title="Fixed PCB" target="images"><img src="http://farm4.static.flickr.com/3378/3621298527_000f39ecac_t.jpg" class="alignright" width="100" height="100" alt="Fixed PCB" /></a>It was just about possible to replace the burnt out component with a T0-92 version soldered to the capacitor pads.  This worked fine and the power shift and logic level converter worked as expected.  I need to test the max amount of current that the LDO 5V-3.3V downshifter can provide without getting too hot (the T0-92 package is likely to be better than the SOT-23 anyway).</p>
<p>
<b>TI TXS0104E and family</b><br />
Another option in level conversion is the <a href="http://focus.ti.com/docs/prod/folders/print/txs0104e.html" target="images">Texas Instruments TXS0104E</a> and <a href="http://focus.ti.com/docs/prod/folders/print/txs0104e.html#relatedproducts" target="images">family</a> these are available in 2-8 bit conversion packages in both open-drain and push pull versions.  These chips also have a output enable pin which can be used to pu the I/O pins into tri-state mode which could be useful.</p>


<p>Related posts:<ol><li><a href='http://www.rocketnumbernine.com/2009/04/10/5v-33v-bidirectional-level-converter/' rel='bookmark' title='Permanent Link: 5V-3.3V bidirectional level converter'>5V-3.3V bidirectional level converter</a></li>
<li><a href='http://www.rocketnumbernine.com/2009/06/03/smt-table-top-reflow-oven-part-1/' rel='bookmark' title='Permanent Link: SMT Table Top Reflow Oven (part 1)'>SMT Table Top Reflow Oven (part 1)</a></li>
<li><a href='http://www.rocketnumbernine.com/2009/06/27/using-the-max6675-thermocouple-to-digital-converter/' rel='bookmark' title='Permanent Link: Using the MAX6675 Thermocouple-to-Digital Converter'>Using the MAX6675 Thermocouple-to-Digital Converter</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.rocketnumbernine.com/2009/06/13/bidirectional-level-converter-pcb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SMT Table Top Reflow Oven (part 1)</title>
		<link>http://www.rocketnumbernine.com/2009/06/03/smt-table-top-reflow-oven-part-1/</link>
		<comments>http://www.rocketnumbernine.com/2009/06/03/smt-table-top-reflow-oven-part-1/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 14:44:01 +0000</pubDate>
		<dc:creator>andrew</dc:creator>
				<category><![CDATA[projects]]></category>
		<category><![CDATA[MAX6675]]></category>
		<category><![CDATA[reflow oven]]></category>
		<category><![CDATA[SMT]]></category>
		<category><![CDATA[soldering]]></category>

		<guid isPermaLink="false">http://www.rocketnumbernine.com/?p=222</guid>
		<description><![CDATA[
My soldering iron is dirt cheap, low power and pretty poor for soldering SMT components.   After looking at the price of a more suitable replacement I decided that it was worth looking at the option of making an SMT Reflow oven from a mini/toaster oven, it would cost less than a decent temperature [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/rocketnumbernine/3592488632/" title="Testing Uninsulated Oven"><img src="http://farm4.static.flickr.com/3563/3592488632_71386c23ca.jpg" width="500" height="333" alt="Testing Uninsulated Oven" /></a></p>
<p>My soldering iron is dirt cheap, low power and pretty poor for soldering SMT components.   After looking at the price of a more suitable replacement I decided that it was worth looking at the option of making an <a href="http://en.wikipedia.org/wiki/Reflow_soldering">SMT Reflow</a> oven from a mini/toaster oven, it would cost less than a decent temperature controlled iron and be fun to create.</p>
<p><b>The Oven</b><br />
I seem to be suffering from several of the <a href="http://en.wikipedia.org/wiki/Lead_poisoning#Symptoms_and_effects">symptoms of lead poisoning</a> already, so use lead-free solder and want to continue, which means a higher temperature is required.</p>
<p>Googling for lead-free solder reflow temperature profiles gives lots of results from both solder and parts manufacturers (<a href="http://www.altera.com/literature/an/an353.pdf">Altera</a>, <a href="http://www.wolfsonmicro.com/uploads/documents/en/WAN0158.pdf">Wolfson Micro</a>, <a href="http://www.latticesemi.com/lit/docs/technotes/tn1076.pdf">Lattice Semiconductor</a>, and <a href="http://www.kester.com/Data%20Sheets/Solder%20Pastes/No-Clean/R276%20Lead-free%20Global%20(03Oct07).pdf">Kester</a>), and the &#8217;standard&#8217;: <a href="http://www.jedec.org/download/search/jstd020d-01.pdf">IPC/JEDEC J-STD-020D.1</a>.  These suggest that a max temperature of between 235-255°C is required and the ability to ramp up the temperature towards the max to reduce the time at the high temperatures (30 seconds within 5% of the max).</p>
<p>This is tough for a mini oven, many are advertised with a maximum of 220°C.  However, some experiments recording the temperature of my parents cheap Hinari Tiny Top with a thermocouple attached to a multimeter, showed that it could get up to over 255°C (the ovens thermostat seems to cut out at around 260°C &#8211; less than the rated 280°C) despite only using 630 Watts.  The only caveat is that as it approached the maximum it could only raise the temperatures by about 0.5-1.0°C/Second. However, the oven was also loosing a lot of heat, particularly from the top, so some insulation may help things.</p>
<p>I considered some of the more powerful ovens (1300-2500W), in particular one with a fan would have been good to regulate the temperature around the oven, but they have correspondingly larger spaces to heat &#8211; 10-24 litres &#8211; compared with the smaller 6.5 litres.  I decided to go with a <a href="http://www.amazon.co.uk/gp/product/B001IBI8FE?ie=UTF8&#038;tag=rocketnumbernine-21&#038;linkCode=as2&#038;camp=1634&#038;creative=6738&#038;creativeASIN=B001IBI8FE">Hinari HTP033</a>:</p>
<ol>
<li>It has two heating elements which allows for more slightly more fine grained temperature control (rather than on/off of a single element)
<li>Has a transparent door allowing the reflow process to be monitored
<li>The housing looks easily to insulate and modifiable
<li>£20 delivered from ebay (Amazon were out of stock)
</ol>
<p><b>The Plan</b></p>
<ol>
<li>Insulate Mini Oven.
<li>Thermocouple to monitor temperature.
<li>Microcontroller monitoring the temperature and controlling the heater elements.
<li>USB interface for control/uploading and downloading temperature profiles.
</ol>
<p>As stated above &#8211; if the ovens heating elements aren&#8217;t quick enough, then using a microcontroller to control the temperature is redundant overkill.  Its just enough to turn on the oven, put the board in when the temperature gets high enough, then turn off and open the door when the maximum temperature is achieved.  If this was the case the backup plan was to just add an LCD screen showing the current temperature and use the oven manually.</p>
<p><b>Thermocouple</b><br />
I thought that measuring the temperature would be simply a matter of connecting a <a href="http://en.wikipedia.org/wiki/Thermocouple">thermocouple</a> (which creates a voltage difference based on the temperature difference between the two metals in its probe) to an Analogue to Digital Converter in some configuration and reading off the temperature value, but it appears to be a fairly non trivial <a href="http://srdata.nist.gov/its90/download/type_k.tab">task</a>.  Luckily there are a number of off the shelf integrated circuits that the thermocouple can be connected to directly and produce a digital temperature reading.  I choose the <a href="http://datasheets.maxim-ic.com/en/ds/MAX6675.pdf">MAX6675</a>, which provides 0.25°C resolution from 0-1023°C on a simple to use SPI output in an 8 pin SOIC.</p>
<p><b>Testing</b><br />
To get temperatures from the oven a <a href="http://www.pjrc.com/teensy/index.html">Teensy</a> at90usb162 board was used to poll the temperature every second and send it to the USB, where it was copied into a spreadsheet.</p>
<p>Chart below shows two test runs of the oven with both elements on (I turned off the oven earlier on the second run).</p>
<p><a href="http://www.flickr.com/photos/rocketnumbernine/3593350708/" title="Uninsulated Oven Temperature Profile" target="images"><img src="http://farm4.static.flickr.com/3322/3593350708_c94cf436d4.jpg" width="500" height="375" alt="Uninsulated Oven Temperature Profile" /></a></p>
<p>Surprisingly it got up to to well over 300°C, before I cut the power, with no sign of a thermostat limiter.   The temperature rises at roughly 0.9°C a second to 200°C then slows down to about 0.5°C/Sec. to 250°C.  Although the slow down towards the peak is disappointing &#8211; this is probably just about usable although it would be better to reduce the time spent near the maximum.<br />
The amount of heat escaping from the oven is fairly excessive &#8211; the temperature of the top of the oven was approaching 100°C so some insulation will help to increase the gradient.</p>
<p><b>Insulation</b><br />
After some research I chose to use <a href="http://www.designengineering.com/products.asp?m=sp&#038;pid=75&#038;tid=1">Reflect-A-Gold</a> sheet for insulation, this aerospace/automotive heat insulation isn&#8217;t the cheapest available but was fairly easy to use and claims to reflect 80% of radiant heat up to 850°F.</p>
<p><a href="http://www.flickr.com/photos/rocketnumbernine/3592489430/" title="Partially insulated oven" target="images"><img src="http://farm4.static.flickr.com/3309/3592489430_3072fe3384_m.jpg" class="alignright" width="239" height="240" alt="Partially insulated oven" /></a></p>
<p>The HTP033 is made of riveted sheet metal with a slightly thicker back plate to keep it rigid, after drilling out 4 rivets the oven basically falls apart.  The side and top comes away from the internals of the oven allowing it to be just about completely wrapped with the Reflect-A-Gold sheet.  Most of the oven had two layers of sheet, with several layers around the edges of the door to increase the closeness of the fit (without there was a 5mm gap with the door closed).</p>
<p><a href="http://www.flickr.com/photos/rocketnumbernine/3591683079/" title="Insulated Oven"><img src="http://farm3.static.flickr.com/2453/3591683079_f16a63a79a.jpg" width="500" height="334" alt="Insulated Oven" /></a></p>
<p>Results after insulation are are shown below, the oven was turned off as close as possible to 245 °Centigrade.</p>
<p><a href="http://www.flickr.com/photos/rocketnumbernine/3592544367/" title="Insulated Oven Temperature Profile"><img src="http://farm4.static.flickr.com/3610/3592544367_56d185973e.jpg" width="500" height="375" alt="Insulated Oven Temperature Profile" /></a></p>
<p>After insulation the oven temperature increases by about 1°C/S. to 200°C and 0.75°C/S. after that (Compared with 0.9°C/S. and 0.5°C/S. respectively for the uninsulated oven).   Temperature inside the case behind the ovens controls went up to about 50-60 °C &#8211; perhaps still a little too hot to house the electronics.</p>
<p><b>Conclusions and Future Work</b><br />
I&#8217;m fairly happy with the performance of the oven, considering its cost and size.   Next steps are:</p>
<ol>
<li>More temperature tests to see if the performance can be increased.
<li>Do some test &#8216;reflows&#8217; &#8211; just turning on the oven and turning off when it reaches the maximum temperature to see what temperature is required by the solder and how good the results are.
<li>Develop a controller/relay board and investigate controlling the heating elements under processor control.
</ol>
<p><b>References/Resources</b></p>
<ol>
<li>Other projects: <a href="http://openhardware.net/Misc_Stuff/ToasterSMD/">&#8220;Toaster oven SMD&#8221;</a>, <a href="http://www.homanndesigns.com/SMDToasterOvenProject.html">&#8220;SMD ReflowToaster Oven&#8221;</a>, <a href="http://www.circuitcellar.com/library/print/0704/Lacoste_168/index.htm">&#8220;Easy Reflow&#8221;</a>, <a href="http://www.seattlerobotics.org/encoder/200006/oven_art.htm">&#8220;Have you seen my new soldering Iron?&#8221;</a>, <a href="http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=59">&#8220;Reflow Skillet&#8221;</a>.  And products:  <a href="http://www.thesiliconhorizon.com/Products.htm">&#8220;techFX reflow 2.0 controller with PID control!&#8221;</a>, <a href="http://www.articulationllc.com/product.sc?categoryId=-1&#038;productId=13">&#8220;Artic Reflow Oven&#8221;</a>, <a href="http://www.apsgold.com/reflow-ovens/low-volume/gf-c2-reflow-oven">&#8220;GF C2 Reflow Oven&#8221;</a>.
<li>Various reflow temperature profiles: <a href="http://www.altera.com/literature/an/an353.pdf>Altera</a>, <a href="http://www.wolfsonmicro.com/uploads/documents/en/WAN0158.pdf">Wolfson Micro</a>, <a href="http://www.latticesemi.com/lit/docs/technotes/tn1076.pdf">Lattice Semiconductor</a>, and  <a href="http://www.kester.com/Data%20Sheets/Solder%20Pastes/No-Clean/R276%20Lead-free%20Global%20(03Oct07).pdf">Kester R206 Solder Paste</a>.
<li><a href="http://www.jedec.org/download/search/jstd020d-01.pdf">IPC/JEDEC J-STD-020D.1 Moisture/Reflow Sensitivity Classification for Nonhermetic Solid State Surface Mount Devices</a>
<li><a href="http://www.leadfreemagazine.com/pages/pdf/pain_out_of_reflow.pdf">Taking the Pain Out of Pb-free Reflow, Lead Free Magazine</a>
<li><a href="http://datasheets.maxim-ic.com/en/ds/MAX6675.pdf">MAX6675 K-type Thermocouple-to-Digital Converter</a>
<li><a href="http://www.designengineering.com/products.asp?m=sp&#038;pid=75&#038;tid=1">Reflect-A-Gold Heat insulation sheet and tape</a>
</ol>


<p>Related posts:<ol><li><a href='http://www.rocketnumbernine.com/2009/08/27/smt-table-top-reflow-oven-part-2/' rel='bookmark' title='Permanent Link: SMT Table Top Reflow Oven (part 2): Controlling the Heater Elements'>SMT Table Top Reflow Oven (part 2): Controlling the Heater Elements</a></li>
<li><a href='http://www.rocketnumbernine.com/2009/11/22/external-time-capsule-fan/' rel='bookmark' title='Permanent Link: External Time Capsule Fan'>External Time Capsule Fan</a></li>
<li><a href='http://www.rocketnumbernine.com/2009/06/27/using-the-max6675-thermocouple-to-digital-converter/' rel='bookmark' title='Permanent Link: Using the MAX6675 Thermocouple-to-Digital Converter'>Using the MAX6675 Thermocouple-to-Digital Converter</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.rocketnumbernine.com/2009/06/03/smt-table-top-reflow-oven-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Xyloduino: Simple Arduino/piezo organ</title>
		<link>http://www.rocketnumbernine.com/2009/03/27/xyloduino-simple-arduinopiezo-organ/</link>
		<comments>http://www.rocketnumbernine.com/2009/03/27/xyloduino-simple-arduinopiezo-organ/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 10:37:30 +0000</pubDate>
		<dc:creator>andrew</dc:creator>
				<category><![CDATA[projects]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[piezo]]></category>

		<guid isPermaLink="false">http://www.rocketnumbernine.com/?p=90</guid>
		<description><![CDATA[The following describes a quick project to create &#8216;music&#8217; (ok &#8217;sound&#8217;) with a piezo-electric transducer driven directly from an AVR microcontroller and as few other components as possible.

Driving the Piezo-electric transducer.
Just about all AVR microcontrollers have one or more counters/timers that can be driven from the main clock or an external signal.  A prescaler [...]]]></description>
			<content:encoded><![CDATA[<p>The following describes a quick project to create &#8216;music&#8217; (ok &#8217;sound&#8217;) with a piezo-electric transducer driven directly from an AVR microcontroller and as few other components as possible.</p>
<p><object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=3878778&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=3878778&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object></p>
<p><strong>Driving the Piezo-electric transducer.</strong></p>
<p>Just about all AVR microcontrollers have one or more counters/timers that can be driven from the main clock or an external signal.  A prescaler can be used to reduce the frequency of the clock (16MHz on the Arduino) by 8, 64, 256, or 1024 when this scaled clock ticks it increments a timer and when that timer reaches a specified MAX value it can create an interrupt to execute a piece of code or toggle a register bit.  That register bit can be connected to an IO pin and used to drive an external circuit.</p>
<p>The brilliance of timers is that once setup, they can run unattended with no code.  They&#8217;re very useful for generating accurate fixed width pulses to control servo motors for example, (the MAX value is set so that the output pin is high for the right amount of time to specify the position of the servo arm).  The controller will continue to generate the pulses at regular intervals allowing the code to do other things &#8211; all it has to do is set the register holding the MAX value of the counter to the desired servo position.</p>
<p>This example uses the AVR counter/timer 0 mechanism in CTC mode (Clear Timer on Compare).  As the counter is incremented its value is compared with the MAX value specified in a register, when these values are equal the timer is cleared back to zero and starts counting up again.  When this happens the microcontroller can  be configured to toggle the value of the OC0A output (which is connected to an output port &#8211; D6 on the atmega68/132, B7 on the AT90USB162).</p>
<p>As the output pin is toggled each time the counter reaches its max value we&#8217;ll get a square wave of frequency:</p>
<div align="center">frequency = clock/(2 * PRESCALE * (1+MAX))</div>
<p>As Timer0 is an 8 bit counter (max value 255), with a 16Mz clock we can generate frequencies of between 8MHz (no prescale, max=0) and 30Hz (1024 prescale, max=255).  The human ear can hear between 15-20KHz although age and the abilities of the piezo transducer will limit this.</p>
<p>The test program below sets a fixed prescale of 256 and cycles the max value of the counter between 0 and 255 to test that things are working (will output between 110-12KHz).  Note, the arduino IDE doesn&#8217;t provide helper functionality for controlling the timer in this mode (the <a href="http://arduino.cc/en/Reference/AnalogWrite">analogueWrite()</a> method uses a similar technique for generating <a href="http://arduino.cc/en/Tutorial/PWM">PWM</a>).  The registers are available as variables/constants (MAX counter value = OCR0A, Timer/Counter Control Register = TCCR0A) and individual bits have to be set to configure the timer accordingly (see <a href="http://www.arduino.cc/playground/Code/BitMath">BitMath tutorial</a> and the full details of the ports used in Section 12 of the <a href="http://www.atmel.com/dyn/resources/prod_documents/doc8025.pdf">Atmega320 Datasheet</a>.<br />
Arduino also uses timer0 for timing so the delay() method won&#8217;t work &#8211; the following uses the _delay_ms() method</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:200px;"><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 /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &lt;util/delay.h&gt;</span><br />
<br />
<span style="color: #0000ff;">void</span> setup<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #666666;">// Toggle OC0A on compare match</span><br />
&nbsp; TCCR0A <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #000080;">&lt;&lt;</span>COM0A1<span style="color: #008000;">&#41;</span> <span style="color: #000040;">|</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span>COM0A0<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #666666;">// Mode 2 - CTC mode</span><br />
&nbsp; &nbsp; <span style="color: #000040;">|</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #000080;">&lt;&lt;</span>WGM02<span style="color: #008000;">&#41;</span> <span style="color: #000040;">|</span> &nbsp;<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span>WGM01<span style="color: #008000;">&#41;</span> <span style="color: #000040;">|</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #000080;">&lt;&lt;</span>WGM00<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <br />
&nbsp; <span style="color: #666666;">// OC0A port is D6 on atmega328 - set to output</span><br />
&nbsp; DDRD <span style="color: #000040;">|</span><span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">6</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <br />
&nbsp; <span style="color: #666666;">// prescale (0=Off,1=clk,2=8,3=64,4=256,5=1024) </span><br />
&nbsp; TCCR0B <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span>CS02<span style="color: #008000;">&#41;</span> <span style="color: #000040;">|</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #000080;">&lt;&lt;</span>CS01<span style="color: #008000;">&#41;</span> <span style="color: #000040;">|</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #000080;">&lt;&lt;</span>CS00<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> loop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">255</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&gt;</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000040;">--</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; OCR0A <span style="color: #000080;">=</span> i<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; _delay_ms<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">50</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>The piezo transducer positive wire was placed into pin D6 and common to ground.</p>
<p><video?></p>
<p>Making Music.</p>
<p>The next stage was to provide a layer ontop of the frequency generation to produce frequencies corresponding to musical notes.</p>
<p>The trick is to find values of MAX counter and prescale that create a frequency as close as possible to a note &#8211; the resolution of the timer means this is not always straightforward.  The following <a href="http://spreadsheets.google.com/ccc?key=pPbDjJ72fU8lv7CWyk13RHQ">Spreadsheet</a> lists the notes (C to B) that make up the Midi pitch scale (based on formula on <a href="http://en.wikipedia.org/wiki/Midi">WikiPedia</a>) and shows the values of MAX counter value for different prescales together with how close it is to the original frequency (accuracy % column).  The cells marked in yellow show the values that I used.  Thanks to the fact that the frequency doubles every octave and the prescale values quadruple &#8211; the MAX values are repeated every 2 octaves &#8211; so we only have to store a third of the values in the program.</p>
<p>The following program configures 12 input pins to use as a 1 octave keyboard, with 3 push button switches used to select which octave to play (pressing all three will make the system go into demo mode).</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:200px;"><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 /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &lt;util/delay.h&gt;</span><br />
<br />
<span style="color: #666666;">// the digital pins used in the keyboard</span><br />
<span style="color: #0000ff;">int</span> keyboard<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">1</span>,<span style="color: #0000dd;">2</span>,<span style="color: #0000dd;">3</span>,<span style="color: #0000dd;">4</span>,<span style="color: #0000dd;">5</span>,<span style="color: #0000dd;">7</span>,<span style="color: #0000dd;">8</span>,<span style="color: #0000dd;">9</span>,<span style="color: #0000dd;">10</span>,<span style="color: #0000dd;">11</span>,<span style="color: #0000dd;">12</span>,<span style="color: #0000dd;">13</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<span style="color: #666666;">// max counts for notes</span><br />
<span style="color: #0000ff;">int</span> oc<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000dd;">239</span>, <span style="color: #0000dd;">225</span>, <span style="color: #0000dd;">213</span>, <span style="color: #0000dd;">201</span>, <span style="color: #0000dd;">190</span>, <span style="color: #0000dd;">179</span>, <span style="color: #0000dd;">169</span>, <span style="color: #0000dd;">159</span>, <span style="color: #0000dd;">150</span>, <span style="color: #0000dd;">142</span>, <span style="color: #0000dd;">134</span>, <span style="color: #0000dd;">127</span>,<br />
&nbsp; <span style="color: #0000dd;">119</span>, <span style="color: #0000dd;">113</span>, <span style="color: #0000dd;">106</span>, <span style="color: #0000dd;">100</span>, <span style="color: #0000dd;">95</span>, <span style="color: #0000dd;">89</span>, <span style="color: #0000dd;">84</span>, <span style="color: #0000dd;">80</span>, <span style="color: #0000dd;">75</span>, <span style="color: #0000dd;">71</span>, <span style="color: #0000dd;">67</span>, <span style="color: #0000dd;">63</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<span style="color: #666666;">// prescale for octaves</span><br />
<span style="color: #0000ff;">int</span> pre<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">5</span>, <span style="color: #0000dd;">5</span>, <span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">3</span>, <span style="color: #0000dd;">3</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">void</span> setup<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #666666;">// Toggle OC0A on compare match</span><br />
&nbsp; TCCR0A <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #000080;">&lt;&lt;</span>COM0A1<span style="color: #008000;">&#41;</span> <span style="color: #000040;">|</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span>COM0A0<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// Mode 2 - CTC mode</span><br />
&nbsp; &nbsp; <span style="color: #000040;">|</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #000080;">&lt;&lt;</span>WGM02<span style="color: #008000;">&#41;</span> <span style="color: #000040;">|</span> &nbsp;<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span>WGM01<span style="color: #008000;">&#41;</span> <span style="color: #000040;">|</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #000080;">&lt;&lt;</span>WGM00<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <br />
&nbsp; <span style="color: #666666;">// OC0A port is D6 on atmega328 - set to output</span><br />
&nbsp; DDRD <span style="color: #000040;">|</span><span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">6</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <br />
&nbsp; <span style="color: #666666;">// prescale (0=Off,1=clk,2=8,3=64,4=256,5=1024) </span><br />
&nbsp; TCCR0B <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span>CS02<span style="color: #008000;">&#41;</span> <span style="color: #000040;">|</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #000080;">&lt;&lt;</span>CS01<span style="color: #008000;">&#41;</span> <span style="color: #000040;">|</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #000080;">&lt;&lt;</span>CS00<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// set the keyboard pins to inputs and set pull up resistors</span><br />
&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">12</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; pinMode<span style="color: #008000;">&#40;</span>keyboard<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>, INPUT<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; digitalWrite<span style="color: #008000;">&#40;</span>keyboard<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>, HIGH<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #666666;">// set the 'analog' pins as digital inputs</span><br />
&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">14</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">17</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; pinMode<span style="color: #008000;">&#40;</span>i, INPUT<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; digitalWrite<span style="color: #008000;">&#40;</span>i, HIGH<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #666666;">// play the note (-1 turns off)</span><br />
<span style="color: #0000ff;">void</span> playMidiNote<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> octave, <span style="color: #0000ff;">int</span> note<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>note <span style="color: #000080;">==</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; TCCR0B <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; TCCR0B <span style="color: #000080;">=</span> pre<span style="color: #008000;">&#91;</span>octave<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; OCR0A <span style="color: #000080;">=</span> oc<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#40;</span>octave<span style="color: #000040;">%</span><span style="color:#800080;">2</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span><span style="color: #0000dd;">12</span> <span style="color: #000040;">+</span> note<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #666666;">// play the scales</span><br />
<span style="color: #0000ff;">void</span> demo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">6</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> j<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">12</span><span style="color: #008080;">;</span> j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; playMidiNote<span style="color: #008000;">&#40;</span>i, j<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; _delay_ms<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">200</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; playMidiNote<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; _delay_ms<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> loop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #666666;">// read the push buttons treating as binary input</span><br />
&nbsp; <span style="color: #0000ff;">int</span> octave <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>digitalRead<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">14</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> LOW<span style="color: #008000;">&#41;</span> <span style="color: #008080;">?</span> <span style="color: #0000dd;">4</span> <span style="color: #008080;">:</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>digitalRead<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">15</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> LOW<span style="color: #008000;">&#41;</span> <span style="color: #008080;">?</span> <span style="color: #0000dd;">2</span> <span style="color: #008080;">:</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>digitalRead<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">16</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> LOW<span style="color: #008000;">&#41;</span> <span style="color: #008080;">?</span> <span style="color: #0000dd;">1</span> <span style="color: #008080;">:</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>octave <span style="color: #000080;">==</span> <span style="color: #0000dd;">7</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// play demo if all pressed</span><br />
&nbsp; &nbsp; demo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #666666;">// see which keyboard 'button' is selected</span><br />
&nbsp; <span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">11</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&gt;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i<span style="color: #000040;">--</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>digitalRead<span style="color: #008000;">&#40;</span>keyboard<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> LOW<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #666666;">// play note (if none selected -1 will stop sound)</span><br />
&nbsp; playMidiNote<span style="color: #008000;">&#40;</span>octave, i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; _delay_ms<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">25</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// debounce switch</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>The &#8217;stylus&#8217; is connected to ground via a small resistor (to limit current incase of accidental connection to +V).  A flickr annotated photo of the circuit is below:</p>
<p><a href="http://www.flickr.com/photos/rocketnumbernine/3389580296/" title="Piezo Keyboard Prototype" target="images"><img src="http://farm4.static.flickr.com/3647/3389580296_dcfe1b48b5_m.jpg" width="240" height="160" alt="Piezo Keyboard Prototype" /></a></p>
<p><strong>Possible improvements:</strong></p>
<p><il></p>
<li>PCB keyboard for input.</li>
<li>A square wave generates a fairly boring tone, its possible to use an interrupt handler to intercept signal generation and create a more colourful signal.  The higher resolution of the 16 bit timer1 may also help here.</li>
<li>Midi file parsing and output?</li>
<p></il></p>


<p>Related posts:<ol><li><a href='http://www.rocketnumbernine.com/2009/03/07/first-arduino-project-the-finger/' rel='bookmark' title='Permanent Link: First Arduino Project: the finger'>First Arduino Project: the finger</a></li>
<li><a href='http://www.rocketnumbernine.com/2009/03/10/which-arduino/' rel='bookmark' title='Permanent Link: Which Arduino'>Which Arduino</a></li>
<li><a href='http://www.rocketnumbernine.com/2009/08/27/smt-table-top-reflow-oven-part-2/' rel='bookmark' title='Permanent Link: SMT Table Top Reflow Oven (part 2): Controlling the Heater Elements'>SMT Table Top Reflow Oven (part 2): Controlling the Heater Elements</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.rocketnumbernine.com/2009/03/27/xyloduino-simple-arduinopiezo-organ/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>First Arduino Project: the finger</title>
		<link>http://www.rocketnumbernine.com/2009/03/07/first-arduino-project-the-finger/</link>
		<comments>http://www.rocketnumbernine.com/2009/03/07/first-arduino-project-the-finger/#comments</comments>
		<pubDate>Sat, 07 Mar 2009 10:15:15 +0000</pubDate>
		<dc:creator>andrew</dc:creator>
				<category><![CDATA[projects]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[photoresistor]]></category>
		<category><![CDATA[seeeduino]]></category>
		<category><![CDATA[servo]]></category>

		<guid isPermaLink="false">http://www.rocketnumbernine.com/blog/?p=43</guid>
		<description><![CDATA[This is a description of my first useful Arduino project &#8211; The Finger.

The external USB drive that&#8217;s a backup device to my Mac Mini media server doesn&#8217;t cope well with being on 24/7. The power supply has had to be replaced once and the disk gets (comparatively) noisy which is annoying as it sits in [...]]]></description>
			<content:encoded><![CDATA[<p>This is a description of my first useful Arduino project &#8211; The Finger.</p>
<p><object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=3516504&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=3516504&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object></p>
<p>The external USB drive that&#8217;s a backup device to my Mac Mini media server doesn&#8217;t cope well with being on 24/7. The power supply has had to be replaced once and the disk gets (comparatively) noisy which is annoying as it sits in my living room under the TV.</p>
<p>The solution was something that would turn the disk on and off when needed. Something controlled by an Arduino communicating with a perl script on the Mac seemed a fun way of doing it.</p>
<p><em>Which Arduino.</em><br />
I used a <a href="http://www.seeedstudio.com/blog/?page_id=110">Seeeduino</a> Arduino compatible board, <a href="http://www.flickr.com/photos/rocketnumbernine/3331255708/" title="Seeeduino Board" target="images"><img src="http://farm4.static.flickr.com/3384/3331255708_e12dc08154_t.jpg" width="100" height="75" class="alignright"  alt="Seeeduino Board" /></a>mainly because the Arduino Duemilanove Boards were out of stock when buying.  But I&#8217;ve grown quite fond of the Seeeduino, the SMD components and mini USB make it look much more compact and looks neat, the ability to turn of auto reset functionality by switch has been very useful.  For prototyping the <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=7914">Sparkfun ProtoShield</a> board fits snugly on top.</p>
<p><em>Pushing the Switch</em><br />
I didn&#8217;t want to take the disk apart or play with mains electricity so it had to be turned on mechanically by pushing the on/off switch.  A servo motor seemed the best choice as they can be easily <a href="http://arduino.cc/en/Reference/Servo">controlled</a> from the Arduino with no extra electronics and can be picked up fairly cheaply compared with linear actuators.  A Servo was also much easier to mount and house on the front of the disk.</p>
<p>Wiring is straightforward: brown cable to Arduino GND pin, red to +5V, and orange to a digital out &#8211; D9.<a href="http://www.flickr.com/photos/rocketnumbernine/3331257510/" target="images" title="Testing the Servo Arm Action"><img src="http://farm4.static.flickr.com/3331/3331257510_fea855132f_m.jpg" width="240" height="180" class="alignleft" alt="Testing the Servo Arm Action" /></a>The test standalone sweep program is below was useful for testing the mechanical assembly and finding out the required amount of movement &#8211; it just sweeps the arm a few degrees, stops for a set period and then sweeps back to neutral position.</p>
<p>If moving slowly enough with little resistance, the servo only drew a max of about 150mA , so can be powered safely from the USB supplied 5V available off the Arduino.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:200px;"><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="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &lt;Servo.h&gt; </span><br />
<br />
<span style="color: #0000ff;">int</span> SERVOPIN <span style="color: #000080;">=</span> <span style="color: #0000dd;">9</span><span style="color: #008080;">;</span><br />
Servo servo<span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">void</span> setup<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <br />
<span style="color: #008000;">&#123;</span> <br />
&nbsp; servo.<span style="color: #007788;">attach</span><span style="color: #008000;">&#40;</span>SERVOPIN<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span> <br />
<br />
<span style="color: #666666;">// sweep the servo from 0 to final holding</span><br />
<span style="color: #666666;">// for hold milliseconds before returning to 0</span><br />
<span style="color: #0000ff;">void</span> servoSweep<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> final, <span style="color: #0000ff;">int</span> hold<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> pos <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> pos <span style="color: #000080;">&lt;</span> final<span style="color: #008080;">;</span> pos <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; servo.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span>pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; delay<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">20</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; delay<span style="color: #008000;">&#40;</span>hold<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> pos <span style="color: #000080;">=</span> final<span style="color: #008080;">;</span> pos <span style="color: #000080;">&gt;=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> pos <span style="color: #000040;">-</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; servo.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span>pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; delay<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">20</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #666666;">// sweep the servo then wait for 5 seconds</span><br />
<span style="color: #0000ff;">void</span> loop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <br />
<span style="color: #008000;">&#123;</span> <br />
&nbsp; servoSweep<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">20</span>, <span style="color: #0000dd;">3000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <br />
&nbsp; delay<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p><em>Detecting state of the box</em></p>
<p>Unfortunately the disk only indicates it&#8217;s on by the light emanating from the grill on the front.  But it&#8217;s trivial to connect up a <a href="http://en.wikipedia.org/wiki/Photoresistor">photoresistor</a> to an analogue pin to detect it.  The photoresistor was put in series with a resistor to form a voltage divider with the midpoint feeding into Analogue pin A0 as shown in the following schematic:</p>
<div id="attachment_45" class="wp-caption aligncenter" style="width: 425px"><img src="http://www.rocketnumbernine.com/blog/wp-content/uploads/2009/03/finger-schematic.png" alt="Schematic for photoresistor and servo connections" title="Schematic" width="415" height="220" class="size-full wp-image-45" /><p class="wp-caption-text">Schematic for photoresistor and servo connections</p></div>
<p><a href="http://www.flickr.com/photos/rocketnumbernine/3331256774/" target="images" title="Testing the Photoresistor"><img src="http://farm4.static.flickr.com/3354/3331256774_6c6e4350f4_m.jpg" class="alignright" width="240" height="180" alt="Testing the Photoresistor" /></a>I found the program below &#8211; which reads the value of the analogue input and sends it to the serial/USB device (which can be viewed from the serial monitor in the Arduino IDE) &#8211; together with a multimeter useful for calculating a useful value for the lower resistor.</p>
<div class="codecolorer-container cpp 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 /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> setup<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <br />
<span style="color: #008000;">&#123;</span> <br />
&nbsp; beginSerial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">19200</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span> <br />
&nbsp;<br />
<span style="color: #666666;">// send the value of A0 to the serial</span><br />
<span style="color: #666666;">// every half second</span><br />
<span style="color: #0000ff;">void</span> loop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <br />
<span style="color: #008000;">&#123;</span> <br />
&nbsp; Serial.<span style="color: #007788;">println</span><span style="color: #008000;">&#40;</span>analogRead<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; delay<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">500</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p><em>Putting It All together.</em><br />
I found a plastic box that the Seeeduino board fits snuggly in (with its battery socket removed and a bit of filing on the corners) with enough room for the servo.<a href="http://www.flickr.com/photos/rocketnumbernine/3330420437/" title="Strip Board" target="images"><img src="http://farm4.static.flickr.com/3387/3330420437_b2351fd6a9_m.jpg" width="240" height="180" class="alignleft" alt="Strip Board" /></a>  There wasn&#8217;t much room for anything else so the extra components went on a piece of strip board connected to the left side header strip (where it gets 5V,GND, and A0) with a header for the servo socket and wire to connect to the digital pin D9 on the other side of the board.<a href="http://www.flickr.com/photos/rocketnumbernine/3331258310/" title="Housing" target="images"><img src="http://farm4.static.flickr.com/3408/3331258310_8bd2dae886_m.jpg" width="240" height="180" class="alignright" alt="Housing" /></a>The photoresistor is positioned at the edge of the hole for the servo arm and gets enough light from the disk.</p>
<p>Vimeo closeup of the device is below: <object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=3516963&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=3516963&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object></p>
<p>The final Arduino program is shown below, it waits for one of two commands from the USB, either &#8220;status&#8221; which will make it reply with the current status of the disk (&#8220;on&#8221; or &#8220;off&#8221;) or &#8220;sweep&#8221; which will make it depress the button (to turn the disk on or off) and then return the sampled status.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:200px;"><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 /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">// servo control/analogue sampling - use as you wish</span><br />
<span style="color: #666666;">// by andrew@rocketnumbernine.com 2009</span><br />
<span style="color: #339900;">#include &lt;Servo.h&gt;</span><br />
<br />
<span style="color: #0000ff;">int</span> ANALOGUE <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">int</span> SERVOPIN <span style="color: #000080;">=</span> <span style="color: #0000dd;">13</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">int</span> BUFSIZE<span style="color: #000080;">=</span><span style="color: #0000dd;">80</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">char</span> cBuf<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">80</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
Servo servo<span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> getStatus<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">int</span> val <span style="color: #000080;">=</span> analogRead<span style="color: #008000;">&#40;</span>ANALOGUE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>val <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #FF0000;">&quot;on&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #FF0000;">&quot;off&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> sendStatus<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; Serial.<span style="color: #007788;">println</span><span style="color: #008000;">&#40;</span>getStatus<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> servoSweep<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> finalPosition, <span style="color: #0000ff;">int</span> holdDuration<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> pos <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> pos <span style="color: #000080;">&lt;</span> finalPosition<span style="color: #008080;">;</span> pos <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; servo.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span>pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; delay<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">20</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; delay<span style="color: #008000;">&#40;</span>holdDuration<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> pos <span style="color: #000080;">=</span> finalPosition<span style="color: #008080;">;</span> pos <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> pos <span style="color: #000040;">-</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; servo.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span>pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; delay<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">20</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> processMessage <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>message<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">strcmp</span><span style="color: #008000;">&#40;</span>message, <span style="color: #FF0000;">&quot;status&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; sendStatus<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">strcmp</span><span style="color: #008000;">&#40;</span>message, <span style="color: #FF0000;">&quot;sweep&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; servoSweep<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">30</span>, <span style="color: #0000dd;">3000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; delay<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; sendStatus<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">char</span> temp<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">80</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> &nbsp;<span style="color: #FF0000;">&quot;unknown command &quot;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">strcat</span><span style="color: #008000;">&#40;</span>temp, message<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; Serial.<span style="color: #007788;">println</span><span style="color: #008000;">&#40;</span>temp<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; <span style="color: #008000;">&#125;</span> <br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> setup<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; pinMode<span style="color: #008000;">&#40;</span>SERVOPIN, OUTPUT<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; servo.<span style="color: #007788;">attach</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">9</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> &nbsp;<br />
&nbsp; servo.<span style="color: #007788;">write</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; Serial.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">19200</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> loop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; getStatus<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>Serial.<span style="color: #007788;">available</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// read the incoming byte:</span><br />
&nbsp; &nbsp; cBuf<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> Serial.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i<span style="color: #000080;">==</span>BUFSIZE<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span> <span style="color: #000040;">||</span> cBuf<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000080;">&lt;</span><span style="color: #0000dd;">41</span> <span style="color: #000040;">||</span> cBuf<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000080;">&gt;</span><span style="color: #0000dd;">176</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; cBuf<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; processMessage<span style="color: #008000;">&#40;</span>cBuf<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; i<span style="color: #000040;">++</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span> &nbsp;<br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>I used perl on the mac to send the commands to the Arduino (I&#8217;m sure this can be done in 3 lines of ruby, but I know perl).  Usage is fairly straightforward:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># disk-control status</span><br />
device off<br />
<span style="color: #666666; font-style: italic;"># disk-control off &nbsp; </span><br />
device reports it is off, use <span style="color: #660033;">-force</span> off to change anyway<br />
<span style="color: #666666; font-style: italic;"># disk-control on</span><br />
device on<br />
<span style="color: #666666; font-style: italic;"># disk-control status</span><br />
device on</div></div>
<p>Finally the perl source is below &#8211; it uses Device::SerialPort which may needed to be installed (&#8220;cpan Device::SerialPort&#8221;).</p>
<div class="codecolorer-container perl default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:200px;"><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 /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#! /usr/bin/perl</span><br />
<span style="color: #666666; font-style: italic;"># Arduino control perl script - use as you wish</span><br />
<span style="color: #666666; font-style: italic;"># by andrew@rocketnumbernine.com 2009</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> Device<span style="color: #339933;">::</span><span style="color: #006600;">SerialPort</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #0000ff;">$SERIAL</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;/dev/tty.usbserial-A9009rh1&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #0000ff;">$USAGE</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;usage: disk-control.pl [-force] [on|off]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># connect to USB as serial</span><br />
<span style="color: #000000; font-weight: bold;">sub</span> serialConnect<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$port</span> <span style="color: #339933;">=</span> Device<span style="color: #339933;">::</span><span style="color: #006600;">SerialPort</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$SERIAL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$port</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">databits</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$port</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">baudrate</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">19200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$port</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">parity</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;none&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$port</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">stopbits</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">return</span> <span style="color: #0000ff;">$port</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># wait for arduino to send something</span><br />
<span style="color: #000000; font-weight: bold;">sub</span> readSerial<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">$data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$port</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">lookfor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066;">chomp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066;">return</span> <span style="color: #0000ff;">$data</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066;">sleep</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$i</span><span style="color: #339933;">++;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">return</span> <span style="color: #ff0000;">&quot;timeout waiting for finger<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># get the current status of the device ('on'/'off')</span><br />
<span style="color: #000000; font-weight: bold;">sub</span> getStatus<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$port</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">write</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;status<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$data</span> <span style="color: #339933;">=</span> readSerial<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$data</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/[^\w]*//g</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">return</span> <span style="color: #0000ff;">$data</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># change the status of the device</span><br />
<span style="color: #666666; font-style: italic;"># checks current status and only calls sweep if </span><br />
<span style="color: #666666; font-style: italic;"># different from desired or $force has been set</span><br />
<span style="color: #000000; font-weight: bold;">sub</span> changeStatus<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$desiredState</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$status</span> <span style="color: #339933;">=</span> getStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$desiredState</span> <span style="color: #b1b100;">eq</span> <span style="color: #0000ff;">$status</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #0000ff;">$force</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;device reports it is $status, use &quot;</span><br />
&nbsp; <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;-force $desiredState to change anyway<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <br />
&nbsp; <span style="color: #000066;">exit</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$port</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">write</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;sweep<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #0000ff;">$port</span> <span style="color: #339933;">=</span> serialConnect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #0000ff;">$message</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span> <span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$message</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/-force/</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$force</span> <span style="color: #339933;">=</span> true<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$message</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span> <span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># either try to change status</span><br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$message</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/on|off/</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; changeStatus<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;"># or request current status</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$port</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">write</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;status<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;device &quot;</span> <span style="color: #339933;">.</span> readSerial<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #0000ff;">$port</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>


<p>Related posts:<ol><li><a href='http://www.rocketnumbernine.com/2009/03/27/xyloduino-simple-arduinopiezo-organ/' rel='bookmark' title='Permanent Link: Xyloduino: Simple Arduino/piezo organ'>Xyloduino: Simple Arduino/piezo organ</a></li>
<li><a href='http://www.rocketnumbernine.com/2009/03/10/which-arduino/' rel='bookmark' title='Permanent Link: Which Arduino'>Which Arduino</a></li>
<li><a href='http://www.rocketnumbernine.com/2009/07/03/using-spi-on-an-avr-3/' rel='bookmark' title='Permanent Link: Using SPI on an AVR (3)'>Using SPI on an AVR (3)</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.rocketnumbernine.com/2009/03/07/first-arduino-project-the-finger/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
