1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00
arduinolibs/blink_charlieplex.html
Rhys Weatherley 100a18a956 Update docs
2015-03-29 08:32:07 +10:00

169 lines
12 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"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.6"/>
<title>ArduinoLibs: Charlieplexing Example</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<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>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.6 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<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>
<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>
<!-- 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><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Pages</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>
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">Charlieplexing Example </div> </div>
</div><!--header-->
<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"><div class="line"><span class="preprocessor">#include &lt;Charlieplex.h&gt;</span></div>
<div class="line"></div>
<div class="line">byte pins[3] = {9, 10, 11};</div>
<div class="line"><a class="code" href="classCharlieplex.html">Charlieplex</a> charlie(pins, <span class="keyword">sizeof</span>(pins));</div>
</div><!-- fragment --></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"><div class="line"><span class="keywordtype">void</span> setup() {</div>
<div class="line"> charlie.setLed(0, <span class="keyword">true</span>); <span class="comment">// Turn on LED1</span></div>
<div class="line"> charlie.setLed(3, <span class="keyword">true</span>); <span class="comment">// Turn on LED4</span></div>
<div class="line"> charlie.setPwmLed(5, 64); <span class="comment">// Set LED6 to one-quarter on</span></div>
<div class="line">}</div>
</div><!-- fragment --></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"><div class="line"><span class="keywordtype">void</span> loop() {</div>
<div class="line"> charlie.loop();</div>
<div class="line">}</div>
</div><!-- fragment --></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"><div class="line"><span class="comment">/* This example is placed into the public domain */</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;Charlieplex.h&gt;</span></div>
<div class="line"></div>
<div class="line">byte pins[3] = {9, 10, 11};</div>
<div class="line"><a class="code" href="classCharlieplex.html">Charlieplex</a> charlie(pins, <span class="keyword">sizeof</span>(pins));</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> setup() {</div>
<div class="line"> charlie.setLed(0, <span class="keyword">true</span>); <span class="comment">// Turn on LED1</span></div>
<div class="line"> charlie.setLed(3, <span class="keyword">true</span>); <span class="comment">// Turn on LED4</span></div>
<div class="line"> charlie.setPwmLed(5, 64); <span class="comment">// Set LED6 to one-quarter on</span></div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> loop() {</div>
<div class="line"> charlie.loop();</div>
<div class="line">}</div>
</div><!-- fragment --><p>A more complex example that performs a LED chase over the 6 LED's follows:</p>
<div class="fragment"><div class="line"><span class="comment">/* This example is placed into the public domain */</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;Charlieplex.h&gt;</span></div>
<div class="line"></div>
<div class="line">byte pins[3] = {9, 10, 11};</div>
<div class="line"><a class="code" href="classCharlieplex.html">Charlieplex</a> charlie(pins, <span class="keyword">sizeof</span>(pins));</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> previous = 1;</div>
<div class="line"><span class="keywordtype">int</span> current = 0;</div>
<div class="line"><span class="keywordtype">int</span> step = 1;</div>
<div class="line"><span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> lastTime;</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> setup() {</div>
<div class="line"> lastTime = millis();</div>
<div class="line"> charlie.setLed(current, <span class="keyword">true</span>);</div>
<div class="line"> charlie.setPwmLed(previous, 64);</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> loop() {</div>
<div class="line"> <span class="keywordflow">if</span> ((millis() - lastTime) &gt;= 100) {</div>
<div class="line"> charlie.setLed(previous, <span class="keyword">false</span>);</div>
<div class="line"> charlie.setPwmLed(current, 64);</div>
<div class="line"> previous = current;</div>
<div class="line"> current += step;</div>
<div class="line"> <span class="keywordflow">if</span> (current &lt; 0) {</div>
<div class="line"> current = 1;</div>
<div class="line"> step = 1;</div>
<div class="line"> } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (current &gt;= charlie.count()) {</div>
<div class="line"> current = charlie.count() - 2;</div>
<div class="line"> step = -1;</div>
<div class="line"> }</div>
<div class="line"> charlie.setLed(current, <span class="keyword">true</span>);</div>
<div class="line"> lastTime += 100;</div>
<div class="line"> }</div>
<div class="line"> charlie.loop();</div>
<div class="line">}</div>
</div><!-- fragment --> </div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Sun Mar 29 2015 08:30:13 for ArduinoLibs by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.6
</small></address>
</body>
</html>