1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00
arduinolibs/blink_charlieplex.html
Rhys Weatherley 17f4fb4873 Update docs
2012-06-18 11:03:07 +10:00

157 lines
9.8 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>ArduinoLibs: Charlieplexing Example</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body onload='searchBox.OnSelectItem(0);'>
<!-- Generated by Doxygen 1.7.4 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Search');
--></script>
<div id="top">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td style="padding-left: 0.5em;">
<div id="projectname">ArduinoLibs</div>
</td>
</tr>
</tbody>
</table>
</div>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li id="searchli">
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
</div>
<div class="header">
<div class="headertitle">
<div class="title">Charlieplexing Example </div> </div>
</div>
<div class="contents">
<div class="textblock"><p><a href="http://en.wikipedia.org/wiki/Charlieplexing">Charlieplexing</a> is a technique for multiplexing large numbers of LED's on a small number of microcontroller output pins. LED's are arranged in complementary pairs and managed by the <a class="el" href="classCharlieplex.html" title="Manage an array of LED&#39;s in a charlieplexed arrangement.">Charlieplex</a> class. For this example we are going to use 3 output pins to drive 6 LED's:</p>
<div class="image">
<img src="charlieplexeg.png" alt="charlieplexeg.png"/>
</div>
<p>The technique can be expanded to even larger numbers of LED's. See the documentation for the <a class="el" href="classCharlieplex.html" title="Manage an array of LED&#39;s in a charlieplexed arrangement.">Charlieplex</a> class for a description of how to connect up larger numbers of pins in a Charlieplexed arrangement.</p>
<p>The first step is to initialize a <a class="el" href="classCharlieplex.html" title="Manage an array of LED&#39;s in a charlieplexed arrangement.">Charlieplex</a> object with the output pins it needs to drive:</p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;Charlieplex.h&gt;</span>
byte pins[3] = {9, 10, 11};
<a class="code" href="classCharlieplex.html" title="Manage an array of LED&#39;s in a charlieplexed arrangement.">Charlieplex</a> charlie(pins, <span class="keyword">sizeof</span>(pins));
</pre></div></p>
<p>Then in <code>setup()</code> we use <a class="el" href="classCharlieplex.html#ab103c9687a0890faf72e4da79e3de0a5" title="Sets the value of the LED at index in the charliplexed array.">Charlieplex::setLed()</a> and <a class="el" href="classCharlieplex.html#a605a302e13005a1aa3d68d0e22bc474b" title="Sets the PWM value of the LED at index in the charliplexed array; between 0 and 255.">Charlieplex::setPwmLed()</a> to set three of the six LED's to the desired output values:</p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> setup() {
charlie.setLed(0, <span class="keyword">true</span>); <span class="comment">// Turn on LED1</span>
charlie.setLed(3, <span class="keyword">true</span>); <span class="comment">// Turn on LED4</span>
charlie.setPwmLed(5, 64); <span class="comment">// Set LED6 to one-quarter on</span>
}
</pre></div></p>
<p>Charlieplexing can only light a single LED at a time. It is therefore necessary to constantly scan the entire LED array, alternatively turning LED's on and off. The user's peristence of vision fills in the gaps. To do this, we call <a class="el" href="classCharlieplex.html#a8313edeacd8387c428b8299d52584d6a" title="Runs the multiplexing loop, to display the LED states on the charlieplexed array.">Charlieplex::loop()</a>:</p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> loop() {
charlie.loop();
}
</pre></div></p>
<p>The downside of Charlieplexing is that when multiple LED's are lit, each LED will appear to be dimmer than if only a single LED was lit. This can be counteracted by using brighter LED's or smaller resistors. The danger with smaller resistors is that if the program crashes or locks up for some reason, a large amount of continuous current could be fed through a single LED and cause it to exceed its maximum rating and burn out.</p>
<p>The full source code for the example follows:</p>
<div class="fragment"><pre class="fragment"><span class="comment">/* This example is placed into the public domain */</span>
<span class="preprocessor">#include &lt;Charlieplex.h&gt;</span>
byte pins[3] = {9, 10, 11};
<a class="code" href="classCharlieplex.html" title="Manage an array of LED&#39;s in a charlieplexed arrangement.">Charlieplex</a> charlie(pins, <span class="keyword">sizeof</span>(pins));
<span class="keywordtype">void</span> setup() {
charlie.setLed(0, <span class="keyword">true</span>); <span class="comment">// Turn on LED1</span>
charlie.setLed(3, <span class="keyword">true</span>); <span class="comment">// Turn on LED4</span>
charlie.setPwmLed(5, 64); <span class="comment">// Set LED6 to one-quarter on</span>
}
<span class="keywordtype">void</span> loop() {
charlie.loop();
}
</pre></div><p>A more complex example that performs a LED chase over the 6 LED's follows:</p>
<div class="fragment"><pre class="fragment"><span class="comment">/* This example is placed into the public domain */</span>
<span class="preprocessor">#include &lt;Charlieplex.h&gt;</span>
byte pins[3] = {9, 10, 11};
<a class="code" href="classCharlieplex.html" title="Manage an array of LED&#39;s in a charlieplexed arrangement.">Charlieplex</a> charlie(pins, <span class="keyword">sizeof</span>(pins));
<span class="keywordtype">int</span> previous = 1;
<span class="keywordtype">int</span> current = 0;
<span class="keywordtype">int</span> step = 1;
<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> lastTime;
<span class="keywordtype">void</span> setup() {
lastTime = millis();
charlie.setLed(current, <span class="keyword">true</span>);
charlie.setPwmLed(previous, 64);
}
<span class="keywordtype">void</span> loop() {
<span class="keywordflow">if</span> ((millis() - lastTime) &gt;= 100) {
charlie.setLed(previous, <span class="keyword">false</span>);
charlie.setPwmLed(current, 64);
previous = current;
current += step;
<span class="keywordflow">if</span> (current &lt; 0) {
current = 1;
step = 1;
} <span class="keywordflow">else</span> <span class="keywordflow">if</span> (current &gt;= charlie.count()) {
current = charlie.count() - 2;
step = -1;
}
charlie.setLed(current, <span class="keyword">true</span>);
lastTime += 100;
}
charlie.loop();
}
</pre></div> </div></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Mon Jun 18 2012 11:02:52 for ArduinoLibs by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
</body>
</html>