mirror of
https://github.com/taigrr/arduinolibs
synced 2025-01-18 04:33:12 -08:00
246 lines
21 KiB
HTML
246 lines
21 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: Star Trek 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>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark"> </span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><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">Star Trek Example </div> </div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<div class="textblock"><p>This example shows how to use the <a class="el" href="classBlinkLED.html" title="Blink a LED on a digital output pin. ">BlinkLED</a> and <a class="el" href="classChaseLEDs.html" title="Chase LED's on output pins in a defined sequence. ">ChaseLEDs</a> classes to simulate the running lights on the starship Enterprise from Star Trek. This can be used as the basis for lighting a model kit. It is recommended that you read the <a class="el" href="blink_blink.html">Blink</a> and <a class="el" href="blink_cylon.html">Cylon</a> tutorials first.</p>
|
|
<p>There are four categories of lights on the Enterprise:</p>
|
|
<ul>
|
|
<li>Static lights in windows, engines, and the deflector dish. We don't handle those in this example as we assume that they are connected directly to the power supply with no computer control. </li>
|
|
<li>Red and green navigation lights on the left and right of the saucer, and on the left and right warp nacelles, typically with a 1 second period. The red light is on the left as viewed from the back of the model. </li>
|
|
<li>White strobe light behind the bridge and on the warp nacelles that comes on briefly every second. </li>
|
|
<li>Nacelle lights that perform a circular LED chase in the front of the nacelles to create the warp engine twirl effect.</li>
|
|
</ul>
|
|
<p>Different models of the Enterprise have the lights in different places, and the period of flashing can vary from TV show to show, and sometimes from episode to episode. There isn't a definitive set of blink timings or number of LED's in the nacelle chase. The sketch has a number of configurable parameters that gives the user the freedom to choose which show and/or episode they wish to treat as "canonical" for their model.</p>
|
|
<p>We start by building a test circuit with a small number of LED's for each of the three categories (navigation, strobe, and nacelles):</p>
|
|
<div class="image">
|
|
<img src="StarTrek.png" alt="StarTrek.png"/>
|
|
</div>
|
|
<p>This won't be the final circuit for the model, but building it on a breadboard will help with the initial prototyping stages and choosing the appropriate blink timings:</p>
|
|
<div class="image">
|
|
<img src="StarTrekBreadboard.png" alt="StarTrekBreadboard.png"/>
|
|
</div>
|
|
<p>Alternatively, the test circuit can be built on a prototyping shield with the chase LED's in a circular arrangement to simulate how they will look when placed in the front of the model's warp nacelles:</p>
|
|
<div class="image">
|
|
<img src="StarTrekShield.png" alt="StarTrekShield.png"/>
|
|
</div>
|
|
<p>Now that we have a circuit, let's configure the red navigation LED on AOUT2 using the <a class="el" href="classBlinkLED.html" title="Blink a LED on a digital output pin. ">BlinkLED</a> class, to blink with a period of 1000 milliseconds on, 1000 milliseconds off:</p>
|
|
<div class="fragment"><div class="line"><span class="preprocessor">#include <BlinkLED.h></span></div>
|
|
<div class="line"><span class="preprocessor">#define NAV_LIGHTS A2 // Output pin for controlling the navigation lights</span></div>
|
|
<div class="line"><span class="preprocessor">#define NAV_LIGHTS_ON 1000 // Time the navigation lights are on (milliseconds)</span></div>
|
|
<div class="line"><span class="preprocessor">#define NAV_LIGHTS_OFF 1000 // Time the navigation lights are off (milliseconds)</span></div>
|
|
<div class="line"><a class="code" href="classBlinkLED.html">BlinkLED</a> navLights(NAV_LIGHTS, NAV_LIGHTS_ON, NAV_LIGHTS_OFF);</div>
|
|
</div><!-- fragment --></p>
|
|
<p>We repeat the process for the strobe LED on AOUT3, with a period of 70 milliseconds on, and 830 milliseconds off:</p>
|
|
<div class="fragment"><div class="line"><span class="preprocessor">#define STROBE_LIGHT A3 // Output pin for controlling the strobe</span></div>
|
|
<div class="line"><span class="preprocessor">#define STROBE_LIGHT_ON 70 // Time the strobe light is on (milliseconds)</span></div>
|
|
<div class="line"><span class="preprocessor">#define STROBE_LIGHT_OFF 830 // Time the strobe light is off (milliseconds)</span></div>
|
|
<div class="line"><a class="code" href="classBlinkLED.html">BlinkLED</a> strobeLight(STROBE_LIGHT, STROBE_LIGHT_ON, STROBE_LIGHT_OFF);</div>
|
|
</div><!-- fragment --></p>
|
|
<p>We also need to arrange for <a class="el" href="classBlinkLED.html#aeeaf42b94c5392935f00f0f12a58c75e">BlinkLED::loop()</a> to be called from the application's main <code>loop()</code> function:</p>
|
|
<div class="fragment"><div class="line"><span class="keywordtype">void</span> loop() {</div>
|
|
<div class="line"> navLights.loop();</div>
|
|
<div class="line"> strobeLight.loop();</div>
|
|
<div class="line">}</div>
|
|
</div><!-- fragment --><p>If you run the sketch at this point, you should see the navigation and strobe LED's blink with the selected rates.</p>
|
|
<p>Next is the twirl effect in the warp nacelles, using the <a class="el" href="classChaseLEDs.html" title="Chase LED's on output pins in a defined sequence. ">ChaseLEDs</a> class. We are actually going to inherit from <a class="el" href="classChaseLEDs.html" title="Chase LED's on output pins in a defined sequence. ">ChaseLEDs</a> to create a custom LED chaser that reads the chase rate from AIN0 and uses PWM outputs to create a trailing flame effect. See the <a class="el" href="blink_cylon.html">Cylon</a> example for more information on creating custom effects with <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="preprocessor">#define NACELLE_CHASE_LEN 6 // Length of nacelle chase, 1..6</span></div>
|
|
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define NACELLE_MIN_PERIOD 25 // Minimum time to advance the nacelle chase (milliseconds)</span></div>
|
|
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define NACELLE_MAX_PERIOD 250 // Maximum time to advance the nacelle chase (milliseconds)</span></div>
|
|
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define NACELLE_DIM_VALUE 32 // Value for dimming previous LED in chase, 0..255</span></div>
|
|
<div class="line"><span class="preprocessor"></span></div>
|
|
<div class="line"><span class="comment">// Output pins to use for the nacelle chase</span></div>
|
|
<div class="line">byte nacelleChasePins[6] = {3, 5, 6, 9, 10, 11};</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keyword">class </span>NacelleChaseLEDs : <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"> NacelleChaseLEDs(<span class="keyword">const</span> byte *pins, <span class="keywordtype">int</span> num)</div>
|
|
<div class="line"> : <a class="code" href="classChaseLEDs.html">ChaseLEDs</a>(pins, num, 0) {}</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, NACELLE_DIM_VALUE);</div>
|
|
<div class="line"> digitalWrite(nextPin, HIGH);</div>
|
|
<div class="line"> <a class="code" href="classChaseLEDs.html#af560270f72302c19fb7f95002089c9d7">setAdvanceTime</a>(map(analogRead(NACELLE_RATE), 0, 1023, NACELLE_MIN_PERIOD, NACELLE_MAX_PERIOD));</div>
|
|
<div class="line"> }</div>
|
|
<div class="line">};</div>
|
|
<div class="line"></div>
|
|
<div class="line">NacelleChaseLEDs nacelleChase(nacelleChasePins, NACELLE_CHASE_LEN);</div>
|
|
</div><!-- fragment --></p>
|
|
<p>We also need to add a call to <a class="el" href="classChaseLEDs.html#a8745fa6b9f33b6c6274a563dd4dea786">ChaseLEDs::loop()</a> to the application's main loop:</p>
|
|
<div class="fragment"><div class="line"><span class="keywordtype">void</span> loop() {</div>
|
|
<div class="line"> navLights.loop();</div>
|
|
<div class="line"> strobeLight.loop();</div>
|
|
<div class="line"> nacelleChase.loop();</div>
|
|
<div class="line">}</div>
|
|
</div><!-- fragment --></p>
|
|
<p>Running the sketch now should cause the six LED's in the nacelle sequence to chase, in addition to the navigation and strobe LED's. The 10K potentiometer can be used to select the desired chase rate. This completes the test circuit, and will allow you to fiddle with the blink timings and chase rate until you are happy with the result.</p>
|
|
<p>We've made provision in this sketch for six outputs in the chase, but some models may only use three or five. The <code>NACELLE_CHASE_LEN</code> parameter controls the length of the chase.</p>
|
|
<p>With three outputs, the LED's can be arranged in opposite pairs, lighting two LED's at a time. The following circuit demonstrates how three outputs can be used to drive six LED's:</p>
|
|
<div class="image">
|
|
<img src="ThreeChase.png" alt="ThreeChase.png"/>
|
|
</div>
|
|
<p>You will need two of these circuits, for the left and right warp nacelles. The transistor drivers reduce the current load on the Arduino CPU and provide the option to drive the LED's from 12V instead of 5V.</p>
|
|
<p>It is recommended that you use transistor drivers for the navigation and strobe lights as well as there will be multiple LED's on each output in a real model. For example, there will be at least three each of the red and green navigation lights: the top of the saucer section, the bottom of the saucer section, and the top of the warp nacelle. Using a 12V supply will make it easier to string lots of LED's together in series.</p>
|
|
<p>Other nacelle effects are possible by modifying the <code>advance()</code> method in the sketch. For example, the "opposite pairs" effect with 3 outputs can also be done with 6 outputs and the following modification to the sketch:</p>
|
|
<div class="fragment"><div class="line"> <span class="keywordtype">void</span> advance(byte prevPin, byte nextPin) {</div>
|
|
<div class="line"> digitalWrite(previousPin(5), LOW);</div>
|
|
<div class="line"> analogWrite(previousPin(4), NACELLE_DIM_VALUE);</div>
|
|
<div class="line"> digitalWrite(previousPin(3), HIGH);</div>
|
|
<div class="line"> digitalWrite(previousPin(2), LOW);</div>
|
|
<div class="line"> analogWrite(prevPin, NACELLE_DIM_VALUE);</div>
|
|
<div class="line"> digitalWrite(nextPin, HIGH);</div>
|
|
<div class="line"> setAdvanceTime(map(analogRead(NACELLE_RATE), 0, 1023, NACELLE_MIN_PERIOD, NACELLE_MAX_PERIOD));</div>
|
|
<div class="line"> }</div>
|
|
</div><!-- fragment --></p>
|
|
<p>The full source code for the example, including the "opposite pairs" effect, 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 Star Trek Enterprise style</span></div>
|
|
<div class="line"><span class="comment">running lights and LED chasers.</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 <BlinkLED.h></span></div>
|
|
<div class="line"><span class="preprocessor">#include <ChaseLEDs.h></span></div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="preprocessor">#define NACELLE_RATE A0 // Analog input for reading the nacelle chase rate</span></div>
|
|
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define NAV_LIGHTS A2 // Output pin for controlling the navigation lights</span></div>
|
|
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define STROBE_LIGHT A3 // Output pin for controlling the strobe</span></div>
|
|
<div class="line"><span class="preprocessor"></span></div>
|
|
<div class="line"><span class="comment">// Configurable parameters.</span></div>
|
|
<div class="line"><span class="preprocessor">#define NAV_LIGHTS_ON 1000 // Time the navigation lights are on (milliseconds)</span></div>
|
|
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define NAV_LIGHTS_OFF 1000 // Time the navigation lights are off (milliseconds)</span></div>
|
|
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define STROBE_LIGHT_ON 70 // Time the strobe light is on (milliseconds)</span></div>
|
|
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define STROBE_LIGHT_OFF 830 // Time the strobe light is off (milliseconds)</span></div>
|
|
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define NACELLE_CHASE_LEN 6 // Length of nacelle chase, 1..6</span></div>
|
|
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define NACELLE_MIN_PERIOD 25 // Minimum time to advance the nacelle chase (milliseconds)</span></div>
|
|
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define NACELLE_MAX_PERIOD 250 // Maximum time to advance the nacelle chase (milliseconds)</span></div>
|
|
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define NACELLE_DIM_VALUE 32 // Value for dimming previous LED in chase, 0..255</span></div>
|
|
<div class="line"><span class="preprocessor"></span></div>
|
|
<div class="line"><span class="comment">// Output pins to use for the nacelle chase</span></div>
|
|
<div class="line">byte nacelleChasePins[6] = {3, 5, 6, 9, 10, 11};</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keyword">class </span>NacelleChaseLEDs : <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"> NacelleChaseLEDs(<span class="keyword">const</span> byte *pins, <span class="keywordtype">int</span> num)</div>
|
|
<div class="line"> : <a class="code" href="classChaseLEDs.html">ChaseLEDs</a>(pins, num, 0) {}</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>(5), LOW);</div>
|
|
<div class="line"> analogWrite(<a class="code" href="classChaseLEDs.html#a27c460fcb341c2dc2fcf9341616eb525">previousPin</a>(4), NACELLE_DIM_VALUE);</div>
|
|
<div class="line"> digitalWrite(<a class="code" href="classChaseLEDs.html#a27c460fcb341c2dc2fcf9341616eb525">previousPin</a>(3), HIGH);</div>
|
|
<div class="line"> digitalWrite(<a class="code" href="classChaseLEDs.html#a27c460fcb341c2dc2fcf9341616eb525">previousPin</a>(2), LOW);</div>
|
|
<div class="line"> analogWrite(prevPin, NACELLE_DIM_VALUE);</div>
|
|
<div class="line"> digitalWrite(nextPin, HIGH);</div>
|
|
<div class="line"> <a class="code" href="classChaseLEDs.html#af560270f72302c19fb7f95002089c9d7">setAdvanceTime</a>(map(analogRead(NACELLE_RATE), 0, 1023, NACELLE_MIN_PERIOD, NACELLE_MAX_PERIOD));</div>
|
|
<div class="line"> }</div>
|
|
<div class="line">};</div>
|
|
<div class="line"></div>
|
|
<div class="line">NacelleChaseLEDs nacelleChase(nacelleChasePins, NACELLE_CHASE_LEN);</div>
|
|
<div class="line"></div>
|
|
<div class="line"><a class="code" href="classBlinkLED.html">BlinkLED</a> navLights(NAV_LIGHTS, NAV_LIGHTS_ON, NAV_LIGHTS_OFF);</div>
|
|
<div class="line"><a class="code" href="classBlinkLED.html">BlinkLED</a> strobeLight(STROBE_LIGHT, STROBE_LIGHT_ON, STROBE_LIGHT_OFF);</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keywordtype">void</span> setup() {</div>
|
|
<div class="line"> <span class="comment">// Turn off the status LED on the Arduino board (we don't need it).</span></div>
|
|
<div class="line"> pinMode(13, OUTPUT);</div>
|
|
<div class="line"> digitalWrite(13, LOW);</div>
|
|
<div class="line">}</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keywordtype">void</span> loop() {</div>
|
|
<div class="line"> navLights.loop();</div>
|
|
<div class="line"> strobeLight.loop();</div>
|
|
<div class="line"> nacelleChase.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 Apr 7 2018 02:43:13 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>
|