<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5274993052759678254</id><updated>2024-08-30T14:21:16.105-04:00</updated><category term="Arduino"/><category term="Java"/><category term="Android"/><category term="Concurrency"/><category term="Threads"/><title type='text'>Amazing Blather</title><subtitle type='html'>This is where I rant. Some times it will make sense.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://amazingblather.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://amazingblather.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Corey</name><uri>http://www.blogger.com/profile/02228538267213653947</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='14' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQBPkpS5fGUzddc1-VnM148vyHgYVz-RUrtlJsmP85shD3EFGIrNO2fjWz_oj_gnIkCctYuiCZ0lPMvxpU-zC-xMDg6ddysToKE0SPeb9RGIs2BSi8Z-vqXWcJFNfAxFs/s220/cimg0084-1.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>10</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5274993052759678254.post-1268741759225388862</id><published>2011-02-06T22:09:00.000-05:00</published><updated>2011-02-06T22:09:47.594-05:00</updated><title type='text'>Motorola&#39;s Xoom is more expensive than Apple&#39;s best iPad and it doesn&#39;t matter.</title><content type='html'>With Motorola &lt;a href=&quot;http://www.youtube.com/watch?v=8BPFODsob1I&quot;&gt;pitting&lt;/a&gt; their tablet against Apple&#39;s mentality. I expected the Xoom to be the same price as the best iPad, maybe less.&amp;nbsp;Engadget&amp;nbsp;&lt;a href=&quot;http://www.engadget.com/2011/02/06/best-buy-ad-prices-motorola-xoom-at-800-affirms-february-24th/&quot;&gt;posted&lt;/a&gt;&amp;nbsp;the price of the Xoom, it&#39;s $799 before any service contracts. That&#39;s $30 less than the &lt;a href=&quot;http://store.apple.com/us/browse/home/shop_ipad/family/ipad?aid=AIC-WWW-NAUS-K2-BUYNOW-IPAD-INDEX&amp;amp;cp=BUYNOW-IPAD-INDEX&quot;&gt;best&lt;/a&gt; iPad at $829.&lt;br /&gt;
&lt;br /&gt;
Sadly, you get less with the Xoom.&lt;br /&gt;
&lt;br /&gt;
The Xoom comes with a&amp;nbsp;paltry&amp;nbsp;1 GB of internal storage. To make up the memory difference an SD card is required. All of the 64 GB SD cards that I &lt;a href=&quot;http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Delectronics&amp;amp;field-keywords=64+GB+SD+Card&amp;amp;x=0&amp;amp;y=0&quot;&gt;find&lt;/a&gt; are over $100.&amp;nbsp;With the SD card the Xoom is $899, and the iPad $829.&lt;br /&gt;
&lt;br /&gt;
The Xoom is at least $70 more expensive. That&#39;s before the astronomically priced data plans.&lt;br /&gt;
&lt;br /&gt;
Which is where the real money is. Both Verizon and AT&amp;amp;T really gouge their customers on these data plans.&amp;nbsp;To be fair&amp;nbsp;&lt;a href=&quot;http://www.verizonwireless.com/b2c/mobilebroadband/?page=plans&quot;&gt;Verizon&#39;s data plans&lt;/a&gt;&amp;nbsp;aren&#39;t as bad as&amp;nbsp;&lt;a href=&quot;http://www.att.com/shop/wireless/devices/ipad.jsp&quot;&gt;AT&amp;amp;T&#39;&lt;/a&gt;s. 10GB on Verizon&#39;s network costs $80. On AT&amp;amp;T&#39;s that cost is $125. Within 2 months on most of AT&amp;amp;T&#39;s network the cost of the Xoom is recouped.&lt;br /&gt;
&lt;br /&gt;
That&#39;s an incredible amount of money. Even as a successful professional with a penchant for expensive gadgets, the first year cost&amp;nbsp;($1860)&amp;nbsp;of a Xoom makes me gag. There are so many other gadgets that I could buy with that money, and still buy a &lt;a href=&quot;http://www.t-mobile.com/shop/plans/Cell-Phone-Plans.aspx?catgroup=MobileBroadband&amp;amp;uid=Shop_2_4&quot;&gt;data plan from T-Mobile&lt;/a&gt; for half the cost.&lt;br /&gt;
&lt;br /&gt;
Motorola may be touting the Xoom as a change in mentality, away from Apple. The &quot;Apple Premium&quot; is a well known cost, what&#39;s different about the Xoom&#39;s pricing?</content><link rel='replies' type='application/atom+xml' href='http://amazingblather.blogspot.com/feeds/1268741759225388862/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingblather.blogspot.com/2011/02/motorolas-xoom-is-more-expensive-than.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/1268741759225388862'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/1268741759225388862'/><link rel='alternate' type='text/html' href='http://amazingblather.blogspot.com/2011/02/motorolas-xoom-is-more-expensive-than.html' title='Motorola&#39;s Xoom is more expensive than Apple&#39;s best iPad and it doesn&#39;t matter.'/><author><name>Corey</name><uri>http://www.blogger.com/profile/02228538267213653947</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='14' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQBPkpS5fGUzddc1-VnM148vyHgYVz-RUrtlJsmP85shD3EFGIrNO2fjWz_oj_gnIkCctYuiCZ0lPMvxpU-zC-xMDg6ddysToKE0SPeb9RGIs2BSi8Z-vqXWcJFNfAxFs/s220/cimg0084-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5274993052759678254.post-6420048744315173083</id><published>2011-01-22T13:46:00.000-05:00</published><updated>2011-01-22T13:46:58.675-05:00</updated><title type='text'>The impossible device</title><content type='html'>&lt;div&gt;&amp;nbsp;&amp;nbsp;As I followed the onslaught of Android tablets announced at CES this year, each device I read about had at least one flaw that was severe enough to remove it from consideration as my next gadget.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;The&amp;nbsp;&lt;a href=&quot;http://www.motorola.com/staticfiles/Consumers/US-EN/XOOM/index.html#/features&quot;&gt;Motorola Xoom&lt;/a&gt;&amp;nbsp;is on Verizon and is rumored to be $800. The&amp;nbsp;&lt;a href=&quot;http://ces2011.asus.com/press-release/asus-tablet-computers-providing-choice-through-innovation-at-ces-2011/&quot;&gt;Asus Transformer&lt;/a&gt;&amp;nbsp;is running Android 2.2 and is heavily skinned by Asus. The &lt;a href=&quot;http://www.motorola.com/Consumers/US-EN/Consumer-Product-and-Services/Mobile-Phones/Motorola-ATRIX-US-EN&quot;&gt;Motorola Atrix&lt;/a&gt; (the best device of CES) is on AT&amp;amp;T; with capped data service having such a device is pointless. The &lt;a href=&quot;http://www.notionink.com/&quot;&gt;Notion Ink Adam&lt;/a&gt; is heavily skinned, with miserable resolution. The list of disappointments goes on.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;I began thinking about what I would want from my next gadget. I have a set digital needs that my laptop, e-book reader, and phone do not meet.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;i&gt;&amp;nbsp;&amp;nbsp;Reading&lt;/i&gt;&amp;nbsp;digital media is the foremost need I have. Between digital references for my professional work, fiction for pleasure, and materials for classes I&#39;m reading more than I ever have before. Reading is a tough activity to facilitate with a digital device. The device must be light enough to use comfortably for long periods of time, the screen must be large, preferably 8.5&quot; x 11&quot; to match the original format. The dot-pitch must be high, and the screen must be readable in sunlight and in darkness. &lt;a href=&quot;http://www.slashgear.com/second-qualcomm-mirasol-plant-in-works-amid-2bn-display-investment-2098334/&quot;&gt;Qualcomm and Mirasol&lt;/a&gt; are still a long way off from making a large screen.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&lt;i&gt;Note Taking&lt;/i&gt;&amp;nbsp;after testing many different applications for taking notes, xournal, org-mode, Google docs, and even Gimp. Nothing beats a pencil and paper for recording high quality notes. Software and a stylus solves this problem. Stylus&#39; are a dime a dozen, sadly there&#39;s no software available that does an adequate job.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&lt;i&gt;Linux&lt;/i&gt;&amp;nbsp;it&#39;s impossible for me to get by in class or at work without a constantly ready Linux environment. Ideally there would be a virtual machine running a user maintained Linux. Access to the virtual machine would be through an application such as VNC.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &lt;i&gt;A portable keyboard clamshell &amp;amp; desktop dock&lt;/i&gt;&amp;nbsp;are needed to interact with the Linux environment. An on-screen keyboard is useful for only short messages, nothing more. To have a useful Linux environment the device needs a desktop dock for the home and the office, and a clamshell to stuff in the messenger bag.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&lt;i&gt;Android or Chrome&lt;/i&gt;&amp;nbsp;nearly all of my online activities pass through Google&#39;s services. After going Google with my browser and now my phone. I prefer most of my Google interactions through my &quot;native&quot; phone applications.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&lt;i&gt;Connectivity&lt;/i&gt;&amp;nbsp;the device must always have an unfettered data and voice connection. Seedy contracts with capped data usage and overage charges are designed to gouge the consumer. I&#39;m not willing to pay for a such a service. I will however, pay a premium to be given unlimited data and voice connectivity.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&lt;i&gt;Wireless power and long battery life&lt;/i&gt;&amp;nbsp;there&#39;s no room in my bag for power cords. Thankfully&amp;nbsp;&lt;a href=&quot;http://www.energizer.com/inductive/product-overview.aspx&quot;&gt;Qi&lt;/a&gt;&amp;nbsp;base stations exist, the device just needs a receiver.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;With the exception of the large Mirasol display all the technologies exist. Sadly I&#39;ll never see a device that meets most of my needs. I&#39;m willing to pay, I&#39;m willing to put the effort into customizing a device that comes close to what I want.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;In the mean time I&#39;ll continue to be disappointed.&amp;nbsp;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingblather.blogspot.com/feeds/6420048744315173083/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingblather.blogspot.com/2011/01/impossible-device.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/6420048744315173083'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/6420048744315173083'/><link rel='alternate' type='text/html' href='http://amazingblather.blogspot.com/2011/01/impossible-device.html' title='The impossible device'/><author><name>Corey</name><uri>http://www.blogger.com/profile/02228538267213653947</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='14' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQBPkpS5fGUzddc1-VnM148vyHgYVz-RUrtlJsmP85shD3EFGIrNO2fjWz_oj_gnIkCctYuiCZ0lPMvxpU-zC-xMDg6ddysToKE0SPeb9RGIs2BSi8Z-vqXWcJFNfAxFs/s220/cimg0084-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5274993052759678254.post-8447243784917844891</id><published>2010-11-24T19:29:00.000-05:00</published><updated>2010-11-24T19:29:31.728-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android"/><title type='text'>CTGenR8 now available!</title><content type='html'>&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;CTGenR8&lt;/span&gt; is now available on the Android market.&lt;br /&gt;
&lt;br /&gt;
It&#39;s my only and worst application, with a very limited purpose. I use this to generate integers and characters to test the solutions to the algorithms we study in class.&lt;br /&gt;
&lt;br /&gt;
I learned a lot about Android, and a fair bit more about the publishing side than I wanted to. In the end it&#39;s all very rewarding to be able to post a QR code for my application on the Android market!&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://qrcode.kaywa.com/img.php?s=8&amp;amp;d=market%3A%2F%2Fsearch%3Fq%3DCorey%2BTessler&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://qrcode.kaywa.com/img.php?s=8&amp;amp;d=market%3A%2F%2Fsearch%3Fq%3DCorey%2BTessler&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Enjoy, happy generating. Please send me your bugs!&lt;br /&gt;
&lt;a href=&quot;http://qrcode.kaywa.com/img.php?s=8&amp;amp;d=market%3A%2F%2Fsearch%3Fq%3DCorey%2BTessler&quot;&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingblather.blogspot.com/feeds/8447243784917844891/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingblather.blogspot.com/2010/11/ctgenr8-now-available.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/8447243784917844891'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/8447243784917844891'/><link rel='alternate' type='text/html' href='http://amazingblather.blogspot.com/2010/11/ctgenr8-now-available.html' title='CTGenR8 now available!'/><author><name>Corey</name><uri>http://www.blogger.com/profile/02228538267213653947</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='14' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQBPkpS5fGUzddc1-VnM148vyHgYVz-RUrtlJsmP85shD3EFGIrNO2fjWz_oj_gnIkCctYuiCZ0lPMvxpU-zC-xMDg6ddysToKE0SPeb9RGIs2BSi8Z-vqXWcJFNfAxFs/s220/cimg0084-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5274993052759678254.post-4869197293184084991</id><published>2010-10-28T21:10:00.000-04:00</published><updated>2010-10-28T21:10:20.611-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Concurrency"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Threads in Java can be simpler</title><content type='html'>&lt;i&gt;Apologies, I&#39;ve been waiting for my project presentation for class before posting this.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
In a&amp;nbsp;&lt;a href=&quot;http://amazingblather.blogspot.com/2010/09/threads-in-java-are-not-simple.html&quot;&gt;previous post&lt;/a&gt;&amp;nbsp;I complained about the Java Concurrency example. It showed how unimpressive and needlessly complicated threading can be in Java. What bothers me most isn&#39;t the concept of &lt;i&gt;wait()&lt;/i&gt; and &lt;i&gt;notify()&lt;/i&gt;. It is the decision to explain &lt;i&gt;wait()&lt;/i&gt; and &lt;i&gt;notify()&lt;/i&gt; before simpler methods of synchronization.&lt;br /&gt;
&lt;br /&gt;
It happens that Java has quite a few helper classes to make synchronization much simpler. The &lt;b&gt;Semaphore&lt;/b&gt; is one of those classes. I have constructed a simpler version of the &lt;b&gt;Drop&lt;/b&gt; class using two &lt;b&gt;Semaphore&lt;/b&gt;s.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #EEEEEE;&quot;&gt;public class Drop {
    private static final int MAX_MESSAGE = 10;
    private String[] messages = new String[MAX_MESSAGE];
    private int next_available;
    private Semaphore taking = new Semaphore(0, true);
    private Semaphore putting = 
        new Semaphore(MAX_MESSAGE, true);
    
    public void put(String message) {
        try {
            putting.acquire();
        } catch (InterruptedException e) { }
        synchronized (messages) {
            messages[next_available] = message;
            next_available++;
        }
        taking.release();                                        
    }

    public String take() {
        String message;
        try {
            taking.acquire();
        } catch (InterruptedException e) { }
        synchronized (this) {
            next_available--;
            message = messages[next_available];
        }
        putting.release();
        return message;
    }
}
&lt;/pre&gt;&lt;br /&gt;
There are three conceptual pieces to that make this new &lt;b&gt;Drop&lt;/b&gt; work.&amp;nbsp;The first and second are two&amp;nbsp;&lt;b&gt;Semaphore&lt;/b&gt;s, &lt;i&gt;putting&lt;/i&gt;&amp;nbsp;and &lt;i&gt;taking&lt;/i&gt;. &amp;nbsp;The third piece is the&amp;nbsp;&lt;i&gt;synchronized&lt;/i&gt;&amp;nbsp;code block&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;The&amp;nbsp;&lt;i&gt;putting&lt;/i&gt;&amp;nbsp;&lt;b&gt;Semaphore&lt;/b&gt;&amp;nbsp;protects against concurrent additions, and overflow.&lt;/li&gt;
&lt;li&gt;The&amp;nbsp;&lt;i&gt;taking&lt;/i&gt;&amp;nbsp;&lt;b&gt;Semaphore&lt;/b&gt;&amp;nbsp;protects against concurrent removals, and underflow.&lt;/li&gt;
&lt;li&gt;The &lt;i&gt;synchronized&lt;/i&gt;&amp;nbsp;code block&amp;nbsp;it utilizes the intrinsic lock on &lt;b&gt;Drop&lt;/b&gt;&amp;nbsp;creating an atomic section. Thereby protecting against concurrent manipulations of the &lt;i&gt;messages&lt;/i&gt;&amp;nbsp;array and &lt;i&gt;next_available&lt;/i&gt;&amp;nbsp;index into it.&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
These three pieces are much easier to understand than a broadcast system.&amp;nbsp;Although we understand (or guess) that the underlying &lt;b&gt;Semphore&lt;/b&gt;&amp;nbsp;is implemented with the&amp;nbsp;broadcasting&amp;nbsp;&lt;i&gt;wait()&lt;/i&gt;&amp;nbsp;and &lt;i&gt;notify()&lt;/i&gt;&amp;nbsp;system.&lt;br /&gt;
&lt;br /&gt;
Utilizing the provided utilities the example has become far simpler. There&#39;s no need to check the state of the &lt;b&gt;Drop&lt;/b&gt;&amp;nbsp;when the thread has resumed. There are no concerns of cascading execution of tasks.&lt;br /&gt;
&lt;br /&gt;
Synchronization in Java &lt;b&gt;could&lt;/b&gt;&amp;nbsp;be presented as a simple use of the available utilities.</content><link rel='replies' type='application/atom+xml' href='http://amazingblather.blogspot.com/feeds/4869197293184084991/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingblather.blogspot.com/2010/10/threads-in-java-can-be-simpler.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/4869197293184084991'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/4869197293184084991'/><link rel='alternate' type='text/html' href='http://amazingblather.blogspot.com/2010/10/threads-in-java-can-be-simpler.html' title='Threads in Java can be simpler'/><author><name>Corey</name><uri>http://www.blogger.com/profile/02228538267213653947</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='14' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQBPkpS5fGUzddc1-VnM148vyHgYVz-RUrtlJsmP85shD3EFGIrNO2fjWz_oj_gnIkCctYuiCZ0lPMvxpU-zC-xMDg6ddysToKE0SPeb9RGIs2BSi8Z-vqXWcJFNfAxFs/s220/cimg0084-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5274993052759678254.post-5267409767507362024</id><published>2010-09-27T22:40:00.001-04:00</published><updated>2010-09-27T22:44:43.045-04:00</updated><title type='text'>Learning about bounce</title><content type='html'>There&#39;s little opportunity to handle hardware interrupts in my day to day. Thankfully &lt;a href=&quot;http://www.emich.edu/compsci/courses_GR_2004.html#625&quot;&gt;COSC 625&lt;/a&gt; and Arduino present the opportunity for hardware interrupts and I to get acquainted.&lt;br /&gt;
&lt;br /&gt;
Arduino has a nice&amp;nbsp;&lt;a href=&quot;http://arduino.cc/en/Reference/HomePage&quot;&gt;online reference&lt;/a&gt;. The documentation for&amp;nbsp;&lt;a href=&quot;http://arduino.cc/en/Reference/AttachInterrupt&quot;&gt;attachInterrupt&lt;/a&gt;&amp;nbsp;includes a short example for creating a circuit that toggles an LED on or off.&lt;br /&gt;
&lt;br /&gt;
There are two interrupts on the Duemilanove, pin 2 and 3. Pin 2 is interrupt 0, pin 3 is interrupt 1. The example uses pin 2. When pin 2 transitions from high to low, or low to high the interrupt service routine is called. The example has the LED turn on when the button is pressed, and off when released.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee;&quot;&gt;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/*&amp;nbsp;The&amp;nbsp;pin&amp;nbsp;the&amp;nbsp;LED&amp;nbsp;is&amp;nbsp;attached&amp;nbsp;to.&amp;nbsp;*/&lt;/span&gt;
#define&amp;nbsp;LED_PIN&amp;nbsp;13

&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/*&amp;nbsp;The&amp;nbsp;initial&amp;nbsp;state&amp;nbsp;of&amp;nbsp;the&amp;nbsp;LED&amp;nbsp;*/&lt;/span&gt;
volatile&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; state = &lt;span style=&quot;color: #006699;&quot;&gt;LOW&lt;/span&gt;;

&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;&lt;b&gt;setup&lt;/b&gt;&lt;/span&gt;()
{
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;pinMode&lt;/span&gt;(LED_PIN, &lt;span style=&quot;color: #006699;&quot;&gt;OUTPUT&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;attachInterrupt&lt;/span&gt;(0, &lt;span style=&quot;color: #cc6600;&quot;&gt;blink&lt;/span&gt;, &lt;span style=&quot;color: #006699;&quot;&gt;CHANGE&lt;/span&gt;);
}

&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;&lt;b&gt;loop&lt;/b&gt;&lt;/span&gt;()
{
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;digitalWrite&lt;/span&gt;(LED_PIN, state);
}

&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;blink&lt;/span&gt;()
{
&amp;nbsp;&amp;nbsp;state&amp;nbsp;=&amp;nbsp;!state;
}

&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;But that&#39;s not what happened!&lt;/b&gt; After pressing the button several times the LED would be on when the button was released.&lt;br /&gt;
&lt;br /&gt;
&lt;object height=&quot;306&quot; width=&quot;500&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/YV8_rGb0kOY?fs=1&amp;amp;hl=en_US&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/YV8_rGb0kOY?fs=1&amp;amp;hl=en_US&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;500&quot; height=&quot;306&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;
Insert video here&lt;br /&gt;
&lt;br /&gt;
After double checking the wiring, and the project source several times I went looking for other interrupt examples. When I stumbled across my problem, which is&amp;nbsp;the well known problem of &lt;a href=&quot;http://www.elexp.com/t_bounc.htm&quot;&gt;bouncing&lt;/a&gt;&lt;i&gt;. &lt;/i&gt;It turns out that physical switches give dirty signals. A switch will turn on and off very quickly many times for even a single action. Something an electrical engineer is well aware of, something I had no idea about.&lt;br /&gt;
&lt;br /&gt;
So well known that Arduino even has a debouncing&amp;nbsp;&lt;a href=&quot;http://www.arduino.cc/playground/Code/Bounce&quot;&gt;library&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
I had some difficulties with getting the library to work; it&#39;s more my fault than anything else. Instead the interrupt service routine debounces.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee;&quot;&gt;#define&amp;nbsp;BOUNCE_MIN&amp;nbsp;250

&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #cc6600;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; last;
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; now;

&amp;nbsp;&amp;nbsp;now&amp;nbsp;=&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;millis&lt;/span&gt;();
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;if&lt;/span&gt; ((now - last) &amp;lt;= BOUNCE_MIN) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* do nothing */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;return&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;}

&amp;nbsp;&amp;nbsp;last&amp;nbsp;=&amp;nbsp;now;
&amp;nbsp;&amp;nbsp;state&amp;nbsp;=&amp;nbsp;!state;
}
&lt;/pre&gt;&lt;br /&gt;
Which limits the frequency of state changes to 250ms. &lt;br /&gt;
&lt;br /&gt;
&lt;object height=&quot;306&quot; width=&quot;500&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/l4DrPZD6fbQ?fs=1&amp;amp;hl=en_US&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/l4DrPZD6fbQ?fs=1&amp;amp;hl=en_US&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;500&quot; height=&quot;306&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;
&lt;br /&gt;
It works!</content><link rel='replies' type='application/atom+xml' href='http://amazingblather.blogspot.com/feeds/5267409767507362024/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingblather.blogspot.com/2010/09/learning-about-bounce.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/5267409767507362024'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/5267409767507362024'/><link rel='alternate' type='text/html' href='http://amazingblather.blogspot.com/2010/09/learning-about-bounce.html' title='Learning about bounce'/><author><name>Corey</name><uri>http://www.blogger.com/profile/02228538267213653947</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='14' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQBPkpS5fGUzddc1-VnM148vyHgYVz-RUrtlJsmP85shD3EFGIrNO2fjWz_oj_gnIkCctYuiCZ0lPMvxpU-zC-xMDg6ddysToKE0SPeb9RGIs2BSi8Z-vqXWcJFNfAxFs/s220/cimg0084-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5274993052759678254.post-7669283811783796323</id><published>2010-09-19T08:19:00.001-04:00</published><updated>2010-09-19T10:17:02.493-04:00</updated><title type='text'>My first integrated circuit</title><content type='html'>It&#39;s really hard to put down a toy. The arduino is no exception, and the projects from &lt;a href=&quot;http://www.oomlout.com/&quot;&gt;oomlout&lt;/a&gt; are so easy to do. Put those two together and you&#39;ve got the start of an addiction.&lt;br /&gt;
&lt;br /&gt;
The &lt;a href=&quot;http://www.oomlout.com/a/products/ardx/circ-05&quot;&gt;CIRC05&lt;/a&gt; project is the first to use an integrated circuit. It works by throwing a latch to tell the integrated circuit to receive data, then sending the data, finally releasing the latch at which point the data is displayed. The data being sent is an 8 bit number, each bit of the number corresponds to a pin. When a bit is set the pin is high (the LED is on), when a bit is clear the pin is low (the LED is off.)&lt;br /&gt;
&lt;br /&gt;
I&#39;m still learning about basic electricity and electronics, so I followed the wiring &lt;a href=&quot;http://ardx.org/BBLS05&quot;&gt;layout&lt;/a&gt;&amp;nbsp;(pdf link) without any variation. I did want to continue playing around with the layout. To do that the source needed some modifications, allowing subsequent projects with the same layout to require minimal work.&lt;br /&gt;
&lt;br /&gt;
First the original in action:&lt;br /&gt;
&lt;object height=&quot;306&quot; width=&quot;500&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/vE1f5uLV5-0?fs=1&amp;amp;hl=en_US&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/vE1f5uLV5-0?fs=1&amp;amp;hl=en_US&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;500&quot; height=&quot;306&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;
&lt;br /&gt;
This is the modified version of the oomlout source, subsequent projects will provide unique &lt;i&gt;get_pin_value()&lt;/i&gt; and &lt;i&gt;get_delay_ms()&lt;/i&gt; functions. By modifying these two functions the LED behavior is easily varied.&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee;&quot;&gt;#define&amp;nbsp;PIN_DATA&amp;nbsp;2
#define&amp;nbsp;PIN_CLOCK&amp;nbsp;3
#define&amp;nbsp;PIN_LATCH&amp;nbsp;4

#define&amp;nbsp;DELAY_MS&amp;nbsp;150

&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;&lt;b&gt;setup&lt;/b&gt;&lt;/span&gt;() {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;pinMode&lt;/span&gt;(PIN_DATA, &lt;span style=&quot;color: #006699;&quot;&gt;OUTPUT&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;pinMode&lt;/span&gt;(PIN_CLOCK, &lt;span style=&quot;color: #006699;&quot;&gt;OUTPUT&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;pinMode&lt;/span&gt;(PIN_LATCH, &lt;span style=&quot;color: #006699;&quot;&gt;OUTPUT&lt;/span&gt;);
}

&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;&lt;b&gt;loop&lt;/b&gt;&lt;/span&gt;() {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; pin_value = 0;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; delay_ms;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;pin_value&amp;nbsp;=&amp;nbsp;get_pin_value(pin_value);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;delay_ms&amp;nbsp;=&amp;nbsp;get_delay_ms(pin_value);

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;updateLEDs(pin_value);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;delay&lt;/span&gt;(delay_ms);
}

&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; updateLEDs(&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; value) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Pull the chip latch low */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;digitalWrite&lt;/span&gt;(PIN_LATCH, &lt;span style=&quot;color: #006699;&quot;&gt;LOW&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Puts the bits into the shift register */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;shiftOut&lt;/span&gt;(PIN_DATA, PIN_CLOCK, &lt;span style=&quot;color: #006699;&quot;&gt;MSBFIRST&lt;/span&gt;, value);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Pulls the latch high, displaying the data */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;digitalWrite&lt;/span&gt;(PIN_LATCH, &lt;span style=&quot;color: #006699;&quot;&gt;HIGH&lt;/span&gt;);
}

&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; get_pin_value(&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;if&lt;/span&gt; (i == 255) {
 &lt;span style=&quot;color: #cc6600;&quot;&gt;return&lt;/span&gt; (0);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;return&lt;/span&gt; (++i);
}

&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; get_delay_ms(&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;return&lt;/span&gt; (DELAY_MS);
}
&lt;/pre&gt;&lt;br /&gt;
The first modification was to make the LEDs light up sequentially.&lt;br /&gt;
&lt;br /&gt;
&lt;object height=&quot;306&quot; width=&quot;500&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/UIXG9joVI9w?fs=1&amp;amp;hl=en_US&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/UIXG9joVI9w?fs=1&amp;amp;hl=en_US&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;500&quot; height=&quot;306&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;
&lt;br /&gt;
The &lt;i&gt;get_pin_value()&lt;/i&gt;&amp;nbsp;and &lt;i&gt;get_delay_ms()&lt;/i&gt;&amp;nbsp;functions follow&lt;br /&gt;
&lt;pre style=&quot;background: #EEEEEE;&quot;&gt;&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; get_pin_value(&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i) {
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;if&lt;/span&gt; (i == 0 || i &amp;gt; 128) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;return&lt;/span&gt; 1;
&amp;nbsp;&amp;nbsp;}

&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;return&lt;/span&gt; (i*2);
}

&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; get_delay_ms(&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;return&lt;/span&gt; (150);
}
&lt;/pre&gt;&lt;br /&gt;
The second modification was to light the LEDs sequentially keeping the previously lit LEDs on.&lt;br /&gt;
&lt;br /&gt;
&lt;object height=&quot;306&quot; width=&quot;500&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/SNEtf2lpn6o?fs=1&amp;amp;hl=en_US&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/SNEtf2lpn6o?fs=1&amp;amp;hl=en_US&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;500&quot; height=&quot;306&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;
&lt;br /&gt;
The &lt;i&gt;get_pin_value()&lt;/i&gt;&amp;nbsp;function is only marginally more complex, &lt;i&gt;get_delay_ms()&lt;/i&gt;&amp;nbsp;is unchanged&lt;br /&gt;
&lt;pre style=&quot;background: #EEEEEE;&quot;&gt;&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; get_pin_value(&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;if&lt;/span&gt; (i == 0 || i &amp;gt; 255) {
        &lt;span style=&quot;color: #cc6600;&quot;&gt;return&lt;/span&gt; 1;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;return&lt;/span&gt; (i * 2 + 1);
}

&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; get_delay_ms(&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;return&lt;/span&gt; (DELAY_MS);
}
&lt;/pre&gt;&lt;br /&gt;
The third modification was to light LEDs randomly, and delay randomly between displaying the values. This is my favorite.&lt;br /&gt;
&lt;br /&gt;
&lt;object height=&quot;306&quot; width=&quot;500&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/7ah5ELAzr8w?fs=1&amp;amp;hl=en_US&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/7ah5ELAzr8w?fs=1&amp;amp;hl=en_US&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;500&quot; height=&quot;306&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;
&lt;br /&gt;
The &lt;i&gt;get_pin_value()&lt;/i&gt;&amp;nbsp;and &lt;i&gt;get_delay_ms()&lt;/i&gt;&amp;nbsp;functions are simplistic.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #EEEEEE;&quot;&gt;&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; get_pin_value(&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* In setup randomSeed(analogRead(0)); */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;random&lt;/span&gt;(0, 256);
}

&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; get_delay_ms(&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;random&lt;/span&gt;(50, 200);
}
&lt;/pre&gt;&lt;br /&gt;
I am having a hard time putting this toy away, luckily I&#39;m getting tired. Maybe I&#39;ll play with inputs tomorrow...</content><link rel='replies' type='application/atom+xml' href='http://amazingblather.blogspot.com/feeds/7669283811783796323/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingblather.blogspot.com/2010/09/my-first-integrated-circuit.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/7669283811783796323'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/7669283811783796323'/><link rel='alternate' type='text/html' href='http://amazingblather.blogspot.com/2010/09/my-first-integrated-circuit.html' title='My first integrated circuit'/><author><name>Corey</name><uri>http://www.blogger.com/profile/02228538267213653947</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='14' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQBPkpS5fGUzddc1-VnM148vyHgYVz-RUrtlJsmP85shD3EFGIrNO2fjWz_oj_gnIkCctYuiCZ0lPMvxpU-zC-xMDg6ddysToKE0SPeb9RGIs2BSi8Z-vqXWcJFNfAxFs/s220/cimg0084-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5274993052759678254.post-2465367350458216081</id><published>2010-09-18T18:18:00.002-04:00</published><updated>2010-09-18T18:32:47.332-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Arduino"/><title type='text'>8 LED Arduino</title><content type='html'>With a little free time this weekend, I invested it in my Arduino. I selected a project from &lt;a href=&quot;http://ardx.org/&quot;&gt;ardx.org&lt;/a&gt;,&amp;nbsp;&lt;a href=&quot;http://ardx.org/CIRC02&quot;&gt;CIRC02&lt;/a&gt;. The board &lt;a href=&quot;http://ardx.org/BBLS02&quot;&gt;layout&lt;/a&gt;&amp;nbsp;(pdf link) involves 8 LEDs controlled by 8 output pins 2 through 9.&lt;br /&gt;
&lt;br /&gt;
The initial project blinks the LEDs in ascending order, and then repeats. Wiring the board took longer than building the project&lt;br /&gt;
&lt;br /&gt;
&lt;object height=&quot;424&quot; width=&quot;343&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/5uHlEB_fVvE?fs=1&amp;amp;hl=en_US&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/5uHlEB_fVvE?fs=1&amp;amp;hl=en_US&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;424&quot; height=&quot;343&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;The original project source&lt;/i&gt;&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9};

&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;&lt;b&gt;setup&lt;/b&gt;&lt;/span&gt;() {
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; 8; i++) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;pinMode&lt;/span&gt;(ledPins[i], &lt;span style=&quot;color: #006699;&quot;&gt;OUTPUT&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;}
}

&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;&lt;b&gt;loop&lt;/b&gt;&lt;/span&gt;() {
&amp;nbsp;&amp;nbsp;oneAfterAnotherNoLoop();
}

&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; oneAfterAnotherNoLoop() {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; delayTime = 75;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; 8; i++) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;digitalWrite&lt;/span&gt;(ledPins[i], &lt;span style=&quot;color: #006699;&quot;&gt;HIGH&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;delay&lt;/span&gt;(delayTime);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;digitalWrite&lt;/span&gt;(ledPins[i], &lt;span style=&quot;color: #006699;&quot;&gt;LOW&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;delay&lt;/span&gt;(delayTime);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}

&lt;/pre&gt;With the board wired up, the blinking LEDs didn&#39;t satisfy me. I created a few other projects that used the same wiring. Adding few utility functions, and an amplification array. With this basic set up, each project need only to implement an in_order() function&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/**&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*&amp;nbsp;Finds&amp;nbsp;the&amp;nbsp;number&amp;nbsp;of&amp;nbsp;elements&amp;nbsp;in&amp;nbsp;an&amp;nbsp;array.&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*/&lt;/span&gt;
#define&amp;nbsp;NELE(array)&amp;nbsp;(sizeof(array)&amp;nbsp;/&amp;nbsp;sizeof(array[0]))

&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/**&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*&amp;nbsp;The&amp;nbsp;amount&amp;nbsp;to&amp;nbsp;decay&amp;nbsp;each&amp;nbsp;pin&amp;nbsp;by&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*/&lt;/span&gt;
#define&amp;nbsp;DECAY_AMOUNT&amp;nbsp;75

&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/**&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*&amp;nbsp;The&amp;nbsp;number&amp;nbsp;of&amp;nbsp;miliseconds&amp;nbsp;to&amp;nbsp;delay&amp;nbsp;by&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*/&lt;/span&gt;
#define&amp;nbsp;DELAY_MS&amp;nbsp;80

&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; ledPins[] = {2,&amp;nbsp;3,&amp;nbsp;4,&amp;nbsp;5,&amp;nbsp;6,&amp;nbsp;7,&amp;nbsp;8,&amp;nbsp;9};
&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; ledAmp[NELE(ledPins)];

&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/**&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*&amp;nbsp;setup&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*&amp;nbsp;Invoked&amp;nbsp;by&amp;nbsp;the&amp;nbsp;arduino&amp;nbsp;infrastructure&amp;nbsp;to&amp;nbsp;initialize&amp;nbsp;state.&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*/&lt;/span&gt;
&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;&lt;b&gt;setup&lt;/b&gt;&lt;/span&gt;() {
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; NELE(ledPins); i++) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;pinMode&lt;/span&gt;(ledPins[i], &lt;span style=&quot;color: #006699;&quot;&gt;OUTPUT&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ledAmp[i]&amp;nbsp;=&amp;nbsp;0;
&amp;nbsp;&amp;nbsp;}
}

&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/**&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*&amp;nbsp;Neverending&amp;nbsp;loop&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*/&lt;/span&gt;
&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #cc6600;&quot;&gt;&lt;b&gt;loop&lt;/b&gt;&lt;/span&gt;() {
&amp;nbsp;&amp;nbsp;in_order();
}

&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/**&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*&amp;nbsp;Decays&amp;nbsp;all&amp;nbsp;the&amp;nbsp;pin&amp;nbsp;outs&amp;nbsp;that&amp;nbsp;are&amp;nbsp;in&amp;nbsp;use&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*&amp;nbsp;@param[in]&amp;nbsp;amount&amp;nbsp;the&amp;nbsp;amount&amp;nbsp;to&amp;nbsp;decay&amp;nbsp;the&amp;nbsp;pins&amp;nbsp;by&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*/&lt;/span&gt;
&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; decay_pins(&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; amount) {
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; NELE(ledPins); i++) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ledAmp[i]&amp;nbsp;-=&amp;nbsp;amount;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;if&lt;/span&gt; (ledAmp[i] &amp;lt; 0) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ledAmp[i]&amp;nbsp;=&amp;nbsp;0;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;}
}

&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/**&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*&amp;nbsp;Writes&amp;nbsp;all&amp;nbsp;the&amp;nbsp;new&amp;nbsp;pin&amp;nbsp;values&lt;/span&gt;
&lt;span style=&quot;color: #7e7e7e;&quot;&gt;&amp;nbsp;*/&lt;/span&gt;
&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; write_pins() {
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; NELE(ledPins); i++) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;analogWrite&lt;/span&gt;(ledPins[i], ledAmp[i]);
&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;The high-top fade,&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: normal;&quot;&gt;&amp;nbsp;the LEDs are faded left to right&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;object height=&quot;385&quot; width=&quot;640&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/Xf5BlfQDi8s&amp;color1=0xb1b1b1&amp;color2=0xd0d0d0&amp;hl=en_US&amp;feature=player_embedded&amp;fs=1&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowScriptAccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/Xf5BlfQDi8s&amp;color1=0xb1b1b1&amp;color2=0xd0d0d0&amp;hl=en_US&amp;feature=player_embedded&amp;fs=1&quot; type=&quot;application/x-shockwave-flash&quot; allowfullscreen=&quot;true&quot; allowScriptAccess=&quot;always&quot; width=&quot;425&quot; height=&quot;343&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Source to in_order&lt;/i&gt;&lt;br /&gt;
&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; in_order() {
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; NELE(ledPins); i++) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ledAmp[i]&amp;nbsp;=&amp;nbsp;255;&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Set the pin high */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;write_pins();&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Write all pin values */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;delay&lt;/span&gt;(DELAY_MS);

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;decay_pins(DECAY_AMOUNT);&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Decay all pins */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;write_pins();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;delay&lt;/span&gt;(DELAY_MS);
&amp;nbsp;&amp;nbsp;}
}
&lt;/pre&gt;&lt;i&gt;&lt;br /&gt;
&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;The Battle Star Galactica&lt;/i&gt;, where the LEDs are&amp;nbsp;fade left to right, then right to left.&lt;br /&gt;
&lt;br /&gt;
&lt;object height=&quot;385&quot; width=&quot;640&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/04mArhvuXuE?fs=1&amp;amp;hl=en_US&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/04mArhvuXuE?fs=1&amp;amp;hl=en_US&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;424&quot; height=&quot;343&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;
&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Source to in_order&lt;/i&gt;&lt;br /&gt;
&lt;pre style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; in_order() {
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; NELE(ledPins) - 1; i++) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ledAmp[i&amp;nbsp;+&amp;nbsp;1]&amp;nbsp;=&amp;nbsp;255;&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Set the pin high */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ledAmp[i]&amp;nbsp;=&amp;nbsp;255;&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Set the pin high */&lt;/span&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;write_pins();&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Write all pin values */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;delay&lt;/span&gt;(DELAY_MS);

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;decay_pins(DECAY_AMOUNT);&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Decay all pins */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;write_pins();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;delay&lt;/span&gt;(DELAY_MS);
&amp;nbsp;&amp;nbsp;}

&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i = NELE(ledPins); i &amp;gt; 0 ; i--) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ledAmp[i]&amp;nbsp;=&amp;nbsp;255;&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Set the pin high */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ledAmp[i&amp;nbsp;-&amp;nbsp;1]&amp;nbsp;=&amp;nbsp;255;&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Set the pin high */&lt;/span&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;write_pins();&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Write all pin values */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;delay&lt;/span&gt;(DELAY_MS);

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;decay_pins(DECAY_AMOUNT);&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Decay all pins */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;write_pins();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;delay&lt;/span&gt;(DELAY_MS);
&amp;nbsp;&amp;nbsp;}
}
&lt;/pre&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;i&gt;The explosion,&lt;/i&gt;&amp;nbsp;the center LED is lit up then fades away towards the edges&lt;br /&gt;
&lt;object height=&quot;385&quot; width=&quot;640&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/f7u6xdZPTfE?fs=1&amp;amp;hl=en_US&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/f7u6xdZPTfE?fs=1&amp;amp;hl=en_US&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;424&quot; height=&quot;343&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;
&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Source to in_order&lt;/i&gt;&lt;br /&gt;
&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;color: #cc6600;&quot;&gt;void&lt;/span&gt; in_order() {
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; center = NELE(ledPins) / 2;
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #cc6600;&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; NELE(ledPins) / 2; i++) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ledAmp[center&amp;nbsp;-&amp;nbsp;i]&amp;nbsp;=&amp;nbsp;255;&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Set the pin high */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ledAmp[center&amp;nbsp;+&amp;nbsp;i]&amp;nbsp;=&amp;nbsp;255;&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Set the pin high */&lt;/span&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;write_pins();&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Write all pin values */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;delay&lt;/span&gt;(DELAY_MS);

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;decay_pins(DECAY_AMOUNT);&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* Decay all pins */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;write_pins();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;delay&lt;/span&gt;(DELAY_MS);
&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #cc6600;&quot;&gt;if&lt;/span&gt; (NELE(ledPins) % 2 == 0) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #7e7e7e;&quot;&gt;/* NELE(ledPins) is even */&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ledAmp[0]&amp;nbsp;=&amp;nbsp;255;
&amp;nbsp;&amp;nbsp;}
}

&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingblather.blogspot.com/feeds/2465367350458216081/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingblather.blogspot.com/2010/09/8-led-arduino.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/2465367350458216081'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/2465367350458216081'/><link rel='alternate' type='text/html' href='http://amazingblather.blogspot.com/2010/09/8-led-arduino.html' title='8 LED Arduino'/><author><name>Corey</name><uri>http://www.blogger.com/profile/02228538267213653947</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='14' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQBPkpS5fGUzddc1-VnM148vyHgYVz-RUrtlJsmP85shD3EFGIrNO2fjWz_oj_gnIkCctYuiCZ0lPMvxpU-zC-xMDg6ddysToKE0SPeb9RGIs2BSi8Z-vqXWcJFNfAxFs/s220/cimg0084-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5274993052759678254.post-1614891884518165868</id><published>2010-09-13T22:00:00.002-04:00</published><updated>2010-09-13T22:18:28.607-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Threads"/><title type='text'>Threads in Java are not simple</title><content type='html'>To prepare for an upcoming project in real-time processing I needed an education in Java threads. I wanted to build a simple producer and consumer project with each producer and consumer given their own thread. There would be multiple producers and consumers, and possibly multiple items in the work queue.&lt;br /&gt;
&lt;br /&gt;
This was supposed to be easy, as Java was designed with threading in mind. This is my experience, followed by an explanation of the costly scheduling that the sanctioned examples produces. The length of this post is a testament to the simplicity of implementing synchronized threads in Java.&lt;br /&gt;
&lt;br /&gt;
Working my way through the Java tutorials I found this&amp;nbsp;&lt;a href=&quot;http://download.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html&quot;&gt;example&lt;/a&gt;. The heart of the example is the Drop class, it is the resource that will be accessed by multiple threads. Through an instance of the Drop class, the Producers will convey messages to the Consumers.&lt;br /&gt;
&lt;br /&gt;
The Drop class with the commentary removed:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;Drop.java
    public class Drop {
        private String message;
        private boolean empty = true;

        public synchronized String take() {
            while (empty) {
                try {
                    wait();
                } catch (InterruptedException e) {}
            }
            empty = true;
            notifyAll();
            return message;
        }

        public synchronized void put(String message) {
            while (!empty) {
                try { 
                    wait();
                } catch (InterruptedException e) {}
            }
            empty = false;
            this.message = message;
            notifyAll();
        }
    }
&lt;/pre&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;The purpose of the Drop class is to prevent concurrent access to the message member. This is achieved by using the Object&#39;s &lt;i&gt;intrinsic lock&lt;/i&gt;&amp;nbsp;(also called the&amp;nbsp;&lt;i&gt;monitor lock&lt;/i&gt;, or the &lt;i&gt;monitor&lt;/i&gt;).&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;A thread obtains the lock by calling and entering a &lt;i&gt;synchronized&lt;/i&gt; method. Any other thread that calls a&amp;nbsp;&lt;i&gt;synchronized&lt;/i&gt; method will be blocked until the lock is released. In this example the lock can be released in two ways; the&amp;nbsp;&lt;i&gt;synchronized&lt;/i&gt;&amp;nbsp;call returns, or the thread &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;wait()&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&#39;s.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;To understand the process, we need to understand &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;wait()&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt; and &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;notifyAll()&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;. When a thread &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;wait()&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&#39;s it blocks, awaiting an &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;InterruptionException&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;notifyAll()&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt; delivers such an&amp;nbsp;&lt;/span&gt;exception.&lt;br /&gt;
&lt;br /&gt;
It&#39;s important to understand that each thread will block on two levels, when in the context of a &lt;i&gt;synchronized&lt;/i&gt; method without the &lt;i&gt;intrinsic lock&lt;/i&gt;, and while &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;wait()&lt;/span&gt;&#39;ing.&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;In the Drop class when modifying or retrieving the message, the procedure is:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Check if the message is empty&lt;/li&gt;
&lt;li&gt;If the message is incorrectly empty then &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;wait()&lt;/span&gt;&lt;/li&gt;

&lt;ul&gt;&lt;li&gt;When notified go to 1.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Modify or retrieve message&lt;/li&gt;
&lt;li&gt;Modify empty&lt;/li&gt;
&lt;li&gt;Notify all threads&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
My Producer and Consumer classes are different from those in the example. They run forever and identify the Producer and Consumer by their thread name. Producers infinitely produce messages, and Consumers read those messages forever spitting them to System.out.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;Producer.java
    import java.util.Random;

    public class Producer implements Runnable {
        private Drop drop;

        public Producer(Drop drop) {
            this.drop = drop;
        }

        public void run() {
            Random random = new Random();
            String pre = Thread.currentThread().getName()
                       + &quot; widget #: &quot;;
            int i = 1;

            while (true) {
                drop.put(pre + &quot; &quot; + i++);
                try {
                    Thread.sleep(random.nextInt(5000));
                } catch (InterruptedException e) { }
            }
        }
    }
&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;Consumer.java
    import java.util.Random;

    public class Consumer implements Runnable {
        private Drop drop;

        public Consumer(Drop drop) {
            this.drop = drop;
        }

        public void run() {
            Random random = new Random();
            while (true) {
                message = drop.take();
                System.out.println(
                    Thread.currentThread().getName() + 
                    &quot; MESSAGE RECEIVED: &quot; + message);
                try {
                    Thread.sleep(random.nextInt(5000));
                } catch (InterruptedException e) {}
            }
        }
    }
&lt;/pre&gt;&lt;br /&gt;
Lastly a driver is needed to start the producers and consumers. To ease the scheduling explanation there will be one Producer and four Consumers.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;ProducerConsumerExample.java
    public class ProducerConsumerExample {
        public static void main(String[] args) {
            Drop drop = new Drop();
            (new Thread(new Producer(drop))).start();
            (new Thread(new Consumer(drop))).start();
            (new Thread(new Consumer(drop))).start();
            (new Thread(new Consumer(drop))).start();
            (new Thread(new Consumer(drop))).start();
        }
    }
&lt;/pre&gt;&lt;br /&gt;
That&#39;s a lot of code, and a long introduction. Now we&#39;re ready to look at how threads are scheduled. To increase the simplicity of this situation I&#39;m going to ignore the Producer for the moment.&lt;br /&gt;
&lt;br /&gt;
I&#39;m going to name the Consumer threads A, through D. When the program starts, it&#39;s non-deterministic which Consumer will start first, second, or third. However, I&#39;m going to order those threads by their start time, and then name them. This means A starts first, B second, C third, and D last.&lt;br /&gt;
&lt;br /&gt;
Assuming there is no message in the Drop instance (it&#39;s empty) the following happens. A, B, C, and D all call drop.take(). Since A started first, A is rewarded with the lock. B, C, and D are now all blocked on the intrinsic lock of the Drop object.&lt;br /&gt;
&lt;br /&gt;
From A&#39;s perspective this happens&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;drop.take()&lt;/span&gt; determines the message is empty&lt;/li&gt;
&lt;li&gt;A &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;wait()&lt;/span&gt;s; which releases the lock&lt;/li&gt;
&lt;/ol&gt;Now the lock is released. We&#39;ll assume the thread scheduling is fair (it doesn&#39;t have to be) and B obtains the lock. Which then...&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;drop.take()&lt;/span&gt; determines the message is empty&lt;/li&gt;
&lt;li&gt;B &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;wait()&lt;/span&gt;s; which releases the lock&lt;/li&gt;
&lt;/ol&gt;And so on, at the end of this process A, B, C, and D have all relinquished the lock on the Drop instance. But they&#39;re all waiting to be notified.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Here is where it gets ugly.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;br /&gt;
The Producer thread wakes up deciding it&#39;s time to put a message in the Drop object. It calls &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;drop.put()&lt;/span&gt;. After storing the message and setting empty to false, &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;drop.put()&lt;/span&gt; invokes &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;notifyAll()&lt;/span&gt;.&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;
&lt;/i&gt;&lt;br /&gt;
&lt;b&gt;What happens next?&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&lt;/i&gt;The naive answer is &lt;i&gt;A Consumer thread consumes the message.&lt;/i&gt;&amp;nbsp;This answer did not satisfy me. &lt;i&gt;Does the consumer thread execute in the context of &lt;/i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;i&gt;notifyAll()&lt;/i&gt;&lt;/span&gt;&lt;i&gt;?&lt;/i&gt;&amp;nbsp;&lt;i&gt;Doesn&#39;t Exception handling trump all other contexts?&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
To answer these question you have to think about the intrinsic lock, as well as the blocking from wait.&lt;br /&gt;
&lt;br /&gt;
Remember that &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;notifyAll()&lt;/span&gt; has been called within the context of &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;drop.put()&lt;/span&gt;, which means that an &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Exception&lt;/span&gt; has been generated for all threads waiting on the &lt;i&gt;intrinsic lock&lt;/i&gt;. The Producer currently owns the lock (the method is synchronized), no other thread can begin executing until the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;drop.put()&lt;/span&gt; call returns. The call to &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;notifyAll()&lt;/span&gt; does not relinquish the lock. (This portion of the answer is &amp;nbsp;in the &amp;nbsp;&lt;a href=&quot;http://download.oracle.com/javase/7/docs/api/java/lang/Object.html#notifyAll()&quot;&gt;documentation&lt;/a&gt;&amp;nbsp;just remember that &lt;i&gt;intrinsic lock&lt;/i&gt;&amp;nbsp;is the same as &lt;i&gt;monitor&lt;/i&gt;.)&lt;br /&gt;
&lt;br /&gt;
Each of the Consumer threads are sitting in wait(). Earlier I said this relinquished the lock, I lied. Really when a thread calls &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;wait()&lt;/span&gt; it&#39;s giving up the lock to obtain it later. &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;wait()&lt;/span&gt; is waiting on the lock.&lt;br /&gt;
&lt;br /&gt;
I find it&#39;s easier to think of the line of code that immediately follows the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;wait()&lt;/span&gt;. The next line of code the thread is going to execute is within the &lt;i&gt;synchronized&lt;/i&gt; method &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;drop.take().&lt;/span&gt;&amp;nbsp;The lock is required to execute in the context of the &lt;i&gt;synchronized method&lt;/i&gt;. It happens to be that the Exception handling is the next logical section of code. To be able to process the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Exception&lt;/span&gt; the thread must obtain the lock on the Drop object. This teaches me that Exceptions are not always handled immediately in Java.&lt;br /&gt;
&lt;br /&gt;
Therefor each thread is in competition for the lock. One of them will be given the lock (suppose it&#39;s C). The following occurs:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Enter &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;drop.take()&lt;/span&gt; context; acquiring the lock&lt;/li&gt;
&lt;li&gt;Handle the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Exception&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Find that there is a message&lt;/li&gt;
&lt;li&gt;Read the message&lt;/li&gt;
&lt;li&gt;Clear empty&lt;/li&gt;
&lt;li&gt;return from &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;drop.take()&lt;/span&gt;; relinquishing the lock.&lt;/li&gt;
&lt;/ol&gt;Now the lock on the Drop object is free. One thread is given the lock, say it&#39;s A.&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Enter &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;drop.take()&lt;/span&gt; context; acquiring the lock&lt;/li&gt;
&lt;li&gt;Handle the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Exception&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Find that there is no message&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;wait()&lt;/span&gt;; relinquishing the lock&lt;/li&gt;
&lt;/ol&gt;Now the lock on the Drop object is free. One thread is given the lock, say it&#39;s D.&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;ol&gt;&lt;li&gt;Enter &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;drop.take()&lt;/span&gt; context; acquiring the lock&lt;/li&gt;
&lt;li&gt;Handle the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Exception&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Find that there is no message&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;wait()&lt;/span&gt;; relinquishing the lock&lt;/li&gt;
&lt;/ol&gt;&lt;div&gt;What this looks like is a cascade of acquire and release of the lock, each thread is given the lock to handle the Exception then almost immediately &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;wait()&lt;/span&gt;&#39;s. Which will lead to another cascade.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Due to the property of &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;notifyAll()&lt;/span&gt; delivering an exception to all threads waiting on the lock of the object, there is overhead of checking the objects state after the handling of any &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Exception&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
In the situation outlined above adding second Producer makes the situation less clear. This second producer will awaken the first. After adding a message to the Drop object, the second Producer&#39;s &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;notifyAll()&lt;/span&gt; will be needlessly noticed by the first.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Understanding how a producer and consumer relationship through the Java tutorial example is anything but simple. Handling the lock is managed through two competing systems &lt;i&gt;synchronized methods&lt;/i&gt;&amp;nbsp;and&amp;nbsp;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;notifyAll()&lt;/span&gt;. With &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;notifyAll()&lt;/span&gt; complicating methods that &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;wait()&lt;/span&gt;.&amp;nbsp;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingblather.blogspot.com/feeds/1614891884518165868/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingblather.blogspot.com/2010/09/threads-in-java-are-not-simple.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/1614891884518165868'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/1614891884518165868'/><link rel='alternate' type='text/html' href='http://amazingblather.blogspot.com/2010/09/threads-in-java-are-not-simple.html' title='Threads in Java are not simple'/><author><name>Corey</name><uri>http://www.blogger.com/profile/02228538267213653947</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='14' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQBPkpS5fGUzddc1-VnM148vyHgYVz-RUrtlJsmP85shD3EFGIrNO2fjWz_oj_gnIkCctYuiCZ0lPMvxpU-zC-xMDg6ddysToKE0SPeb9RGIs2BSi8Z-vqXWcJFNfAxFs/s220/cimg0084-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5274993052759678254.post-8255410575820752962</id><published>2010-09-10T00:08:00.001-04:00</published><updated>2010-09-11T10:14:15.216-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Arduino"/><title type='text'>Blink ... Blink ... Blink ...</title><content type='html'>Setting up the Arduino SDK on my laptop was the hardest part of this project. Some of the cross compiling set up scripts on gentoo have a &lt;a href=&quot;http://bugs.gentoo.org/show_bug.cgi?id=286267&quot;&gt;couple&lt;/a&gt;&amp;nbsp;of&amp;nbsp;&lt;a href=&quot;http://bugs.gentoo.org/show_bug.cgi?id=147155&quot;&gt;issues&lt;/a&gt;. Which was just as fun as chasing down the need to use icedtea for the SDK instead of the JRE from Sun (now Oracle.)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;object height=&quot;344&quot; width=&quot;425&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/ArwTZEszMns?hl=en&amp;fs=1&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/ArwTZEszMns?hl=en&amp;fs=1&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;425&quot; height=&quot;344&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;
&lt;br /&gt;
The source for the project is extremely short.&lt;br /&gt;
&lt;blockquote&gt;&lt;pre&gt;int ledpin = 13;

void setup() {
    pinMode(ledPin, OUTPUT);
}

void loop() {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
}&lt;/pre&gt;&lt;/blockquote&gt;&amp;nbsp;I&#39;m really looking forward to more Arduino projects.</content><link rel='replies' type='application/atom+xml' href='http://amazingblather.blogspot.com/feeds/8255410575820752962/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingblather.blogspot.com/2010/09/blink-blink-blink.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/8255410575820752962'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/8255410575820752962'/><link rel='alternate' type='text/html' href='http://amazingblather.blogspot.com/2010/09/blink-blink-blink.html' title='Blink ... Blink ... Blink ...'/><author><name>Corey</name><uri>http://www.blogger.com/profile/02228538267213653947</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='14' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQBPkpS5fGUzddc1-VnM148vyHgYVz-RUrtlJsmP85shD3EFGIrNO2fjWz_oj_gnIkCctYuiCZ0lPMvxpU-zC-xMDg6ddysToKE0SPeb9RGIs2BSi8Z-vqXWcJFNfAxFs/s220/cimg0084-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5274993052759678254.post-8233884112982168692</id><published>2010-09-09T23:06:00.000-04:00</published><updated>2010-09-09T23:06:52.729-04:00</updated><title type='text'>Best intentions.</title><content type='html'>&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;This September is an important month in my life. My anticipated, and extremely delayed return to&amp;nbsp;&lt;a href=&quot;http://emich.edu/&quot;&gt;Eastern Michigan University&lt;/a&gt;&amp;nbsp;began this month. After a five year hiatus I intend to complete my masters in computer science.&lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;A lot has changed with the school, computer science, and me.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: 13px;&quot;&gt;There are new buildings and professors on campus. Java has taken over in academia. I&#39;m older and more focused, driven to complete this important educational task.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;Through this blog I&#39;ll record my challenges, opinions, and successes.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingblather.blogspot.com/feeds/8233884112982168692/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingblather.blogspot.com/2010/09/best-intentions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/8233884112982168692'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5274993052759678254/posts/default/8233884112982168692'/><link rel='alternate' type='text/html' href='http://amazingblather.blogspot.com/2010/09/best-intentions.html' title='Best intentions.'/><author><name>Corey</name><uri>http://www.blogger.com/profile/02228538267213653947</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='14' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQBPkpS5fGUzddc1-VnM148vyHgYVz-RUrtlJsmP85shD3EFGIrNO2fjWz_oj_gnIkCctYuiCZ0lPMvxpU-zC-xMDg6ddysToKE0SPeb9RGIs2BSi8Z-vqXWcJFNfAxFs/s220/cimg0084-1.jpg'/></author><thr:total>0</thr:total></entry></feed>