mirror of
https://github.com/taigrr/arduinolibs
synced 2025-01-18 04:33:12 -08:00
168 lines
11 KiB
HTML
168 lines
11 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: Cylon Eyes 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 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 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">Cylon Eyes Example </div> </div>
|
|
</div>
|
|
<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"><pre class="fragment"><span class="preprocessor">#include <ChaseLEDs.h></span>
|
|
</pre></div></p>
|
|
<p>The next step is to define the pins that the chase will run over:</p>
|
|
<div class="fragment"><pre class="fragment">byte pins[] = {3, 5, 6, 9, 10, 11, 10, 9, 6, 5};
|
|
<a class="code" href="classChaseLEDs.html" title="Chase LED's on output pins in a defined sequence.">ChaseLEDs</a> cylonEyes(pins, <span class="keyword">sizeof</span>(pins), 100);
|
|
</pre></div></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"><pre class="fragment"><span class="keywordtype">void</span> loop() {
|
|
cylonEyes.loop();
|
|
}
|
|
</pre></div></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"><pre class="fragment"><span class="keyword">class </span>CylonChase : <span class="keyword">public</span> <a class="code" href="classChaseLEDs.html" title="Chase LED's on output pins in a defined sequence.">ChaseLEDs</a>
|
|
{
|
|
<span class="keyword">public</span>:
|
|
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)
|
|
: <a class="code" href="classChaseLEDs.html" title="Chase LED's on output pins in a defined sequence.">ChaseLEDs</a>(pins, num, advanceTime) {}
|
|
|
|
<span class="keyword">protected</span>:
|
|
<span class="keywordtype">void</span> <a class="code" href="classChaseLEDs.html#aa0f4e0bd07dd65ee5574e894a612486b" title="Advances to the next LED in sequence, turning off prevPin, and turning on nextPin.">advance</a>(byte prevPin, byte nextPin) {
|
|
digitalWrite(<a class="code" href="classChaseLEDs.html#a27c460fcb341c2dc2fcf9341616eb525" title="Returns the pin that is n steps back in the sequence.">previousPin</a>(2), LOW);
|
|
digitalWrite(prevPin, HIGH);
|
|
digitalWrite(nextPin, HIGH);
|
|
}
|
|
};
|
|
</pre></div></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"><pre class="fragment">byte pins[] = {3, 5, 6, 9, 10, 11, 10, 9, 6, 5};
|
|
CylonChase cylonEyes(pins, <span class="keyword">sizeof</span>(pins), 100);
|
|
</pre></div></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"><pre class="fragment"> <span class="keywordtype">void</span> advance(byte prevPin, byte nextPin) {
|
|
digitalWrite(previousPin(2), LOW);
|
|
analogWrite(prevPin, 32);
|
|
digitalWrite(nextPin, HIGH);
|
|
}
|
|
</pre></div></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"><pre class="fragment"> <span class="keywordtype">void</span> advance(byte prevPin, byte nextPin) {
|
|
digitalWrite(previousPin(2), LOW);
|
|
analogWrite(prevPin, 32);
|
|
digitalWrite(nextPin, HIGH);
|
|
setAdvanceTime(map(analogRead(A0), 0, 1023, 25, 250));
|
|
}
|
|
</pre></div></p>
|
|
<p>The full source code for the final version of the example follows:</p>
|
|
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
|
|
<span class="comment">Sketch that manipulates Arduino outputs to create the "Cylon Eyes" effect from</span>
|
|
<span class="comment">Battlestar Galactica. It uses the ChaseLEDs utility class.</span>
|
|
<span class="comment"></span>
|
|
<span class="comment">This example is placed into the public domain.</span>
|
|
<span class="comment">*/</span>
|
|
|
|
<span class="preprocessor">#include <ChaseLEDs.h></span>
|
|
|
|
<span class="keyword">class </span>CylonChase : <span class="keyword">public</span> <a class="code" href="classChaseLEDs.html" title="Chase LED's on output pins in a defined sequence.">ChaseLEDs</a>
|
|
{
|
|
<span class="keyword">public</span>:
|
|
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)
|
|
: <a class="code" href="classChaseLEDs.html" title="Chase LED's on output pins in a defined sequence.">ChaseLEDs</a>(pins, num, advanceTime) {}
|
|
|
|
<span class="keyword">protected</span>:
|
|
<span class="keywordtype">void</span> <a class="code" href="classChaseLEDs.html#aa0f4e0bd07dd65ee5574e894a612486b" title="Advances to the next LED in sequence, turning off prevPin, and turning on nextPin.">advance</a>(byte prevPin, byte nextPin) {
|
|
digitalWrite(<a class="code" href="classChaseLEDs.html#a27c460fcb341c2dc2fcf9341616eb525" title="Returns the pin that is n steps back in the sequence.">previousPin</a>(2), LOW);
|
|
analogWrite(prevPin, 32);
|
|
digitalWrite(nextPin, HIGH);
|
|
<a class="code" href="classChaseLEDs.html#af560270f72302c19fb7f95002089c9d7" title="Sets the number of milliseconds to advance between LED's to advanceTime.">setAdvanceTime</a>(map(analogRead(A0), 0, 1023, 25, 250));
|
|
}
|
|
};
|
|
|
|
byte pins[] = {3, 5, 6, 9, 10, 11, 10, 9, 6, 5};
|
|
CylonChase cylonEyes(pins, <span class="keyword">sizeof</span>(pins), 100);
|
|
|
|
<span class="keywordtype">void</span> setup() {}
|
|
|
|
<span class="keywordtype">void</span> loop() {
|
|
cylonEyes.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"> </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></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 Tue Jun 12 2012 11:29:25 for ArduinoLibs by 
|
|
<a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
|
|
</body>
|
|
</html>
|