<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Channel 9</title>
    <atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Niners/c4f.Tim-Farley/Posts/RSS"></atom:link>
    <itunes:summary></itunes:summary>
    <itunes:author>Microsoft</itunes:author>
    <itunes:subtitle></itunes:subtitle>
    <image>
      <url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url>
      <title>Channel 9</title>
      <link>http://channel9.msdn.com/Niners/c4f.Tim-Farley/Posts</link>
    </image>
    <itunes:image href=""></itunes:image>
    <itunes:category text="Technology"></itunes:category>
    <description>Channel 9 keeps you up to date with the latest news and behind the scenes info from Microsoft that developers love to keep up with. From LINQ to SilverLight – Watch videos and hear about all the cool technologies coming and the people behind them.</description>
    <link>http://channel9.msdn.com/Niners/c4f.Tim-Farley/Posts</link>
    <language>en</language>
    <pubDate>Sat, 25 May 2013 08:26:44 GMT</pubDate>
    <lastBuildDate>Sat, 25 May 2013 08:26:44 GMT</lastBuildDate>
    <generator>Rev9</generator>
    <c9:totalResults>1</c9:totalResults>
    <c9:pageCount>1</c9:pageCount>
    <c9:pageSize>25</c9:pageSize>
  <item>
      <title>Remote Controlled lunaring with Arduino</title>
      <description><![CDATA[
<p></p>
<div id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:56a2b4c6-feff-43d5-a5a8-49ebe34e7ee5" class="wlWriterEditableSmartContent">
<div id="70103c64-82b3-42f5-a873-90c4b319e9b4">
<div><a href="http://www.youtube.com/watch?v=-AqTJ7hOJTk" target="_new"><img src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/9727393/video9579a69d1e46.jpg" alt=""></a></div>
</div>
</div>
<p></p>
<p>Nothing puts the fun back into coding like watching your very own creations come to life in the physical world. Luckily, with the current glut of high-tech toys and electronic gadgets on the market, it's never been easier to experience the thrill of designing
 your own hardware by modifying what's already available. A great way to get into the hobby is to wander the isles of your local toy store or browse the pages of an online novelty shop and just let your imagination run wild. I try to go through the toy department
 with a certain child-like wonderment. I look at everything on every shelf and try to imagine what might be done to improve upon the design, to make it more interesting, or just plain more fun.</p>
<p>In this article, we'll take a quick look at this hobby by exploring one of my most well received projects to date. We'll take a simple night-light that cycles through the phases of the moon based on input from an infrared remote control and modify it so
 that it displays the phases of the moon based off the actual calendar date instead.</p>

<h2><strong>Gathering the materials</strong></h2>
<p>Let's take a moment to think about what is needed in order for our project to work.</p>
<p>Obviously, we need some way to keep track of the current date, and preferably retain that date even when the batteries run out. We also need some means of controlling the LEDs on the device and to calculate the current lunar phase based on that date. In
 addition, it would be nice to have some user-friendly method by which we could update the stored date, should it start to drift over time.</p>
<p>As it happens, everything we need to get our prototype off the ground is readily available – and for a reasonable price.</p>
<p>Arduino: The Arduino is an open source electronics prototyping platform designed to help artists and designers rapidly prototype their ideas; it allows designers to write their programs in a highly abstracted form of the C programming language that hides
 a lot of the technical underpinnings of microcontroller programming. Because it is open source, the Arduino comes in many varieties. You can even build one yourself. A pre-built Arduino typically runs around $30 or less, but building one yourself can be significantly
 cheaper.</p>
<p>DS1307 Real Time Clock Module: While the Arduino offers a powerful and flexible platform on which to build our project, we still need some way to keep track of the date. For this, we will use a DS1307 real time clock module. I opted to go with the offering
 from SparkFun Electronics, a Colorado-based company that specialies in providing drop-in solutions for hobbyists and prototypes. This DS1307 module comes pre-configured with an oscillator, coin cell battery backup, and ready to communicate with the Arduino
 using a standard I2C two-wire bus.</p>
<p>I also chose to add a standard 100k potentiometer (variable resistor) in-line with the existing Light-dependent resistor (LDR) so that we can easily tweak the moon's sensitivity to light in different environments.</p>
<p>Finally, in order to update the DS1307 clock, I wrote a very simple C# application that simply queries the system date of your PC and passes that to the Arduino which in turn uses the I2C bus to set the date on the DS1307.</p>
<h2>Getting Started</h2>
<p>If you've ever peered into the mysterious inner workings of an electronic device, then you'll appreciate how extremely straightforward this moon really is.</p>
<p>The video below will give you a basic overview of the hardware that we're using.</p>
<p></p>
<div id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:a8e6d2fd-9c29-451f-8a0c-fb2bbd7a099d" class="wlWriterEditableSmartContent">
<div id="a04de03b-96be-43ad-8e0e-909e11f1b479">
<div><a href="http://www.youtube.com/watch?v=F0yKRRh9LF0" target="_new"><img src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/9727393/video1f1d06ee829c.jpg" alt=""></a></div>
</div>
</div>
<p></p>
<p><b></b><b></b></p>
<p>Essentially, all we've done here is to remove the existing controller from the moon and replace it with our Arduino controller, which is in-turn connected to the DS1307 RTC.</p>
<p><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/9727393/clip_image002_2.jpg"><img title="moon_schematic[1]" border="0" alt="moon_schematic[1]" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/9727393/moon_schematic_5B1_5D_271671bc-41a5-4a5a-b599-2bb4bf97a037.png" width="500" height="312">
</a></p>
<p>If you're unfamiliar with how to read electronic schematic diagrams, the table below explains how the parts are interconnected.
<table border="1" cellspacing="0" cellpadding="0" width="447">
<tbody>
<tr>
<td valign="top" width="165">
<p>Arduino pin:</p>
</td>
<td valign="top" width="280">
<p>Connected to:</p>
</td>
</tr>
<tr>
<td valign="top" width="165">
<p>Digital pin 3</p>
</td>
<td valign="top" width="280">
<p>Center leg of potentiometer</p>
</td>
</tr>
<tr>
<td valign="top" width="165">
<p>Digital pins 7-12</p>
</td>
<td valign="top" width="280">
<p>LED lights (Red 7 … Purple 12)</p>
</td>
</tr>
<tr>
<td valign="top" width="165">
<p>Analog pin 5</p>
</td>
<td valign="top" width="280">
<p>DS1307 SCL pin</p>
</td>
</tr>
<tr>
<td valign="top" width="165">
<p>Analog pin 4</p>
</td>
<td valign="top" width="280">
<p>DS1307 SDA pin</p>
</td>
</tr>
<tr>
<td valign="top" width="165">
<p>GND</p>
</td>
<td valign="top" width="280">
<p>GND of DS1307</p>
</td>
</tr>
<tr>
<td valign="top" width="165">
<p>GND</p>
</td>
<td valign="top" width="280">
<p>(-) Negative battery Terminal</p>
</td>
</tr>
<tr>
<td valign="top" width="165">
<p>GND</p>
</td>
<td valign="top" width="280">
<p>Outside leg of potentiometer</p>
</td>
</tr>
<tr>
<td valign="top" width="165">
<p>GND</p>
</td>
<td valign="top" width="280">
<p>GND (Brown) wire from LED panel</p>
</td>
</tr>
<tr>
<td valign="top" width="165">
<p>5V</p>
</td>
<td valign="top" width="280">
<p>One leg of LDR</p>
</td>
</tr>
<tr>
<td valign="top" width="165">
<p>5V</p>
</td>
<td valign="top" width="280">
<p>5V of DS1307</p>
</td>
</tr>
<tr>
<td valign="top" width="165">
<p>VIN</p>
</td>
<td valign="top" width="280">
<p>Center leg of power switch.</p>
</td>
</tr>
</tbody>
</table>
</p>
<p>Also connect:
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="217">
<p>Unused leg of LDR</p>
</td>
<td valign="top" width="373">
<p>Unused leg of potentiometer</p>
</td>
</tr>
<tr>
<td valign="top" width="217">
<p>Outside leg of power switch</p>
</td>
<td valign="top" width="373">
<p>(&#43;) Positive battery terminal</p>
</td>
</tr>
</tbody>
</table>
</p>
<p>If it all sounds too complicated. It isn't. Once you have the components in your hands, you can easily identify the parts listed above and put everything together by simply using the table above as a checklist.</p>
<p><b>The Code</b></p>
<p>The code for interfacing the Arduino with the DS1307 module is available in the Arduino playground. The Arduino playground is a fantastic resource of community-contributed code samples that you can use in your own projects to interact with a remarkable array
 of hardware devices and software platforms.</p>
<p>Once we retrieve the date from the DS1307, we can calculate the phase of the moon in our Arduino program:</p>
<pre class="csharpcode"><span class="rem">// Adapted from Stephen R. Schmitt's Lunar phase computation program.</span>
<span class="rem">// Originally written for the Zeno programming language.</span>
<span class="rem">// http://home.att.net/~srschmitt/lunarphasecalc.html</span>

<span class="kwrd">byte</span> getPhase(<span class="kwrd">int</span> Y, <span class="kwrd">int</span> M, <span class="kwrd">int</span> D) {
  <span class="kwrd">double</span> AG, IP;
  <span class="kwrd">byte</span> phase;
  <span class="kwrd">long</span> YY, MM, K1, K2, K3, JD;
  YY = Y - floor((12 - M) / 10);
  MM = M &#43; 9;
  <span class="kwrd">if</span>(MM &gt;= 12)
    MM = MM - 12;
  
  K1 = floor(365.25 * (YY &#43; 4712));
  K2 = floor(30.6 * MM &#43; 0.5);
  K3 = floor(floor((YY / 100) &#43; 49) * 0.75) - 38;

  JD = K1 &#43; K2 &#43; D &#43; 59;
  <span class="kwrd">if</span>(JD &gt; 2299160)
    JD = JD -K3;
  
  IP = normalize((JD - 2451550.1) / 29.530588853);
  AG = IP*29.53;
  
  <span class="kwrd">if</span>(AG &lt; 1.20369)
    phase = B00000000;
  <span class="kwrd">else</span> <span class="kwrd">if</span>(AG &lt; 3.61108)
    phase = B00000001;
  <span class="kwrd">else</span> <span class="kwrd">if</span>(AG &lt; 6.01846)
    phase = B00000011;
  <span class="kwrd">else</span> <span class="kwrd">if</span>(AG &lt; 8.42595)
    phase = B00000111;
  <span class="kwrd">else</span> <span class="kwrd">if</span>(AG &lt; 10.83323)
    phase = B00001111;
  <span class="kwrd">else</span> <span class="kwrd">if</span>(AG &lt; 13.24062)
    phase = B00011111;
  <span class="kwrd">else</span> <span class="kwrd">if</span>(AG &lt; 15.64800)
    phase = B00111111;
  <span class="kwrd">else</span> <span class="kwrd">if</span>(AG &lt; 18.05539)
    phase = B00111110;
  <span class="kwrd">else</span> <span class="kwrd">if</span>(AG &lt; 20.46277)
    phase = B00111100;
  <span class="kwrd">else</span> <span class="kwrd">if</span>(AG &lt; 22.87016)
    phase = B00111000;
  <span class="kwrd">else</span> <span class="kwrd">if</span>(AG &lt; 25.27754)
    phase = B00110000;
  <span class="kwrd">else</span> <span class="kwrd">if</span>(AG &lt; 27.68493)
    phase = B00100000;
  <span class="kwrd">else</span>
    phase = 0;
  
  <span class="kwrd">return</span> phase;    
}</pre>
<style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style>
<p>Finally, the Arduino is capable of communicating with a PC via standard serial communication thanks to the FTDI USB-to-serial controller that comes standard with many Arduino boards.</p>
<p><strong>C#:</strong></p>
<pre class="csharpcode"><span class="rem">// Use a button Click event to send data to the Arduino.</span>
<span class="rem">// The following example is intended to be a very basic example</span>
<span class="rem">// of writing the date to the serial port. A more robust application</span>
<span class="rem">// would use a protocol and CRC to ensure that the data being transmitted</span>
<span class="rem">// was valid.</span>
<span class="kwrd">private</span> <span class="kwrd">void</span> updateButton_Click(<span class="kwrd">object</span> sender, EventArgs e)
{
    <span class="kwrd">byte</span>[] result = <span class="kwrd">new</span> <span class="kwrd">byte</span>[7];

    DateTime currTime = DateTime.Now;

    serialPort.PortName = “COM4”;
    serialPort.BaudRate = 19200;
    serialPort.DataBits = 8;
    serialPort.Parity = Parity.None;
    serialPort.StopBits = StopBits.One;

    <span class="kwrd">try</span>
    {
        serialPort.Open();
    }
    <span class="kwrd">catch</span> (IOException ioe)
    {
         <span class="rem">// Handle your exceptions.</span>
    }

    <span class="rem">// Some Arduino models reboot automatically when the serial</span>
    <span class="rem">// port is open. Allow a small delay to account for this.</span>
    Thread.Sleep(10000);

    result[0] = BitConverter.GetBytes(currTime.Second)[0];
    result[1] = BitConverter.GetBytes(currTime.Minute)[0];
    result[2] = BitConverter.GetBytes(currTime.Hour)[0];
    result[3] = BitConverter.GetBytes(1)[0];
    result[4] = BitConverter.GetBytes(currTime.Day)[0];
    result[5] = BitConverter.GetBytes(currTime.Month)[0];
    result[6] = BitConverter.GetBytes((currTime.Year % 2000))[0];
    
    <span class="kwrd">try</span>
    {
         serialPort.Write(result, 0, result.Length);
         serialPort.Close();
    }
    <span class="kwrd">catch</span> (InvalidOperationException ioe)
    {
         <span class="rem">// Handle more exceptions.</span>
    }
    <span class="kwrd">catch</span> (TimeoutException te)
    {
         <span class="rem">// Handle more exceptions.</span>
    }
}</pre>
<p><b>Arduino</b></p>
<p>All that's left is for the Arduino to read and process the data. Using our extremely simple example above, let's take a look at some Arduino code.</p>
<pre class="csharpcode"><span class="kwrd">void</span> loop() {
  <span class="kwrd">while</span>(Serial.available()) {
    reader = Serial.read();
    <span class="kwrd">if</span>(reader == 255) {
      <span class="rem">// do nothing - dummy byte</span>
    }
    <span class="kwrd">else</span> {
        <span class="rem">// populate our array with values</span>
      needsUpdate = <span class="kwrd">true</span>;
      update[addr] = reader;
      addr &#43;= 1;
    }
  }
  
    <span class="rem">// Once we have all the values, update the clock.</span>
  <span class="kwrd">if</span>((needsUpdate) &amp;&amp; (addr == 7)) { 
    setDateDs1307(update[0],
          update[1],
          update[2],
          update[3],
          update[4],
          update[5],
          update[6]);
      <span class="rem">// Store the last two digits of the year in EEPROM.</span>
    EEPROM.write(0, update[6]);
  }
  delay(100);
  needsUpdate = <span class="kwrd">false</span>;
  addr = 0;
}</pre>
<h2>Conclusion</h2>
<p>That's really all there is to it. Feel free to check out the complete code samples at imagitronics.org, or strike out on your own. Take part in the true spirit of the hobby by extending or modifying the moon to meet your own ends: implement a communication
 protocol, create a more robust desktop application, find ways to make the device cheaper using a USB HID controller and allowing the moon to run from USB power.
</p>
<p>My intent in writing this article was to get people excited about the prospect hardware design and prototyping and to remove any preconceived notions about the difficulty of such projects.</p>
<p>Most importantly, just remember my motto: Dream it, build it, break it, repeat.</p>
<h4>Special Thanks</h4>
<ul>
<li>John Vaughters's <a href="http://combustory.com/wiki/index.php/RTC1307_-_Real_Time_Clock">
DS1307 RTC functions</a> </li><li>SparkFun Electronics' <a href="http://www.opencircuits.com/SFE_Footprint_Library_Eagle">
EAGLE CAD library</a> </li><li>Rob Faludi's <a href="http://www.faludi.com/2008/10/07/arduino-avr-library-for-eagle-layout-editor/">
Arduino EAGLE CAD library</a> </li><li>Stephen Schmitt's Zeno programming language <a href="http://home.att.net/~srschmitt/lunarphasecalc.html">
lunar phase sample</a> </li></ul>
<h2>About the author</h2>
<p>Tim Farley is an Iraq war veteran, Systems Integrator for the Department of Defense (DoD), military spouse, and sole maintainer of
<a href="http://www.imagitronics.org">imagitronics.org</a>; a site devoted to his personal projects and hobbies. Although he has been working in and around government agencies for over 12 years, he hopes to someday break into world of toy/game research and
 design.</p>
 <img src="http://m.webtrends.com/dcs1wotjh10000w0irc493s0e_6x1g/njs.gif?dcssip=channel9.msdn.com&dcsuri=http://channel9.msdn.com/Niners/c4f.Tim-Farley/Posts/RSS&WT.dl=0&WT.entryid=Entry:RSSView:1187a8574bf0410781b39e7600cb8ded">]]></description>
      <comments>http://channel9.msdn.com/coding4fun/articles/Remote-Controlled-lunaring-with-Arduino</comments>
      <itunes:summary>
 





 
Nothing puts the fun back into coding like watching your very own creations come to life in the physical world. Luckily, with the current glut of high-tech toys and electronic gadgets on the market, it&#39;s never been easier to experience the thrill of designing
 your own hardware by modifying what&#39;s already available. A great way to get into the hobby is to wander the isles of your local toy store or browse the pages of an online novelty shop and just let your imagination run wild. I try to go through the toy department
 with a certain child-like wonderment. I look at everything on every shelf and try to imagine what might be done to improve upon the design, to make it more interesting, or just plain more fun. 
In this article, we&#39;ll take a quick look at this hobby by exploring one of my most well received projects to date. We&#39;ll take a simple night-light that cycles through the phases of the moon based on input from an infrared remote control and modify it so
 that it displays the phases of the moon based off the actual calendar date instead. 

Gathering the materials
Let&#39;s take a moment to think about what is needed in order for our project to work. 
Obviously, we need some way to keep track of the current date, and preferably retain that date even when the batteries run out. We also need some means of controlling the LEDs on the device and to calculate the current lunar phase based on that date. In
 addition, it would be nice to have some user-friendly method by which we could update the stored date, should it start to drift over time. 
As it happens, everything we need to get our prototype off the ground is readily available – and for a reasonable price. 
Arduino: The Arduino is an open source electronics prototyping platform designed to help artists and designers rapidly prototype their ideas; it allows designers to write their programs in a highly abstracted form of the C programming language that hides
 a lot of the technical underpinnings of microcon</itunes:summary>
      <link>http://channel9.msdn.com/coding4fun/articles/Remote-Controlled-lunaring-with-Arduino</link>
      <pubDate>Tue, 18 Aug 2009 16:01:00 GMT</pubDate>
      <guid isPermaLink="false">http://channel9.msdn.com/coding4fun/articles/Remote-Controlled-lunaring-with-Arduino</guid>
      <media:thumbnail url="http://ecn.channel9.msdn.com/o9/c4f/images/9727393_100.jpg" height="75" width="100"></media:thumbnail>
      <media:thumbnail url="http://ecn.channel9.msdn.com/o9/c4f/images/9727393_220.jpg" height="165" width="220"></media:thumbnail>      
      <dc:creator>Tim Farley</dc:creator>
      <itunes:author>Tim Farley</itunes:author>
      <slash:comments>1</slash:comments>
      <wfw:commentRss>http://channel9.msdn.com/coding4fun/articles/Remote-Controlled-lunaring-with-Arduino/RSS</wfw:commentRss>
      <category>Hardware</category>
    </item>    
</channel>
</rss>