mirror of
https://github.com/taigrr/arduinolibs
synced 2025-01-18 04:33:12 -08:00
180 lines
13 KiB
HTML
180 lines
13 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: Cylon Eyes 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 Page</span></a></li>
|
|
<li class="current"><a href="pages.html"><span>Related 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"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark"> </span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark"> </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">Cylon Eyes Example </div> </div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<div class="textblock"><p>This example shows how to use the <a class="el" href="classChaseLEDs.html" title="Chase LED's on output pins in a defined sequence. ">ChaseLEDs</a> class to simulate the Cylon eye effect from Battlestar Galactica. Digital outputs are used to drive six LED's in a back and forth motion, using the following schematic:</p>
|
|
<div class="image">
|
|
<img src="Cylon.png" alt="Cylon.png"/>
|
|
</div>
|
|
<p>We start by including the <a class="el" href="classChaseLEDs.html" title="Chase LED's on output pins in a defined sequence. ">ChaseLEDs</a> class:</p>
|
|
<div class="fragment"><div class="line"><span class="preprocessor">#include <ChaseLEDs.h></span></div>
|
|
</div><!-- fragment --></p>
|
|
<p>The next step is to define the pins that the chase will run over:</p>
|
|
<div class="fragment"><div class="line">byte pins[] = {3, 5, 6, 9, 10, 11, 10, 9, 6, 5};</div>
|
|
<div class="line"><a class="code" href="classChaseLEDs.html">ChaseLEDs</a> cylonEyes(pins, <span class="keyword">sizeof</span>(pins), 100);</div>
|
|
</div><!-- fragment --></p>
|
|
<p>The chase runs from the first pin to the sixth pin and back again, with each LED lit for 100 milliseconds before moving onto the next one. To complete the example, we need to call <a class="el" href="classChaseLEDs.html#a8745fa6b9f33b6c6274a563dd4dea786">ChaseLEDs::loop()</a> each time around our main loop to cause the chase to run:</p>
|
|
<div class="fragment"><div class="line"><span class="keywordtype">void</span> loop() {</div>
|
|
<div class="line"> cylonEyes.loop();</div>
|
|
<div class="line">}</div>
|
|
</div><!-- fragment --></p>
|
|
<p>While this example uses only six pins, it can be easily extended to any number of pins by modifying the <code>pins</code> array and altering the schematic accordingly.</p>
|
|
<p>So far we are chasing only a single LED. We could change this to chase two adjacent LED's instead by defining a new <code>CylonChase</code> class that inherits from <a class="el" href="classChaseLEDs.html" title="Chase LED's on output pins in a defined sequence. ">ChaseLEDs</a>:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">class </span>CylonChase : <span class="keyword">public</span> <a class="code" href="classChaseLEDs.html">ChaseLEDs</a></div>
|
|
<div class="line">{</div>
|
|
<div class="line"><span class="keyword">public</span>:</div>
|
|
<div class="line"> CylonChase(<span class="keyword">const</span> byte *pins, <span class="keywordtype">int</span> num, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> advanceTime)</div>
|
|
<div class="line"> : <a class="code" href="classChaseLEDs.html">ChaseLEDs</a>(pins, num, advanceTime) {}</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keyword">protected</span>:</div>
|
|
<div class="line"> <span class="keywordtype">void</span> <a class="code" href="classChaseLEDs.html#aa0f4e0bd07dd65ee5574e894a612486b">advance</a>(byte prevPin, byte nextPin) {</div>
|
|
<div class="line"> digitalWrite(<a class="code" href="classChaseLEDs.html#a27c460fcb341c2dc2fcf9341616eb525">previousPin</a>(2), LOW);</div>
|
|
<div class="line"> digitalWrite(prevPin, HIGH);</div>
|
|
<div class="line"> digitalWrite(nextPin, HIGH);</div>
|
|
<div class="line"> }</div>
|
|
<div class="line">};</div>
|
|
</div><!-- fragment --></p>
|
|
<p>The important part is the implementation of the <code>advance()</code> method, which overrides <a class="el" href="classChaseLEDs.html#aa0f4e0bd07dd65ee5574e894a612486b" title="Advances to the next LED in sequence, turning off prevPin, and turning on nextPin. ">ChaseLEDs::advance()</a> to provide our own scheme for lighting the LED's each time the chase advances. We use <a class="el" href="classChaseLEDs.html#a27c460fcb341c2dc2fcf9341616eb525" title="Returns the pin that is n steps back in the sequence. ">ChaseLEDs::previousPin()</a> to get the pin that is 2 steps back in the sequence, set it to LOW, and then set the previous pin (1 step back) and the next pin to HIGH. All that remains is to change our chase initialization to use the new class:</p>
|
|
<div class="fragment"><div class="line">byte pins[] = {3, 5, 6, 9, 10, 11, 10, 9, 6, 5};</div>
|
|
<div class="line">CylonChase cylonEyes(pins, <span class="keyword">sizeof</span>(pins), 100);</div>
|
|
</div><!-- fragment --></p>
|
|
<p>We can do even better than this. Instead of fully lighting both LED's, we could instead use the PWM outputs to dim the previous pin, creating a kind of "trailing flame" effect:</p>
|
|
<div class="fragment"><div class="line"> <span class="keywordtype">void</span> advance(byte prevPin, byte nextPin) {</div>
|
|
<div class="line"> digitalWrite(previousPin(2), LOW);</div>
|
|
<div class="line"> analogWrite(prevPin, 32);</div>
|
|
<div class="line"> digitalWrite(nextPin, HIGH);</div>
|
|
<div class="line"> }</div>
|
|
</div><!-- fragment --></p>
|
|
<p>The current chase is fixed at 100 milliseconds per LED, which takes a full second to run the sequence. An alternative to hard-wiring the chase rate is to hook up a 10K potentiometer to the A0 analog input:</p>
|
|
<div class="image">
|
|
<img src="Cylon4.png" alt="Cylon4.png"/>
|
|
</div>
|
|
<p>We then modify the <code>advance()</code> method to read the new chase rate from the potentiometer each time the LED advances:</p>
|
|
<div class="fragment"><div class="line"> <span class="keywordtype">void</span> advance(byte prevPin, byte nextPin) {</div>
|
|
<div class="line"> digitalWrite(previousPin(2), LOW);</div>
|
|
<div class="line"> analogWrite(prevPin, 32);</div>
|
|
<div class="line"> digitalWrite(nextPin, HIGH);</div>
|
|
<div class="line"> setAdvanceTime(map(analogRead(A0), 0, 1023, 25, 250));</div>
|
|
<div class="line"> }</div>
|
|
</div><!-- fragment --></p>
|
|
<p>The full source code for the final version of the example follows:</p>
|
|
<div class="fragment"><div class="line"><span class="comment">/*</span></div>
|
|
<div class="line"><span class="comment">Sketch that manipulates Arduino outputs to create the "Cylon Eyes" effect from</span></div>
|
|
<div class="line"><span class="comment">Battlestar Galactica. It uses the ChaseLEDs utility class.</span></div>
|
|
<div class="line"><span class="comment"></span></div>
|
|
<div class="line"><span class="comment">This example is placed into the public domain.</span></div>
|
|
<div class="line"><span class="comment">*/</span></div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="preprocessor">#include <ChaseLEDs.h></span></div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keyword">class </span>CylonChase : <span class="keyword">public</span> <a class="code" href="classChaseLEDs.html">ChaseLEDs</a></div>
|
|
<div class="line">{</div>
|
|
<div class="line"><span class="keyword">public</span>:</div>
|
|
<div class="line"> CylonChase(<span class="keyword">const</span> byte *pins, <span class="keywordtype">int</span> num, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> advanceTime)</div>
|
|
<div class="line"> : <a class="code" href="classChaseLEDs.html">ChaseLEDs</a>(pins, num, advanceTime) {}</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keyword">protected</span>:</div>
|
|
<div class="line"> <span class="keywordtype">void</span> <a class="code" href="classChaseLEDs.html#aa0f4e0bd07dd65ee5574e894a612486b">advance</a>(byte prevPin, byte nextPin) {</div>
|
|
<div class="line"> digitalWrite(<a class="code" href="classChaseLEDs.html#a27c460fcb341c2dc2fcf9341616eb525">previousPin</a>(2), LOW);</div>
|
|
<div class="line"> analogWrite(prevPin, 32);</div>
|
|
<div class="line"> digitalWrite(nextPin, HIGH);</div>
|
|
<div class="line"> <a class="code" href="classChaseLEDs.html#af560270f72302c19fb7f95002089c9d7">setAdvanceTime</a>(map(analogRead(A0), 0, 1023, 25, 250));</div>
|
|
<div class="line"> }</div>
|
|
<div class="line">};</div>
|
|
<div class="line"></div>
|
|
<div class="line">byte pins[] = {3, 5, 6, 9, 10, 11, 10, 9, 6, 5};</div>
|
|
<div class="line">CylonChase cylonEyes(pins, <span class="keyword">sizeof</span>(pins), 100);</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keywordtype">void</span> setup() {}</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keywordtype">void</span> loop() {</div>
|
|
<div class="line"> cylonEyes.loop();</div>
|
|
<div class="line">}</div>
|
|
<div class="line"></div>
|
|
</div><!-- fragment --> </div></div><!-- contents -->
|
|
<!-- start footer part -->
|
|
<hr class="footer"/><address class="footer"><small>
|
|
Generated on Sat Mar 14 2015 15:47:30 for ArduinoLibs by  <a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="doxygen.png" alt="doxygen"/>
|
|
</a> 1.8.6
|
|
</small></address>
|
|
</body>
|
|
</html>
|