<?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/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
xmlns:rawvoice="http://www.rawvoice.com/rawvoiceRssModule/"
>

<channel>
	<title>KwartzLab Makerspace &#187; interrupt</title>
	<atom:link href="http://www.kwartzlab.ca/tag/interrupt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kwartzlab.ca</link>
	<description>Home of Kwartzlab Makerspace in Kitchener/Waterloo, Ontario</description>
	<lastBuildDate>Wed, 19 Jun 2013 04:07:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
<!-- podcast_generator="Blubrry PowerPress/4.0.7" -->
	<itunes:summary>Regular discussions with hackers, makers and artists at the Kwartzlab Makerspace. We talk about what projects people are working on, what events are coming up and how you can get involved.</itunes:summary>
	<itunes:author>kwartzlab</itunes:author>
	<itunes:explicit>no</itunes:explicit>
	<itunes:image href="http://www.kwartzlab.ca/wp-content/uploads/powerpress/light_box_logo.jpg" />
	<itunes:owner>
		<itunes:name>kwartzlab</itunes:name>
		<itunes:email>podcast@kwartzlab.ca</itunes:email>
	</itunes:owner>
	<managingEditor>podcast@kwartzlab.ca (kwartzlab)</managingEditor>
	<itunes:subtitle>A hackerspace radio show</itunes:subtitle>
	<itunes:keywords>kwartzlab, hackerspace, makerspace, diy, hardware, software, maker, hacker, artist, roundtable</itunes:keywords>
	<image>
		<title>KwartzLab Makerspace &#187; interrupt</title>
		<url>http://www.kwartzlab.ca/wp-content/uploads/powerpress/light_box_logo.jpg</url>
		<link>http://www.kwartzlab.ca</link>
	</image>
	<itunes:category text="Technology" />
	<itunes:category text="Arts" />
	<itunes:category text="Games &amp; Hobbies">
		<itunes:category text="Hobbies" />
	</itunes:category>
		<rawvoice:location>Kitchener, ON</rawvoice:location>
		<item>
		<title>New and Improved Arduino Multi-Threading Library</title>
		<link>http://www.kwartzlab.ca/2010/10/new-and-improved-arduino-multi/</link>
		<comments>http://www.kwartzlab.ca/2010/10/new-and-improved-arduino-multi/#comments</comments>
		<pubDate>Sat, 30 Oct 2010 15:40:48 +0000</pubDate>
		<dc:creator>Jonathan Lamothe</dc:creator>
				<category><![CDATA[Member Blogs]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[interrupt]]></category>
		<category><![CDATA[multi-threading]]></category>
		<category><![CDATA[switch]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<p>So... I've been doing a little more work on the multi-threading library <a href="http://kwartzlab.ca/blog/jlamothe/2010-09-06/arduino-multi-threading-librar">I blogged about earlier</a>, and I decided that it would be a good idea to add some interrupt-like functionality to handle switches.  Thus, the <code>SwitchInput</code> class was born.  This class has several nice features that make working with switches easier.</p>

<!--more-->

<h2>Wiring Switches to the Arduino</h2>

<p>The simplest and easiest way to connect a switch is to just wire a dry contact switch directly from the input pin to the ground pin taking advantage of the Arduino's internal 20k pull up resistor.  That way, when the switch is open, the input will read <code>HIGH</code>, and when it's closed, the input will read <code>LOW</code> (although, this is a little counter-intuitive for most people).</p>

<p><strong>NOTE:</strong> Pull-up resistors usually don't work well for pin 13 because of the built-in LED (see below).</p>

<p>The next way is to supply your own pull-up resistor by wiring the switch the same way, but also running a resistor between the 5V power output pin and the input pin.  I'm not sure why anyone would choose this option over the former, but you <strong>could</strong> if you really wanted to.  The logic for reading this switch would be the same as in the previous case.</p>

<p>The last way is to supply a pull-down resistor.  In this case, you hook your dry contact from input to the 5V supply, and your resistor from the input to the ground pin.  In this case, the logic makes more sense, where a <code>LOW</code> input level is an open switch and a <code>HIGH</code> input level is a closed switch.  This option works well with pin 13.</p>

<h2>What <code>SwitchInput</code> Provides</h2>

<p>Primarily, <code>SwitchInput</code> is intended to provide a layer of abstraction for a switch.  Instead of thinking of a switch input in terms of <code>HIGH</code> and <code>LOW</code> logic levels, it allows you to think of the switch in terms of open and closed states, which is a little easier on the brain.  The <code>SwitchInput</code> class has <code>is_open()</code> and <code>is_closed()</code> member functions that will return <code>true</code> when the switch is open or closed respectively (and <code>false</code> otherwise... obviously).</p>

<p>That's hardly the best feature of the <code>SwitchInput</code> class however.  Another handy feature of this class are the <code>on_open()</code> and <code>on_close()</code> functions.  These are virtual member functions that are automatically called when the state of the switch changes, and can be overridden to perform tasks in an interrupt-like manner when these events occur.</p>

<p>As their names would imply, the <code>on_open()</code> function is called when the state of the switch transitions from closed to open, and the <code>on_close()</code> function is called when the switch changes from from open to closed.</p>

<p>That brings us to our third feature: automatic debounce filtering.  This helps to clean up "dirty" input signals.  With most switches, when you close or open them, they don't transition cleanly from one state to another; the voltage tends to "jitter" back and forth.  Without some kind of filtering, this would cause <code>on_open()</code> and <code>on_close()</code> to be called repeatedly for no reason, which needless to say, would be undesirable.</p>

<p>This feature operates transparently.  When the voltage level of the input changes, the object will wait for a pre-determined number of milliseconds (50 by default) before doing anything.  If at the end of this time period, the level is different from what it was at the beginning, the appropriate function (<code>on_open()</code> or <code>on_close()</code>) will be called.  Otherwise, the input change will be treated as a spike and be ignored.</p>

<h2>A Note about <code>SwitchInput</code></h2>

<p>This class is derived from the <code>Thread</code> class since it needs to do logic in the background for the debouncing and edge-detection.  As such, it must be added to a <code>ThreadList</code> object (such as <code>main_thread_list</code>) just as any other <code>Thread</code> object would.  Don't expect your switch to work if you forget to do this.  ;)</p>

<h2>Source and Documentation</h2>

<p>As always, the most recent version of the <a href="http://github.com/jlamothe/mthread">source</a> is available under the <a href="http://www.gnu.org/licenses/gpl.html">GNU General Public Licence</a> on GitHub, and the <a href="http://www.jlamothe.net/projects/mthread/current">documentation</a> is available on my web site.</p>

<p>Happy Hacking.</p>

<p><strong>EDIT 2010-11-11:</strong><br />
As of version 0.4, the <code>SwitchInput</code> class now has <code>time_open()</code> and <code>time_closed()</code> member functions which will return the time for which the switch has been open or closed (in milliseconds).</p>
]]></description>
				<content:encoded><![CDATA[<p>So&#8230; I&#8217;ve been doing a little more work on the multi-threading library <a href="http://kwartzlab.ca/blog/jlamothe/2010-09-06/arduino-multi-threading-librar">I blogged about earlier</a>, and I decided that it would be a good idea to add some interrupt-like functionality to handle switches.  Thus, the <code>SwitchInput</code> class was born.  This class has several nice features that make working with switches easier.</p>
<p><span id="more-434"></span></p>
<h2>Wiring Switches to the Arduino</h2>
<p>The simplest and easiest way to connect a switch is to just wire a dry contact switch directly from the input pin to the ground pin taking advantage of the Arduino&#8217;s internal 20k pull up resistor.  That way, when the switch is open, the input will read <code>HIGH</code>, and when it&#8217;s closed, the input will read <code>LOW</code> (although, this is a little counter-intuitive for most people).</p>
<p><strong>NOTE:</strong> Pull-up resistors usually don&#8217;t work well for pin 13 because of the built-in LED (see below).</p>
<p>The next way is to supply your own pull-up resistor by wiring the switch the same way, but also running a resistor between the 5V power output pin and the input pin.  I&#8217;m not sure why anyone would choose this option over the former, but you <strong>could</strong> if you really wanted to.  The logic for reading this switch would be the same as in the previous case.</p>
<p>The last way is to supply a pull-down resistor.  In this case, you hook your dry contact from input to the 5V supply, and your resistor from the input to the ground pin.  In this case, the logic makes more sense, where a <code>LOW</code> input level is an open switch and a <code>HIGH</code> input level is a closed switch.  This option works well with pin 13.</p>
<h2>What <code>SwitchInput</code> Provides</h2>
<p>Primarily, <code>SwitchInput</code> is intended to provide a layer of abstraction for a switch.  Instead of thinking of a switch input in terms of <code>HIGH</code> and <code>LOW</code> logic levels, it allows you to think of the switch in terms of open and closed states, which is a little easier on the brain.  The <code>SwitchInput</code> class has <code>is_open()</code> and <code>is_closed()</code> member functions that will return <code>true</code> when the switch is open or closed respectively (and <code>false</code> otherwise&#8230; obviously).</p>
<p>That&#8217;s hardly the best feature of the <code>SwitchInput</code> class however.  Another handy feature of this class are the <code>on_open()</code> and <code>on_close()</code> functions.  These are virtual member functions that are automatically called when the state of the switch changes, and can be overridden to perform tasks in an interrupt-like manner when these events occur.</p>
<p>As their names would imply, the <code>on_open()</code> function is called when the state of the switch transitions from closed to open, and the <code>on_close()</code> function is called when the switch changes from from open to closed.</p>
<p>That brings us to our third feature: automatic debounce filtering.  This helps to clean up &#8220;dirty&#8221; input signals.  With most switches, when you close or open them, they don&#8217;t transition cleanly from one state to another; the voltage tends to &#8220;jitter&#8221; back and forth.  Without some kind of filtering, this would cause <code>on_open()</code> and <code>on_close()</code> to be called repeatedly for no reason, which needless to say, would be undesirable.</p>
<p>This feature operates transparently.  When the voltage level of the input changes, the object will wait for a pre-determined number of milliseconds (50 by default) before doing anything.  If at the end of this time period, the level is different from what it was at the beginning, the appropriate function (<code>on_open()</code> or <code>on_close()</code>) will be called.  Otherwise, the input change will be treated as a spike and be ignored.</p>
<h2>A Note about <code>SwitchInput</code></h2>
<p>This class is derived from the <code>Thread</code> class since it needs to do logic in the background for the debouncing and edge-detection.  As such, it must be added to a <code>ThreadList</code> object (such as <code>main_thread_list</code>) just as any other <code>Thread</code> object would.  Don&#8217;t expect your switch to work if you forget to do this.  <img src='http://www.kwartzlab.ca/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<h2>Source and Documentation</h2>
<p>As always, the most recent version of the <a href="http://github.com/jlamothe/mthread">source</a> is available under the <a href="http://www.gnu.org/licenses/gpl.html">GNU General Public Licence</a> on GitHub, and the <a href="http://www.jlamothe.net/projects/mthread/current">documentation</a> is available on my web site.</p>
<p>Happy Hacking.</p>
<p><strong>EDIT 2010-11-11:</strong><br />
As of version 0.4, the <code>SwitchInput</code> class now has <code>time_open()</code> and <code>time_closed()</code> member functions which will return the time for which the switch has been open or closed (in milliseconds).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kwartzlab.ca/2010/10/new-and-improved-arduino-multi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
