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

<channel>
	<title>ItOpen - Open Web Solutions, WebGis Development</title>
	<atom:link href="http://www.itopen.it/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.itopen.it</link>
	<description>[lang_en]Open Web Solutions: WebGis, Open Source development[/lang_en][lang_it]Soluzioni WebGIS e sviluppo software Open Source[lang_it]</description>
	<lastBuildDate>Wed, 16 May 2012 11:47:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Goodbye KMail!</title>
		<link>http://www.itopen.it/2012/05/16/goodbye-kmail/</link>
		<comments>http://www.itopen.it/2012/05/16/goodbye-kmail/#comments</comments>
		<pubDate>Wed, 16 May 2012 11:47:21 +0000</pubDate>
		<dc:creator>Alessandro Pasotti</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://www.itopen.it/?p=855</guid>
		<description><![CDATA[After more that ten years, today I finally said goodbye to KMail (and eventually to the whole KDE-PIM package). I started working on the italian translations of the KDE-PIM programs many years ago, that was my first contribute to the open source software (before I became a professional developer) and of course KDE and the [...]]]></description>
			<content:encoded><![CDATA[<p>After more that ten years, today I finally said goodbye to KMail (and eventually to the whole KDE-PIM package).</p>
<p>I started working on the italian translations of the KDE-PIM programs many years ago, that was my first contribute to the open source software (before I became a professional developer) and of course KDE and the PIM package were my beloved choice for every-day email and addressbook/calendar activities.</p>
<p>The pain started when KDE4 introduced akonadi and all the DB-backed stuff (virtuoso and its companions), let aside the endless configuration and installation problems, kmail it&#8217;s now completely unusable: for example I couldn&#8217;t make filters work anymore and it suddenly started asking for account&#8217;s passwords at reandom.</p>
<p>I&#8217;m a bit sad about leaving KMail, it has been a good pal and I liked its rich features and nice UI.</p>
<p>Goodbye KMail, it&#8217;s a real pity that KDE it&#8217;s slowly committing suicide.</p>
<p>BTW switching to thunderbird was not straight, consider using the utility described in the following link to convert your email folders: <a title="Bruno Cornec’s Blog" href="http://brunocornec.wordpress.com/2011/01/10/migrating-from-kmail-to-thunderbird/" target="_blank">Migrating from KMail to Thunderbird</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.itopen.it/2012/05/16/goodbye-kmail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>V-USB on Attiny85</title>
		<link>http://www.itopen.it/2012/05/05/v-usb-on-attiny85/</link>
		<comments>http://www.itopen.it/2012/05/05/v-usb-on-attiny85/#comments</comments>
		<pubDate>Sat, 05 May 2012 11:15:10 +0000</pubDate>
		<dc:creator>Alessandro Pasotti</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[AVR MCU]]></category>
		<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://www.itopen.it/?p=838</guid>
		<description><![CDATA[I&#8217;ve been recently fascinated by the V-USB project and its interesting applications to the ATtiny85 MCU. There are a couple of articles about its implementation on this tiny MCU but I&#8217;ve encountered many problems with the code and this is why I&#8217;m writing this notes. Most notable materials/projects I&#8217;ve found on the net are: V-USB [...]]]></description>
			<content:encoded><![CDATA[<p><a title="V-usb attiny85 di apasotti, su Flickr" href="http://www.flickr.com/photos/45502883@N06/7144413387/"><img class="alignright" src="http://farm9.staticflickr.com/8022/7144413387_8dcaf788b2_q.jpg" alt="V-usb attiny85" width="150" height="150" /></a></p>
<p>I&#8217;ve been recently fascinated by the<a title="V-USB project page" href="http://www.obdev.at/products/vusb/index.html" target="_blank"> V-USB project </a>and its interesting applications to the ATtiny85 MCU. There are a couple of articles about its implementation on this tiny MCU but I&#8217;ve encountered many problems with the code and this is why I&#8217;m writing this notes.</p>
<p>Most notable materials/projects I&#8217;ve found on the net are:</p>
<ul>
<li><a title="V-USB project page" href="http://www.obdev.at/products/vusb/index.html" target="_blank">V-USB project</a> website, not specific to Attiny85</li>
<li><a title="USB Tiny" href="http://dicks.home.xs4all.nl/avr/usbtiny/" target="_blank">usb tiny project</a></li>
<li><a title="I2C Tiny USB" href="http://www.harbaum.org/till/i2c_tiny_usb/index.shtml" target="_blank">I2C tiny USB</a></li>
<li><a title="EasyLogger" href="http://www.obdev.at/products/vusb/easylogger.html" target="_blank">EasyLogger</a></li>
<li><a title="attiny45" href="http://davehillier.wordpress.com/category/attiny45/" target="_blank">Attiny45 V-USB projects</a> , applies to Attiny45</li>
<li><a title="Code and Life" href="http://codeandlife.com/2012/02/22/v-usb-with-attiny45-attiny85-without-a-crystal/" target="_blank">Code and Life V-USB articles</a></li>
<li><a href="http://fabiobaltieri.com/2011/10/19/fun-with-attiny-and-v-usb/">Fun-with-attiny-and-v-usb</a></li>
<li><a href="http://www.ruinelli.ch/how-to-use-v-usb-on-an-attiny85">how-to-use-v-usb-on-an-attiny85</a></li>
</ul>
<div>Unfortunately, none of them worked out-of-the-box.  After following some of the tips indicated in the links above and I was able to run Hid-Mouse example from the main V-USB library and EasyLogger.</div>
<div></div>
<div>The compressed file linked below contains the schematic and the source code for the EasyLogger reference implementation, modified by me to run on my Attiny85, I just hope it will be useful if you want to start playing USB on that MCU.</div>
<div></div>
<div><a href="http://www.itopen.it/wp-content/uploads/2012/05/vusb-easy-logger-attiny85.tar.gz">vusb-easy-logger-attiny85.tar.gz</a></div>
<div></div>
<div></div>
<div>I&#8217;m still having random errors like:</div>
<pre>May  5 10:23:48 darkstar kernel: [ 5060.340336] input: obdev.at EasyLogger as /devices/pci0000:00/0000:00:1d.0/usb6/6-1/6-1:1.0/input/input36
May  5 10:23:48 darkstar kernel: [ 5060.341108] generic-usb 0003:4242:E131.0018: input,hidraw1: USB HID v1.01 Keyboard [obdev.at EasyLogger] on usb-0000:00:1d.0-1/input0
May  5 10:23:49 darkstar kernel: [ 5060.592176] hub 6-0:1.0: port 1 disabled by hub (EMI?), re-enabling...
May  5 10:23:49 darkstar kernel: [ 5060.592190] usb 6-1: USB disconnect, device number 23
May  5 10:23:49 darkstar kernel: [ 5060.852121] usb 6-1: new low-speed USB device number 24 using uhci_hcd
May  5 10:23:49 darkstar kernel: [ 5060.976057] usb 6-1: device descriptor read/64, error -71
May  5 10:23:49 darkstar kernel: [ 5061.204082] usb 6-1: device descriptor read/64, error -71
May  5 10:23:49 darkstar kernel: [ 5061.420119] usb 6-1: new low-speed USB device number 25 using uhci_hcd
May  5 10:23:50 darkstar kernel: [ 5061.544134] usb 6-1: device descriptor read/64, error -71
May  5 10:23:50 darkstar kernel: [ 5061.810138] usb 6-1: unable to read config index 0 descriptor/all
May  5 10:23:50 darkstar kernel: [ 5061.810151] usb 6-1: can't read configurations, error -71
May  5 10:23:50 darkstar kernel: [ 5061.924102] usb 6-1: new low-speed USB device number 26 using uhci_hcd
May  5 10:23:50 darkstar kernel: [ 5062.340078] usb 6-1: device not accepting address 26, error -71
May  5 10:23:50 darkstar kernel: [ 5062.452149] usb 6-1: new low-speed USB device number 27 using uhci_hcd
May  5 10:23:51 darkstar kernel: [ 5062.868115] usb 6-1: device not accepting address 27, error -71
May  5 10:23:51 darkstar kernel: [ 5062.868156] hub 6-0:1.0: unable to enumerate USB device on port 1
May  5 10:23:54 darkstar kernel: [ 5065.648077] hub 2-0:1.0: connect-debounce failed, port 1 disabled</pre>
<div>They are not reproducible and depend on which port/hub you connect the device, I suspect that I should use a shorter/better or shielded USB cable.</div>
<p>Have fun with Attiny85 and V-USB!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.itopen.it/2012/05/05/v-usb-on-attiny85/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My first Arduino shield: Attiny programmer support</title>
		<link>http://www.itopen.it/2012/04/21/my-first-arduino-shield-attiny-programmer-support/</link>
		<comments>http://www.itopen.it/2012/04/21/my-first-arduino-shield-attiny-programmer-support/#comments</comments>
		<pubDate>Sat, 21 Apr 2012 21:39:18 +0000</pubDate>
		<dc:creator>Alessandro Pasotti</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://www.itopen.it/?p=831</guid>
		<description><![CDATA[I was simply tired of all that wires on my breadboard and I had a small chunk of a stripboard left from another project. This small shield (a bit forced since Arduino has a wierd holes matrix and it&#8217;s not compatible with a standard  stripboard or protoboard) speeds up the development integrating the hearthbeat led [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Arduino Attiny shield di apasotti, su Flickr" href="http://www.flickr.com/photos/45502883@N06/6953980910/"><img class="alignright" src="http://farm6.staticflickr.com/5328/6953980910_abcac333d2_m.jpg" alt="Arduino Attiny shield" width="240" height="180" /></a></p>
<p>I was simply tired of all that wires on my breadboard and I had a small chunk of a stripboard left from another project.</p>
<p>This small shield (a bit forced since Arduino has a wierd holes matrix and it&#8217;s not compatible with a standard  stripboard or protoboard) speeds up the development integrating the hearthbeat led for Arduino ISP when using Arduino as the programmer for the Attiny. I also added the capacitor needed by my Arduino UNO rev. 2 and a spare led for Pin 0 (Pin 5 on the chip) so that I can quick test with a blink if the sketch is running fine.</p>
<p>Today I&#8217;ve also adapted my Makefile to work with the Attiny and got rid o that silly IDE.</p>
<p><iframe src="http://www.youtube.com/embed/_UukSq-uuiw" frameborder="0" width="560" height="315"></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://www.itopen.it/2012/04/21/my-first-arduino-shield-attiny-programmer-support/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shrinked Arduino: flashing an Attiny 85</title>
		<link>http://www.itopen.it/2012/04/20/shrinked-arduino-flashing-an-attiny-85/</link>
		<comments>http://www.itopen.it/2012/04/20/shrinked-arduino-flashing-an-attiny-85/#comments</comments>
		<pubDate>Fri, 20 Apr 2012 14:52:08 +0000</pubDate>
		<dc:creator>Alessandro Pasotti</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://www.itopen.it/?p=823</guid>
		<description><![CDATA[I&#8217;ve just started to explore the Attiny 85 small Arduino&#8217;s brother. To program the chip I&#8217;ve successfully followed the guide Programming an ATtiny w/ Arduino 1.0  , just a few quick notes: you can use Arduino IDE 1.0, after uploading Attiny Arduino libs into your sketchbook/hardware folder in the Arduino IDE: load Arduino as ISP sketch from examples [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Programming an Attiny85 di apasotti, su Flickr" href="http://www.flickr.com/photos/45502883@N06/7095933685/"><img class="alignright" src="http://farm6.staticflickr.com/5328/7095933685_1a464f3ce9.jpg" alt="Programming an Attiny85" width="500" height="376" /></a></p>
<p>I&#8217;ve just started to explore the Attiny 85 small Arduino&#8217;s brother.</p>
<p>To program the chip I&#8217;ve successfully followed the guide <a href="http://hlt.media.mit.edu/?p=1695" target="_blank">Programming an ATtiny w/ Arduino 1.0</a>  , just a few quick notes:</p>
<ul>
<li>you can use Arduino IDE 1.0, after uploading <a href="https://github.com/damellis/attiny/tree/Arduino1" target="_blank">Attiny Arduino libs</a> into your sketchbook/hardware folder</li>
<li>in the Arduino IDE: load Arduino as ISP sketch from examples and change in the heartbeat function the delay(40) to delay(20), then upload the sketch on your Arduino board</li>
<li>prepare the breadboard to flash the Attiny chip</li>
<li>if you have an Arduino UNO rev.2 you now MUST put a 10uF capacitor between reset and gnd pins on the Arduino board (not sure about other boards)</li>
<li>in the Arduino IDE: set the board to Attiny 85 (or to your chip type, if differs)</li>
<li>in the Arduino IDE: set the clock of the Attiny to 8MHz (or whatever you choose), this must be done only the first time you use the chip</li>
<li>in the Arduino IDE:open the blink sketch, change the pin from 13 to 0</li>
<li>click the upload upload (if you get errors, see below)</li>
</ul>
<div>Next step: how to use 3 PWM pins? It seems like it only has two.</div>
<div></div>
<div>If you get this error:</div>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">avrdude: stk500_initialize<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>: programmer not <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #c20cb9; font-weight: bold;">sync</span>, <span style="color: #007800;">resp</span>=0x15
avrdude: stk500_initialize<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>: programmer not <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #c20cb9; font-weight: bold;">sync</span>, <span style="color: #007800;">resp</span>=0x15
avrdude: please define PAGEL and BS2 signals <span style="color: #000000; font-weight: bold;">in</span> the configuration <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">for</span> part ATtiny85
avrdude: stk500_program_enable<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>: protocol error, <span style="color: #007800;">expect</span>=0x14, <span style="color: #007800;">resp</span>=0x12
avrdude: initialization failed, <span style="color: #007800;">rc</span>=-<span style="color: #000000;">1</span>
         Double check connections and try again, or use <span style="color: #660033;">-F</span> to override
         this check.</pre></div></div>

<div>just retry, usually the second time it goes through.</div>
]]></content:encoded>
			<wfw:commentRss>http://www.itopen.it/2012/04/20/shrinked-arduino-flashing-an-attiny-85/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arduino pong with 8&#215;8 led matrix and MAX7219</title>
		<link>http://www.itopen.it/2012/03/09/arduino-pong-with-8x8-led-matrix-and-max7219/</link>
		<comments>http://www.itopen.it/2012/03/09/arduino-pong-with-8x8-led-matrix-and-max7219/#comments</comments>
		<pubDate>Fri, 09 Mar 2012 14:19:38 +0000</pubDate>
		<dc:creator>Alessandro Pasotti</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://www.itopen.it/?p=808</guid>
		<description><![CDATA[Last night I had an hard time wiring a coupful of connectors to drive an 8&#215;8 led matrix, my breadbord is now really a jungle of wires! Given that I had no datasheet for the led matrix, I tried several combinations of wiring schemes until I found the right one on http://arduino.cc/playground/Main/DirectDriveLEDMatrix. Wiring that beast is [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_812" class="wp-caption alignright" style="width: 310px"><a href="http://www.itopen.it/wp-content/uploads/2012/03/20120309_003.jpg"><img class="size-medium wp-image-812" title="The jungle breadboard" src="http://www.itopen.it/wp-content/uploads/2012/03/20120309_003-300x225.jpg" alt="" width="300" height="225" /></a><p class="wp-caption-text">The jungle breadboard</p></div>
<p>Last night I had an hard time wiring a coupful of connectors to drive an 8&#215;8 led matrix, my breadbord is now really a jungle of wires!</p>
<p>Given that I had no datasheet for the led matrix, I tried several combinations of wiring schemes until I found the right one on <a href="http://arduino.cc/playground/Main/DirectDriveLEDMatrix">http://arduino.cc/playground/Main/DirectDriveLEDMatrix</a>.</p>
<p>Wiring that beast is tedious and making mistakes is really easy, but after a few tries I could successfully run the test sketch.</p>
<p>Second question was what (possibly useless) object to make with the matrix, I saw some cool projects on the net, the most exciting being  a ping-pong clone at <a href="http://blog.bsoares.com.br/arduino/ping-pong-with-8x8-led-matrix-on-arduino">http://blog.bsoares.com.br/arduino/ping-pong-with-8&#215;8-led-matrix-on-arduino</a>.</p>
<p>Since I just have one potentiometer I choose a simpler variant, and I tried to implement the whole game from the ground up without looking at someone else&#8217;s code.</p>
<p>To drive the matrix I used a MAX7219 chip which only takes three Arduino pins, it has a nice library: <a href="http://arduino.cc/playground/Main/LedControl">http://arduino.cc/playground/Main/LedControl</a> which allows to light single leds, rows and columns independently.</p>
<p>I also used the great <a href="http://arduino.cc/playground/Code/Timer">http://arduino.cc/playground/Code/Timer</a> library to adjust the ball speed.</p>
<p>This video shows the results of my efforts.</p>
<p><iframe src="http://www.youtube.com/embed/CfSgJctghk0" frameborder="0" width="560" height="315"></iframe></p>
<p>The code still needs some polishing, but it&#8217;s basically working.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;">/**
*  Play pong on an 8x8 matrix
*
*    0 1 2 3 4 5 6 7
*  0
*  1
*  2      7 0 1
*  3      6 X 2 (directions)
*  4      5 4 3
*  5
*  6
*  7    [pad]         
*
*
*
*
*/</span>
&nbsp;
&nbsp;
<span style="color: #339933;">#include &quot;LedControl.h&quot;</span>
<span style="color: #339933;">#include &quot;Timer.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #339933;">#define POTPIN A5 // Potentiometer</span>
<span style="color: #339933;">#define PADSIZE 3</span>
<span style="color: #339933;">#define BALL_DELAY 200</span>
<span style="color: #339933;">#define GAME_DELAY 10</span>
<span style="color: #339933;">#define BOUNCE_VERTICAL 1</span>
<span style="color: #339933;">#define BOUNCE_HORIZONTAL -1</span>
<span style="color: #339933;">#define NEW_GAME_ANIMATION_SPEED 50</span>
<span style="color: #339933;">#define HIT_NONE 0</span>
<span style="color: #339933;">#define HIT_CENTER 1</span>
<span style="color: #339933;">#define HIT_LEFT 2</span>
<span style="color: #339933;">#define HIT_RIGHT 3</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">//#define DEBUG 1</span>
&nbsp;
byte sad<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
B00000000<span style="color: #339933;">,</span>
B01000100<span style="color: #339933;">,</span>
B00010000<span style="color: #339933;">,</span>
B00010000<span style="color: #339933;">,</span>
B00000000<span style="color: #339933;">,</span>
B00111000<span style="color: #339933;">,</span>
B01000100<span style="color: #339933;">,</span>
B00000000
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
byte smile<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
B00000000<span style="color: #339933;">,</span>
B01000100<span style="color: #339933;">,</span>
B00010000<span style="color: #339933;">,</span>
B00010000<span style="color: #339933;">,</span>
B00010000<span style="color: #339933;">,</span>
B01000100<span style="color: #339933;">,</span>
B00111000<span style="color: #339933;">,</span>
B00000000
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
Timer timer<span style="color: #339933;">;</span>
&nbsp;
LedControl lc <span style="color: #339933;">=</span> LedControl<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #339933;">,</span><span style="color: #0000dd;">3</span><span style="color: #339933;">,</span><span style="color: #0000dd;">2</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
byte direction<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Wind rose, 0 is north</span>
<span style="color: #993333;">int</span> xball<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> yball<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> yball_prev<span style="color: #339933;">;</span>
byte xpad<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> ball_timer<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span> setSprite<span style="color: #009900;">&#40;</span>byte <span style="color: #339933;">*</span>sprite<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> r <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> r <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span> r<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        lc.<span style="color: #202020;">setRow</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> r<span style="color: #339933;">,</span> sprite<span style="color: #009900;">&#91;</span>r<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> newGame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    lc.<span style="color: #202020;">clearDisplay</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// initial position</span>
    xball <span style="color: #339933;">=</span> random<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    yball <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    direction <span style="color: #339933;">=</span> random<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Go south</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> r <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> r <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span> r<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> c <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> c <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span> c<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            lc.<span style="color: #202020;">setLed</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> r<span style="color: #339933;">,</span> c<span style="color: #339933;">,</span> HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            delay<span style="color: #009900;">&#40;</span>NEW_GAME_ANIMATION_SPEED<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    setSprite<span style="color: #009900;">&#40;</span>smile<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    lc.<span style="color: #202020;">clearDisplay</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> setPad<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    xpad <span style="color: #339933;">=</span> map<span style="color: #009900;">&#40;</span>analogRead<span style="color: #009900;">&#40;</span>POTPIN<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1020</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">8</span> <span style="color: #339933;">-</span> PADSIZE<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> debug<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">char</span><span style="color: #339933;">*</span> desc<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #339933;">#ifdef DEBUG</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>desc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; XY: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>xball<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;, &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>yball<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; XPAD: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>xpad<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; DIR: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>direction<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#endif</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> checkBounce<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>xball <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>yball <span style="color: #339933;">||</span> xball <span style="color: #339933;">==</span> <span style="color: #0000dd;">7</span> <span style="color: #339933;">||</span> yball <span style="color: #339933;">==</span> <span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #993333;">int</span> bounce <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>yball <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">||</span> yball <span style="color: #339933;">==</span> <span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> BOUNCE_HORIZONTAL <span style="color: #339933;">:</span> BOUNCE_VERTICAL<span style="color: #339933;">;</span>
<span style="color: #339933;">#ifdef DEBUG</span>
        debug<span style="color: #009900;">&#40;</span>bounce <span style="color: #339933;">==</span> BOUNCE_HORIZONTAL <span style="color: #339933;">?</span> <span style="color: #ff0000;">&quot;HORIZONTAL&quot;</span> <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;VERTICAL&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#endif</span>
        <span style="color: #b1b100;">return</span> bounce<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> getHit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>yball <span style="color: #339933;">!=</span> <span style="color: #0000dd;">6</span> <span style="color: #339933;">||</span> xball <span style="color: #339933;">&lt;</span> xpad <span style="color: #339933;">||</span> xball <span style="color: #339933;">&gt;</span> xpad <span style="color: #339933;">+</span> PADSIZE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> HIT_NONE<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>xball <span style="color: #339933;">==</span> xpad <span style="color: #339933;">+</span> PADSIZE <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> HIT_CENTER<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> xball <span style="color: #339933;">&lt;</span> xpad <span style="color: #339933;">+</span> PADSIZE <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span> <span style="color: #339933;">?</span> HIT_LEFT <span style="color: #339933;">:</span> HIT_RIGHT<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
bool checkLoose<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> yball <span style="color: #339933;">==</span> <span style="color: #0000dd;">6</span> <span style="color: #339933;">&amp;&amp;</span> getHit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> HIT_NONE<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> moveBall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    debug<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MOVE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> bounce <span style="color: #339933;">=</span> checkBounce<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>bounce<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>direction<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">:</span>
                direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">:</span>
                direction <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>bounce <span style="color: #339933;">==</span> BOUNCE_VERTICAL<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #0000dd;">7</span> <span style="color: #339933;">:</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">:</span>
                direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">6</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">6</span><span style="color: #339933;">:</span>
                direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">:</span>
                direction <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>bounce <span style="color: #339933;">==</span> BOUNCE_VERTICAL<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">:</span> <span style="color: #0000dd;">5</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">5</span><span style="color: #339933;">:</span>
                direction <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>bounce <span style="color: #339933;">==</span> BOUNCE_VERTICAL<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #0000dd;">3</span> <span style="color: #339933;">:</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">:</span>
                direction <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>bounce <span style="color: #339933;">==</span> BOUNCE_VERTICAL<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #0000dd;">5</span> <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">:</span>
                direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        debug<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;-&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Check hit: modify direction is left or right</span>
    <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>getHit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">case</span> HIT_LEFT<span style="color: #339933;">:</span>
            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                direction <span style="color: #339933;">=</span>  <span style="color: #0000dd;">7</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> HIT_RIGHT<span style="color: #339933;">:</span>
            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #0000dd;">7</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Check orthogonal directions and borders ...</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">&amp;&amp;</span> xball <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #0000dd;">4</span> <span style="color: #339933;">&amp;&amp;</span> xball <span style="color: #339933;">==</span> <span style="color: #0000dd;">7</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        direction<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">&amp;&amp;</span> xball <span style="color: #339933;">==</span> <span style="color: #0000dd;">7</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #0000dd;">4</span> <span style="color: #339933;">&amp;&amp;</span> xball <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #0000dd;">2</span> <span style="color: #339933;">&amp;&amp;</span> yball <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #0000dd;">2</span> <span style="color: #339933;">&amp;&amp;</span> yball <span style="color: #339933;">==</span> <span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #0000dd;">6</span> <span style="color: #339933;">&amp;&amp;</span> yball <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">5</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #0000dd;">6</span> <span style="color: #339933;">&amp;&amp;</span> yball <span style="color: #339933;">==</span> <span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// &quot;Corner&quot; case</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>xball <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">&amp;&amp;</span> yball <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>xball <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">&amp;&amp;</span> yball <span style="color: #339933;">==</span> <span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>xball <span style="color: #339933;">==</span> <span style="color: #0000dd;">7</span> <span style="color: #339933;">&amp;&amp;</span> yball <span style="color: #339933;">==</span> <span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>xball <span style="color: #339933;">==</span> <span style="color: #0000dd;">7</span> <span style="color: #339933;">&amp;&amp;</span> yball <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        direction <span style="color: #339933;">=</span> <span style="color: #0000dd;">5</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    yball_prev <span style="color: #339933;">=</span> yball<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span> <span style="color: #339933;">&lt;</span> direction <span style="color: #339933;">&amp;&amp;</span> direction <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        yball<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">!=</span> <span style="color: #0000dd;">6</span> <span style="color: #339933;">&amp;&amp;</span> direction <span style="color: #339933;">!=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        yball<span style="color: #339933;">--;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">&lt;</span> direction <span style="color: #339933;">&amp;&amp;</span> direction <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        xball<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">!=</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">&amp;&amp;</span> direction <span style="color: #339933;">!=</span> <span style="color: #0000dd;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        xball<span style="color: #339933;">--;</span>
    <span style="color: #009900;">&#125;</span>
    xball <span style="color: #339933;">=</span> max<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> min<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">7</span><span style="color: #339933;">,</span> xball<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    yball <span style="color: #339933;">=</span> max<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> min<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">6</span><span style="color: #339933;">,</span> yball<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debug<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;AFTER MOVE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> gameOver<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    setSprite<span style="color: #009900;">&#40;</span>sad<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    lc.<span style="color: #202020;">clearDisplay</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> drawGame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>yball_prev <span style="color: #339933;">!=</span> yball<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        lc.<span style="color: #202020;">setRow</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> yball_prev<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    lc.<span style="color: #202020;">setRow</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> yball<span style="color: #339933;">,</span> byte<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #009900;">&#40;</span>xball<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    byte padmap <span style="color: #339933;">=</span> byte<span style="color: #009900;">&#40;</span><span style="color: #208080;">0xFF</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">8</span> <span style="color: #339933;">-</span> PADSIZE<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> xpad<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #339933;">#ifdef DEBUG</span>
    <span style="color: #666666; font-style: italic;">//Serial.println(padmap, BIN);</span>
<span style="color: #339933;">#endif</span>
    lc.<span style="color: #202020;">setRow</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">,</span> padmap<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// The MAX72XX is in power-saving mode on startup,</span>
  <span style="color: #666666; font-style: italic;">// we have to do a wakeup call</span>
  pinMode<span style="color: #009900;">&#40;</span>POTPIN<span style="color: #339933;">,</span> INPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  lc.<span style="color: #202020;">shutdown</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Set the brightness to a medium values</span>
  lc.<span style="color: #202020;">setIntensity</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// and clear the display</span>
  lc.<span style="color: #202020;">clearDisplay</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  randomSeed<span style="color: #009900;">&#40;</span>analogRead<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#ifdef DEBUG</span>
  Serial.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">9600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Pong&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#endif</span>
  newGame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  ball_timer <span style="color: #339933;">=</span> timer.<span style="color: #202020;">every</span><span style="color: #009900;">&#40;</span>BALL_DELAY<span style="color: #339933;">,</span> moveBall<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    timer.<span style="color: #202020;">update</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Move pad</span>
    setPad<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#ifdef DEBUG</span>
    Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>xpad<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#endif</span>
    <span style="color: #666666; font-style: italic;">// Update screen</span>
    drawGame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>checkLoose<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        debug<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;LOOSE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gameOver<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        newGame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    delay<span style="color: #009900;">&#40;</span>GAME_DELAY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.itopen.it/2012/03/09/arduino-pong-with-8x8-led-matrix-and-max7219/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Driving a 7-segments display with arduino CD4511 and pyfirmata</title>
		<link>http://www.itopen.it/2012/03/02/driving-a-7-segments-display-with-arduino-cd4511-and-pyfirmata/</link>
		<comments>http://www.itopen.it/2012/03/02/driving-a-7-segments-display-with-arduino-cd4511-and-pyfirmata/#comments</comments>
		<pubDate>Fri, 02 Mar 2012 12:02:54 +0000</pubDate>
		<dc:creator>Alessandro Pasotti</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://www.itopen.it/?p=792</guid>
		<description><![CDATA[My experiments with the Arduino platform continue with a small 7-segments common cathode LED display and a cheap IC (Integrated Circuit) CD4511. This IC is basically a BCD (Binary Coded Decimal) to 7-segments driver, you feed it with a 4-bit array and it lights up corresponding segments on the display, additional pins on the IC allow [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_800" class="wp-caption alignright" style="width: 310px"><a href="http://www.itopen.it/wp-content/uploads/2012/03/7segments_board.jpg"><img class="size-medium wp-image-800" title="7segments_board" src="http://www.itopen.it/wp-content/uploads/2012/03/7segments_board-300x225.jpg" alt="" width="300" height="225" /></a><p class="wp-caption-text">Driving a 7-segments LED display with Arduino and a DC4511</p></div><br />
My experiments with the Arduino platform continue with a small 7-segments common cathode LED display and a cheap IC (Integrated Circuit) <a href="http://talkingelectronics.com/ChipDataEbook-1d/html/4511.html">CD4511</a>.</p>
<p>This IC is basically a <a href="http://en.wikipedia.org/wiki/Binary-coded_decimal">BCD (Binary Coded Decimal)</a> to 7-segments driver, you feed it with a 4-bit array and it lights up corresponding segments on the display, additional pins on the IC allow you to switch on and off all leds and to &#8220;freeze&#8221; (store) the display.</p>
<p>The advantage of using the 4511 is that you can consume just 4 arduino pins instead of the 7 that you would need when controlling the individual display segments one-by-one (set aside the decimal point, which would count to 8).</p>
<p>Note that the 4511 has 7 outputs only, this means that you cannot use it to control the DP (Decimal Point)  rounded led in the display.</p>
<p>I&#8217;m using firmata and pyfirmata (see my previous article about firmata: <a href="http://www.itopen.it/2012/02/21/arduino-pyfirmata-ldr-semaphore/">http://www.itopen.it/2012/02/21/arduino-pyfirmata-ldr-semaphore/</a> )  because I wanted to interactively control what was going on issuing commands on my favourite python shell (<a href="http://ipython.org/">http://ipython.org/</a>) .</p>
<p>The code is quite self explanatory, the wiring is listed at the top (see also the picture attached to this article), this script basically counts up and down from 0 to 9 and blinks when reaches 0 and 9.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;&quot;&quot;
driving 7 segments led display with a CD4511 IC
&nbsp;
Wiring:
&nbsp;
    CD4511    Connection
    1   A1    arduino pin 9
    2   A2    arduino pin 10
    3   LT    arduino pin 3 (lamp test)
    4   BL    arduino pin 2 (blink)
    5   LE    GND
    6   A3    arduino pin 11
    7   A7    arduino pin 8
    8   GND   GND
    9   e     <span style="color: #000099; font-weight: bold;">\</span>
    10  d     |     AA
    11  c     |    F  B
    12  b      &gt;    GG
    13  a     |    E  C
    14  g     |     DD
    15  f     /
    16  +5V   +5V
&nbsp;
&quot;&quot;&quot;</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">from</span> pyfirmata <span style="color: #ff7700;font-weight:bold;">import</span> Arduino, util
board = Arduino<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/dev/ttyACM0'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Assign data pins</span>
pins = <span style="color: black;">&#91;</span>board.<span style="color: black;">get_pin</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'d:'</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>+<span style="color: #483d8b;">':o'</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">8</span>, <span style="color: #ff4500;">9</span>, <span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">11</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>
<span style="color: #808080; font-style: italic;"># BLank pin</span>
bl = board.<span style="color: black;">get_pin</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'d:2:o'</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># Light Test pin</span>
lt = board.<span style="color: black;">get_pin</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'d:3:o'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> blink<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Blink active leds&quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;blinking...&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>:
        bl.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span>
        bl.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span>
    bl.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> show_digit<span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Shows a digit on the display&quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;showing digit %s&quot;</span> <span style="color: #66cc66;">%</span> num
    <span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>:
        pins<span style="color: black;">&#91;</span>p<span style="color: black;">&#93;</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>num <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
        num /= <span style="color: #ff4500;">2</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Test lamp</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Testing lamp...&quot;</span>
lt.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
lt.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
bl.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;...done&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;start counting...&quot;</span>
&nbsp;
cycle = <span style="color: #ff4500;">0</span>
<span style="color: #ff7700;font-weight:bold;">while</span> cycle <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">100</span>:
    <span style="color: #ff7700;font-weight:bold;">for</span> d <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>:
        show_digit<span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.25</span><span style="color: black;">&#41;</span>
    blink<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> d <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">9</span>, -<span style="color: #ff4500;">1</span>, -<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
        show_digit<span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.25</span><span style="color: black;">&#41;</span>
    blink<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.itopen.it/2012/03/02/driving-a-7-segments-display-with-arduino-cd4511-and-pyfirmata/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arduino pyfirmata LDR semaphore</title>
		<link>http://www.itopen.it/2012/02/21/arduino-pyfirmata-ldr-semaphore/</link>
		<comments>http://www.itopen.it/2012/02/21/arduino-pyfirmata-ldr-semaphore/#comments</comments>
		<pubDate>Tue, 21 Feb 2012 10:08:34 +0000</pubDate>
		<dc:creator>Alessandro Pasotti</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://www.itopen.it/?p=775</guid>
		<description><![CDATA[A few days ago I was experimenting with the firmata library on Arduino uno, the simple experiment I describe here is about a LED semaphore triggered by a Ligh Dependent Resistor (LDR). Firmata is a software library which implements the firmata protocol, better described in the website http://firmata.org/wiki/Main_Page : Firmata is a generic protocol for communicating [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago I was experimenting with the firmata library on Arduino uno, the simple experiment I describe here is about a LED semaphore triggered by a Ligh Dependent Resistor (LDR).</p>
<p>Firmata is a software library which implements the firmata protocol, better described in the website <a href="http://firmata.org/wiki/Main_Page">http://firmata.org/wiki/Main_Page</a> :</p>
<blockquote><p>Firmata is a generic protocol for communicating with microcontrollers from software on a host computer.</p></blockquote>
<p>Basically, it allows to send commands on a serial link to an Arduino board from a host PC.</p>
<p>Of course you could open a serial terminal and issue the commands typing on the keyboard, luckily enough libraries that &#8220;speaks firmata&#8221; are available for all the main programming languages, here I will use pyfirmata, which is a firmata library for python.</p>
<p>To reproduce this experiment, you will need to:</p>
<ul>
<li>install firmata binary on Arduino board</li>
<li>installa pyfirmata on the host</li>
<li>assemble the circuit on the breadboard</li>
<li>start sending commands from the host</li>
</ul>
<p>Part list for the circuit:</p>
<ul>
<li>3 x leds: green red and yellow (it&#8217;s a semaphore, after all)</li>
<li>3 x 330 ohm resistors to limit current in the leds</li>
<li>1 x LDR (light dependent resistor)</li>
</ul>
<h2>Install firmata on Arduino</h2>
<p>You should find Firmata on you standard Arduino examples, fire your IDE or search for Firmata on the example folder and load it on the board, here is where I found it on my dev machine:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>arduino-<span style="color: #000000;">1.0</span><span style="color: #000000; font-weight: bold;">/</span>libraries<span style="color: #000000; font-weight: bold;">/</span>Firmata<span style="color: #000000; font-weight: bold;">/</span>examples<span style="color: #000000; font-weight: bold;">/</span>StandardFirmata<span style="color: #000000; font-weight: bold;">/</span>StandardFirmata.ino</pre></div></div>

<p>You can install it as you would do for an ordinary sketch.</p>
<h2>Install pyfirmata on your host PC</h2>
<p>You can get the sources from the repository below:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span>
hg clone https:<span style="color: #000000; font-weight: bold;">//</span>bitbucket.org<span style="color: #000000; font-weight: bold;">/</span>tino<span style="color: #000000; font-weight: bold;">/</span>pyfirmata
<span style="color: #7a0874; font-weight: bold;">cd</span> pyfirmata
<span style="color: #c20cb9; font-weight: bold;">sudo</span> python manage.py <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Now you can connect your board and test the library with a simple LED 13 blink program which turns on and off the on-board LED connected on pin 13:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> pyfirmata <span style="color: #ff7700;font-weight:bold;">import</span> Arduino, util
board=Arduino<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/dev/ttyACM0'</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># adapt to your device address</span>
ledpin = board.<span style="color: black;">get_pin</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'d:13:o'</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># d=digital 13=pin number o=output</span>
ledpin.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
ledpin.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span></pre></div></div>

<h2>Green light to go!</h2>
<p><a href="http://www.itopen.it/wp-content/uploads/2012/02/semaphore_board.jpg"><img class="alignright size-thumbnail wp-image-787" title="semaphore_board" src="http://www.itopen.it/wp-content/uploads/2012/02/semaphore_board-150x150.jpg" alt="Semaphore circuit, click to enlarge." width="150" height="150" /></a>Here is the semaphore script, see the picture for the circuit details.</p>
<p>The idea is to control the semaphore through a LDR, when you darken the sensor, the semaphore goes green (after two cycles the program ends).</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">from</span> pyfirmata <span style="color: #ff7700;font-weight:bold;">import</span> Arduino, util
&nbsp;
board = Arduino<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/dev/ttyACM0'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;
rosso: pin D 8  -&gt; red
giallo: pin D 11 -&gt; yellow
verde: pin D 13 -&gt; green
light pin A 0 -&gt; LDR
&nbsp;
&quot;&quot;&quot;</span>
DELAY=<span style="color: #ff4500;">1</span>
GREEN=<span style="color: #ff4500;">7</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># GYR 13 11 8</span>
g = board.<span style="color: black;">get_pin</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'d:13:o'</span><span style="color: black;">&#41;</span>
y = board.<span style="color: black;">get_pin</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'d:11:o'</span><span style="color: black;">&#41;</span>
r = board.<span style="color: black;">get_pin</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'d:8:o'</span><span style="color: black;">&#41;</span>
&nbsp;
l = board.<span style="color: black;">get_pin</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'a:0:i'</span><span style="color: black;">&#41;</span>
&nbsp;
iterator = util.<span style="color: black;">Iterator</span><span style="color: black;">&#40;</span>board<span style="color: black;">&#41;</span>
iterator.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
r.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> go_green<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	y.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span>DELAY<span style="color: black;">&#41;</span>
	r.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
	g.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
	y.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
	<span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span>GREEN<span style="color: black;">&#41;</span>
	y.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
	<span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span>DELAY<span style="color: black;">&#41;</span>
	g.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
	y.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
	r.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
cycles = <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">while</span> cycles <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">2</span>: 	
       val = l.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> 	
       <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Val %s&quot;</span> <span style="color: #66cc66;">%</span> val 	
       <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> 	
       <span style="color: #ff7700;font-weight:bold;">if</span> val <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">0.8</span>:
		cycles+=<span style="color: #ff4500;">1</span>
		go_green<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
board.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.itopen.it/2012/02/21/arduino-pyfirmata-ldr-semaphore/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Finding unique CSS colors with grep and perl</title>
		<link>http://www.itopen.it/2012/02/14/finding-unique-css-values-with-grep-and-perl/</link>
		<comments>http://www.itopen.it/2012/02/14/finding-unique-css-values-with-grep-and-perl/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 08:28:50 +0000</pubDate>
		<dc:creator>Alessandro Pasotti</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.itopen.it/?p=759</guid>
		<description><![CDATA[Just a tiny one-liner I&#8217;ve found useful to get unique CSS color values in a style sheet: egrep -r '#[a-fA-F0-9]{3,6}+(\s&#124;;)' *.css&#124;perl -ne 'if (m/(#[a-fA-F0-9]{3,6}+)(\s&#124;;)/){print &#34;$1\n&#34;;}'&#124;sort&#124;uniq Suggestions and improvements are welcome!]]></description>
			<content:encoded><![CDATA[<p>Just a tiny one-liner I&#8217;ve found useful to get unique CSS color values in a style sheet:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-r</span> <span style="color: #ff0000;">'#[a-fA-F0-9]{3,6}+(\s|;)'</span> <span style="color: #000000; font-weight: bold;">*</span>.css<span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">'if (m/(#[a-fA-F0-9]{3,6}+)(\s|;)/){print &quot;$1\n&quot;;}'</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">sort</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">uniq</span></pre></div></div>

<p>Suggestions and improvements are welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.itopen.it/2012/02/14/finding-unique-css-values-with-grep-and-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arduino 1.0 development with a makefile on Linux</title>
		<link>http://www.itopen.it/2012/02/12/arduino-1-0-development-with-a-makefile/</link>
		<comments>http://www.itopen.it/2012/02/12/arduino-1-0-development-with-a-makefile/#comments</comments>
		<pubDate>Sun, 12 Feb 2012 15:06:18 +0000</pubDate>
		<dc:creator>Alessandro Pasotti</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[Varie]]></category>

		<guid isPermaLink="false">http://www.itopen.it/?p=747</guid>
		<description><![CDATA[A few months ago I started playing with an Arduino UNO R2 board (yes, the flawed one, I had to solder a diode to convince the board to load sketches&#8230;). One of the machines I use for development is ARM based, so I couldn&#8217;t use the newest Arduino versions which are downloadable from the website, [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-754" title="Arduino-logo" src="http://www.itopen.it/wp-content/uploads/2012/02/Arduino-logo.png" alt="" width="192" height="134" /></p>
<p>A few months ago I started playing with an Arduino UNO R2 board (yes, the flawed one, I had to solder a diode to convince the board to load sketches&#8230;). One of the machines I use for development is ARM based, so I couldn&#8217;t use the newest Arduino versions which are downloadable from the website, because  they are available only for 32 and 64 bit architectures.</p>
<p>But the main reason for not using Arduino IDE is that I prefer vim or other IDEs to the one provided with Arduino package.</p>
<p>Finding a working Makefile was not an easy job: I tried first to adapt the old Makefile I was using with the older Arduino codebase but it was clearly not compatibile, after having tried to adapt some other makefiles, finally I discovered a mostly working and very well written Makefile from <a href="http://ed.am/dev/make/arduino-mk">http://ed.am/dev/make/arduino-mk</a>, I had to patch it to support more configuration overrides and to fix the missing compilation of &#8220;c&#8221; source utilities from the Wire library.</p>
<p>Meanwhile, I contacted Tim and he was independently fixing the same issues, after a few emails and tests we now have a wonderfull arduino Makefile: thanks Tim!</p>
<h2>Advantages of the Makefile approach</h2>
<p>Arduino IDE sucks. Ok, I admit it&#8217;s nice for beginners and for the click-and-forget kind of programmers, but it needs the full Java stack and hides to the user the fundamental steps involved in compiling and uploading programs to the arduino board.</p>
<p>I (an many other programmers) prefer to understand what I&#8217;m doing and running a Makefile from the command line is the way to see the individual steps and choose which to run and when. Finally, I&#8217;m not going to drop my favourite editor just to write arduino programs.</p>
<p>If your editor is vim, you will find this syntax highlighter useful: <a href="http://www.vim.org/scripts/script.php?script_id=2654">http://www.vim.org/scripts/script.php?script_id=2654</a>.</p>
<h2>Prepare your environment</h2>
<p>There is nothing particular to do here, what you need is basically:</p>
<ol>
<li>the arduino libraries (source code)</li>
<li>the Makefile</li>
<li>the avr gcc compiler toolchain (the compiler and linker)</li>
<li>avrdude (the program to speak with the MCU and upload the binaries)</li>
</ol>
<h3>Install arduino libraries</h3>
<p>You can install the libraries with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> arduino</pre></div></div>

<p>or dowload the latest version from here:</p>
<p><a href="http://arduino.cc/en/Main/Software">http://arduino.cc/en/Main/Software</a> (follow the source Luke: download the source tarball for Linux)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span>
<span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>arduino.googlecode.com<span style="color: #000000; font-weight: bold;">/</span>files<span style="color: #000000; font-weight: bold;">/</span>arduino-<span style="color: #000000;">1.0</span>-src.tar.gz
<span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xzvf</span> arduino-<span style="color: #000000;">1.0</span>-src.tar.gz</pre></div></div>

<h3>Get the Makefile</h3>
<p>then grab the latest Makefile, put it in your sketchbook folder (the folder where your arduino programs are kept, create one if it does not exists, the folder name can be changed if you like):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> sketchbook
<span style="color: #7a0874; font-weight: bold;">cd</span> sketchbook
<span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>ed.am<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>make<span style="color: #000000; font-weight: bold;">/</span>arduino-mk<span style="color: #000000; font-weight: bold;">/</span>arduino.mk
<span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-s</span> arduino.mk Makefile</pre></div></div>

<p>The Makefile configuration is normally very limited, you just tell the Makefile which board type you are using (e.g. &#8220;uno&#8221;) and, in a few cases, where are installed your arduino libraries.</p>
<p>Except for the board type which cannot be guessed in any way,  the Makefile is smart enough to guess all other configuration parameters, which can be overriden setting environment variables for the need.</p>
<p>In the case of manual source installation above, additional configuration would be:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;export ARDUINODIR=~/arduino-1.0&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> ~<span style="color: #000000; font-weight: bold;">/</span>.bashrc
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;export BOARD=uno&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> ~<span style="color: #000000; font-weight: bold;">/</span>.bashrc
<span style="color: #7a0874; font-weight: bold;">source</span> ~<span style="color: #000000; font-weight: bold;">/</span>.bashrc</pre></div></div>

<p>An important convention is that the program you are going to compile resides in a folder with the same name, see the examples below.</p>
<h3>Install the gcc AVR compiler and avrdude</h3>
<p>If you have installed arduino with apt-get you have already installed the compiler and avrdude, if you have downloaded the sources, you need to install the compiler and avrdude with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> avrdude binutils-avr gcc-avr avr-libc gdb-avr</pre></div></div>

<h2>Test sketch</h2>
<p>The steps are:</p>
<ul>
<li>create a directory with the same name of the sketch, this must be a subdirectory of the the sketchbook directory you have created above</li>
<li>create the sketch with your favourite editor and save into the directory, the name must be the same, file extension  can be &#8220;.ino&#8221; or &#8220;.pde&#8221;</li>
<li>connect your board to the USB plug</li>
<li>compile and upload (in one step)</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>sketchbook
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> blink
<span style="color: #7a0874; font-weight: bold;">cd</span> blink
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;void setup(){pinMode(13,OUTPUT);}void loop(){digitalWrite(13,HIGH);delay(1000);digitalWrite(13,LOW);delay(1000);}&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> blink.ino
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #660033;">-f</span> ..<span style="color: #000000; font-weight: bold;">/</span>Makefile</pre></div></div>

<p>That&#8217;s all!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.itopen.it/2012/02/12/arduino-1-0-development-with-a-makefile/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>HtmlPurifier plugin for Joomla 2.5</title>
		<link>http://www.itopen.it/2012/02/07/htmlpurifier-plugin-for-joomla-2-5/</link>
		<comments>http://www.itopen.it/2012/02/07/htmlpurifier-plugin-for-joomla-2-5/#comments</comments>
		<pubDate>Tue, 07 Feb 2012 09:25:56 +0000</pubDate>
		<dc:creator>Alessandro Pasotti</dc:creator>
				<category><![CDATA[Joomla]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[[lang_it]Accessibilità[/lang_it][lang_en]Accessibility[/lang_en]]]></category>

		<guid isPermaLink="false">http://www.itopen.it/?p=735</guid>
		<description><![CDATA[I&#8217;ve recently discovered that the good old HtmlPurifier content plugin for Joomla wasn&#8217;t working with the latest Joomla 2.5.x due to some subtle changes in the Joomla autoloader stuff. I had to patch the plugin to store spl_autoload_functions before registering its own functions, the library method hit a PHP issue which prevents re-registering of spl [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently discovered that the good old HtmlPurifier content plugin for Joomla wasn&#8217;t working with the latest Joomla 2.5.x due to some subtle changes in the Joomla autoloader stuff.</p>
<p>I had to patch the plugin to store <strong>spl_autoload_functions</strong> before registering its own functions, the library method hit a PHP issue which prevents re-registering of spl abstract methods which were registerd by a private constructor of a child class: this definitely makes sense but in this case it&#8217;s just an idle complication.</p>
<p>The patched plugin is available for download from my <a href="https://github.com/elpaso/joomla-fap-25/blob/master/release/plg_htmlpurifier_content_2.5_itopen.zip?raw=true">github project page</a>, please report any issue on <a href="https://github.com/elpaso/joomla-fap-25/issues">the tracker</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.itopen.it/2012/02/07/htmlpurifier-plugin-for-joomla-2-5/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

