index.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  6. <meta name="generator" content="Doxygen 1.8.17"/>
  7. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  8. <title>Bounce2: BOUNCE 2</title>
  9. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  10. <script type="text/javascript" src="jquery.js"></script>
  11. <script type="text/javascript" src="dynsections.js"></script>
  12. <link href="search/search.css" rel="stylesheet" type="text/css"/>
  13. <script type="text/javascript" src="search/searchdata.js"></script>
  14. <script type="text/javascript" src="search/search.js"></script>
  15. <link href="doxygen.css" rel="stylesheet" type="text/css" />
  16. </head>
  17. <body>
  18. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  19. <div id="titlearea">
  20. <table cellspacing="0" cellpadding="0">
  21. <tbody>
  22. <tr style="height: 56px;">
  23. <td id="projectalign" style="padding-left: 0.5em;">
  24. <div id="projectname">Bounce2
  25. </div>
  26. </td>
  27. </tr>
  28. </tbody>
  29. </table>
  30. </div>
  31. <!-- end header part -->
  32. <!-- Generated by Doxygen 1.8.17 -->
  33. <script type="text/javascript">
  34. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  35. var searchBox = new SearchBox("searchBox", "search",false,'Search');
  36. /* @license-end */
  37. </script>
  38. <script type="text/javascript" src="menudata.js"></script>
  39. <script type="text/javascript" src="menu.js"></script>
  40. <script type="text/javascript">
  41. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  42. $(function() {
  43. initMenu('',true,false,'search.php','Search');
  44. $(document).ready(function() { init_search(); });
  45. });
  46. /* @license-end */</script>
  47. <div id="main-nav"></div>
  48. </div><!-- top -->
  49. <!-- window showing the filter options -->
  50. <div id="MSearchSelectWindow"
  51. onmouseover="return searchBox.OnSearchSelectShow()"
  52. onmouseout="return searchBox.OnSearchSelectHide()"
  53. onkeydown="return searchBox.OnSearchSelectKey(event)">
  54. </div>
  55. <!-- iframe showing the search results (closed by default) -->
  56. <div id="MSearchResultsWindow">
  57. <iframe src="javascript:void(0)" frameborder="0"
  58. name="MSearchResults" id="MSearchResults">
  59. </iframe>
  60. </div>
  61. <div class="PageDoc"><div class="header">
  62. <div class="headertitle">
  63. <div class="title">BOUNCE 2 </div> </div>
  64. </div><!--header-->
  65. <div class="contents">
  66. <div class="textblock"><p>Debouncing library for Arduino and Wiring by Thomas Ouellet Fredericks with many contributions from the community : <a href="https://github.com/thomasfredericks/Bounce2/graphs/contributors">https://github.com/thomasfredericks/Bounce2/graphs/contributors</a></p>
  67. <p>Basically, the mechanical part of buttons and switches vibrate slightly when closed or opened causing multiple undesired false states (similar to noise). This library filters out these undesired state changes. More about debouncing:</p><ul>
  68. <li>John Errington's Experiments with an Arduino : <a href="http://www.skillbank.co.uk/arduino/switchbounce.htm">Using digital inputs: Switch bounce and solutions to it</a></li>
  69. <li>Wikipedia article : <a href="http://en.wikipedia.org/wiki/Debounce#Contact_bounce">http://en.wikipedia.org/wiki/Debounce#Contact_bounce</a></li>
  70. </ul>
  71. <p>See the bottom of this page for a basic usage example and the "examples" folder for more.</p>
  72. <p>The library is composed of three classes:</p><ul>
  73. <li><a class="el" href="class_debouncer.html" title="The Debouce class. Just the deboucing code separated from all harware.">Debouncer</a> : The code that does the actual debouncing. Only advanced users should play with this class.</li>
  74. <li><a class="el" href="class_bounce.html" title="The Debouncer:Bounce class. Links the Deboucing class to a hardware pin. This class is odly named,...">Bounce</a> : This is the general use library. It links the <a class="el" href="class_debouncer.html" title="The Debouce class. Just the deboucing code separated from all harware.">Debouncer</a> to a hardware pin on your board.</li>
  75. <li>Button : A special version of <a class="el" href="class_bounce.html" title="The Debouncer:Bounce class. Links the Deboucing class to a hardware pin. This class is odly named,...">Bounce</a> for buttons that are pressed.</li>
  76. </ul>
  77. <h1>INSTALLATION &amp; DOWNLOAD</h1>
  78. <p>Install through your software's Library Manager or download the latest version <a href="https://github.com/thomasfredericks/Bounce2/archive/master.zip">here</a> and put the "Bounce2" folder in your "libraries" folder.</p>
  79. <p>Please note that the original version of this library (<a class="el" href="class_bounce.html" title="The Debouncer:Bounce class. Links the Deboucing class to a hardware pin. This class is odly named,...">Bounce</a> 1) is included in the "extras" folder of the download but not supported anymore.</p>
  80. <h2>BASIC USE</h2>
  81. <h3>INSTANTIATE</h3>
  82. <div class="fragment"><div class="line"><span class="preprocessor">#include &lt;Bounce2.h&gt;</span></div>
  83. <div class="line"><a class="code" href="class_bounce.html">Bounce</a> b = <a class="code" href="class_bounce.html">Bounce</a>(); <span class="comment">// Instantiate a Bounce object</span></div>
  84. </div><!-- fragment --><h3>SETUP</h3>
  85. <div class="fragment"><div class="line">b.<a class="code" href="class_bounce.html#aba08e592941465d033e3eba3dde66eaf">attach</a> ( &lt;PIN&gt; , &lt;PIN MODE&gt; );</div>
  86. <div class="line">b.<a class="code" href="class_debouncer.html#a930bf3945e698d77b889f6309079857d">interval</a>( &lt;INTERVAL IN MS&gt; );</div>
  87. </div><!-- fragment --><h3>LOOP</h3>
  88. <div class="fragment"><div class="line">b.<a class="code" href="class_debouncer.html#a72f3e8d483555031d2ac21b0b7702c06">update</a>();</div>
  89. <div class="line"><span class="keywordflow">if</span> ( b.<a class="code" href="class_debouncer.html#a1bbd627f9d8809b5ee9e64e9fb21e248">changed</a>() ) { </div>
  90. <div class="line"> <span class="comment">// THE STATE OF THE INPUT CHANGED</span></div>
  91. <div class="line"> <span class="keywordtype">int</span> deboucedValue = b.<a class="code" href="class_debouncer.html#aa5fcde9156f700c362fd49af5dd92ecd">read</a>();</div>
  92. <div class="line"> <span class="comment">// DO SOMETHING WITH THE VALUE</span></div>
  93. <div class="line">}</div>
  94. </div><!-- fragment --><h2>BOUNCE EXAMPLE</h2>
  95. <div class="fragment"><div class="line"><span class="comment">// This example toggles the debug LED (pin 13) on or off when a button on pin 2 is pressed.</span></div>
  96. <div class="line"> </div>
  97. <div class="line"><span class="comment">// Include the Bounce2 library found here :</span></div>
  98. <div class="line"><span class="comment">// https://github.com/thomasfredericks/Bounce2</span></div>
  99. <div class="line"><span class="preprocessor">#include &lt;Bounce2.h&gt;</span></div>
  100. <div class="line"> </div>
  101. <div class="line"><span class="preprocessor">#define BUTTON_PIN 2</span></div>
  102. <div class="line"><span class="preprocessor">#define LED_PIN 13</span></div>
  103. <div class="line"> </div>
  104. <div class="line"><span class="keywordtype">int</span> ledState = LOW;</div>
  105. <div class="line"> </div>
  106. <div class="line"> </div>
  107. <div class="line"><a class="code" href="class_bounce.html">Bounce</a> b = <a class="code" href="class_bounce.html">Bounce</a>(); <span class="comment">// Instantiate a Bounce object</span></div>
  108. <div class="line"> </div>
  109. <div class="line"><span class="keywordtype">void</span> setup() {</div>
  110. <div class="line"> </div>
  111. <div class="line"> b.<a class="code" href="class_bounce.html#aba08e592941465d033e3eba3dde66eaf">attach</a>(BUTTON_PIN,INPUT_PULLUP); <span class="comment">// Attach the debouncer to a pin with INPUT_PULLUP mode</span></div>
  112. <div class="line"> b.<a class="code" href="class_debouncer.html#a930bf3945e698d77b889f6309079857d">interval</a>(25); <span class="comment">// Use a debounce interval of 25 milliseconds</span></div>
  113. <div class="line"> </div>
  114. <div class="line"> </div>
  115. <div class="line"> pinMode(LED_PIN,OUTPUT); <span class="comment">// Setup the LED</span></div>
  116. <div class="line"> digitalWrite(LED_PIN,ledState); <span class="comment">// Turn off the LED</span></div>
  117. <div class="line"> </div>
  118. <div class="line">}</div>
  119. <div class="line"> </div>
  120. <div class="line"><span class="keywordtype">void</span> loop() {</div>
  121. <div class="line"> </div>
  122. <div class="line"> b.<a class="code" href="class_debouncer.html#a72f3e8d483555031d2ac21b0b7702c06">update</a>(); <span class="comment">// Update the Bounce instance</span></div>
  123. <div class="line"> </div>
  124. <div class="line"> <span class="keywordflow">if</span> ( b.<a class="code" href="class_debouncer.html#a80315de311b855f9787b4b6d5b7899e0">fell</a>() ) { <span class="comment">// Call code if button transitions from HIGH to LOW</span></div>
  125. <div class="line"> ledState = !ledState; <span class="comment">// Toggle LED state</span></div>
  126. <div class="line"> digitalWrite(LED_PIN,ledState); <span class="comment">// Apply new LED state</span></div>
  127. <div class="line"> }</div>
  128. <div class="line">}</div>
  129. </div><!-- fragment --><h2>BUTTON EXAMPLE</h2>
  130. <div class="fragment"><div class="line"><span class="comment">/* </span></div>
  131. <div class="line"><span class="comment"> DESCRIPTION</span></div>
  132. <div class="line"><span class="comment"> ====================</span></div>
  133. <div class="line"><span class="comment"> This is an example of the Bounce2::Button class. </span></div>
  134. <div class="line"><span class="comment"> When the user presses a physical button, it toggles a LED on or off.</span></div>
  135. <div class="line"><span class="comment"> The Button class matches an electrical state to a physical action. </span></div>
  136. <div class="line"><span class="comment"> Use .setPressedState(LOW or HIGH) to set the detection state for when the button is pressed.</span></div>
  137. <div class="line"><span class="comment"></span> </div>
  138. <div class="line"><span class="comment"> INSTRUCTIONS</span></div>
  139. <div class="line"><span class="comment"> ====================</span></div>
  140. <div class="line"><span class="comment"> Set BUTTON_PIN to the pin attached to the button.</span></div>
  141. <div class="line"><span class="comment"> Set LED_PIN to the pin attached to a LED.</span></div>
  142. <div class="line"><span class="comment"> </span></div>
  143. <div class="line"><span class="comment"> */</span></div>
  144. <div class="line"> </div>
  145. <div class="line"><span class="comment">// Include the Bounce2 library found here :</span></div>
  146. <div class="line"><span class="comment">// https://github.com/thomasfredericks/Bounce2</span></div>
  147. <div class="line"><span class="preprocessor">#include &lt;Bounce2.h&gt;</span></div>
  148. <div class="line"> </div>
  149. <div class="line"><span class="comment">// INSTANTIATE A Button OBJECT</span></div>
  150. <div class="line"><a class="code" href="class_bounce2_1_1_button.html">Bounce2::Button</a> button = <a class="code" href="class_bounce2_1_1_button.html">Bounce2::Button</a>();</div>
  151. <div class="line"> </div>
  152. <div class="line"><span class="comment">// WE WILL attach() THE BUTTON TO THE FOLLOWING PIN IN setup()</span></div>
  153. <div class="line"><span class="preprocessor">#define BUTTON_PIN 39 </span></div>
  154. <div class="line"> </div>
  155. <div class="line"><span class="comment">// DEFINE THE PIN FOR THE LED :</span></div>
  156. <div class="line"><span class="comment">// 1) SOME BOARDS HAVE A DEFAULT LED (LED_BUILTIN)</span></div>
  157. <div class="line"><span class="comment">//#define LED_PIN LED_BUILTIN</span></div>
  158. <div class="line"><span class="comment">// 2) OTHERWISE SET YOUR OWN PIN</span></div>
  159. <div class="line"><span class="preprocessor">#define LED_PIN 13</span></div>
  160. <div class="line"> </div>
  161. <div class="line"><span class="comment">// SET A VARIABLE TO STORE THE LED STATE</span></div>
  162. <div class="line"><span class="keywordtype">bool</span> ledState = LOW;</div>
  163. <div class="line"> </div>
  164. <div class="line"><span class="keywordtype">void</span> setup() {</div>
  165. <div class="line"> </div>
  166. <div class="line"> <span class="comment">// BUTTON SETUP </span></div>
  167. <div class="line"> </div>
  168. <div class="line"> <span class="comment">// SELECT ONE OF THE FOLLOWING :</span></div>
  169. <div class="line"> <span class="comment">// 1) IF YOUR BUTTON HAS AN INTERNAL PULL-UP</span></div>
  170. <div class="line"> <span class="comment">// button.attach( BUTTON_PIN , INPUT_PULLUP ); // USE INTERNAL PULL-UP</span></div>
  171. <div class="line"> <span class="comment">// 2) IF YOUR BUTTON USES AN EXTERNAL PULL-UP</span></div>
  172. <div class="line"> button.<a class="code" href="class_bounce.html#aba08e592941465d033e3eba3dde66eaf">attach</a>( BUTTON_PIN, INPUT ); <span class="comment">// USE EXTERNAL PULL-UP</span></div>
  173. <div class="line"> </div>
  174. <div class="line"> <span class="comment">// DEBOUNCE INTERVAL IN MILLISECONDS</span></div>
  175. <div class="line"> button.<a class="code" href="class_debouncer.html#a930bf3945e698d77b889f6309079857d">interval</a>(5); </div>
  176. <div class="line"> </div>
  177. <div class="line"> <span class="comment">// INDICATE THAT THE LOW STATE CORRESPONDS TO PHYSICALLY PRESSING THE BUTTON</span></div>
  178. <div class="line"> button.<a class="code" href="class_bounce2_1_1_button.html#a3c88b7938b26bca9dc2c7e72aedc442e">setPressedState</a>(LOW); </div>
  179. <div class="line"> </div>
  180. <div class="line"> <span class="comment">// LED SETUP</span></div>
  181. <div class="line"> pinMode(LED_PIN,OUTPUT);</div>
  182. <div class="line"> digitalWrite(LED_PIN,ledState);</div>
  183. <div class="line"> </div>
  184. <div class="line">}</div>
  185. <div class="line"> </div>
  186. <div class="line"><span class="keywordtype">void</span> loop() {</div>
  187. <div class="line"> <span class="comment">// UPDATE THE BUTTON</span></div>
  188. <div class="line"> <span class="comment">// YOU MUST CALL THIS EVERY LOOP</span></div>
  189. <div class="line"> button.<a class="code" href="class_debouncer.html#a72f3e8d483555031d2ac21b0b7702c06">update</a>();</div>
  190. <div class="line"> </div>
  191. <div class="line"> <span class="keywordflow">if</span> ( button.<a class="code" href="class_bounce2_1_1_button.html#a61541ae21354cb7f5cc5bc8c05db59dd">pressed</a>() ) {</div>
  192. <div class="line"> </div>
  193. <div class="line"> <span class="comment">// TOGGLE THE LED STATE : </span></div>
  194. <div class="line"> ledState = !ledState; <span class="comment">// SET ledState TO THE OPPOSITE OF ledState</span></div>
  195. <div class="line"> digitalWrite(LED_PIN,ledState);</div>
  196. <div class="line"> </div>
  197. <div class="line"> }</div>
  198. <div class="line">}</div>
  199. </div><!-- fragment --><h1>DOCUMENTATION</h1>
  200. <p>The complete class documentation can be found in the "docs" folder or <a href="http://thomasfredericks.github.io/Bounce2/">online here</a>.</p>
  201. <h2>GITHUB PAGE (SOURCE CODE)</h2>
  202. <p><a href="https://github.com/thomasfredericks/Bounce2">https://github.com/thomasfredericks/Bounce2</a></p>
  203. <h1>HAVE A QUESTION?</h1>
  204. <p>Please post your questions <a href="http://forum.arduino.cc/index.php?topic=266132.0">here</a>.</p>
  205. <h1>ALTERNATE DEBOUNCE ALGORITHMS FOR ADVANCED USERS AND SPECIFIC CASES</h1>
  206. <h2>STABLE INTERVAL</h2>
  207. <p>By default, the <a class="el" href="class_bounce.html" title="The Debouncer:Bounce class. Links the Deboucing class to a hardware pin. This class is odly named,...">Bounce</a> library uses a stable interval to process the debouncing. This is simpler to understand and can cancel unwanted noise.</p>
  208. <p><img src="https://raw.github.com/thomasfredericks/Bounce-Arduino-Wiring/master/extras/BouncySwitch_stable.png" alt="" class="inline"/></p>
  209. <h2>LOCK-OUT INTERVAL</h2>
  210. <p>By defining "#define BOUNCE_LOCK_OUT" in "Bounce.h" (or in your code before including "Bounce.h") you can activate an alternative debouncing method. This method is a lot more responsive, but does not cancel noise.</p>
  211. <div class="fragment"><div class="line">#define BOUNCE_LOCK_OUT</div>
  212. </div><!-- fragment --><p><img src="https://raw.github.com/thomasfredericks/Bounce-Arduino-Wiring/master/extras/BouncySwitch_lockout.png" alt="" class="inline"/></p>
  213. <h2>WITH PROMPT DETECTION</h2>
  214. <p>By defining "#define BOUNCE_WITH_PROMPT_DETECTION" in "Bounce.h" (or in your code before including "Bounce.h") you can activate an alternative debouncing method. Button state changes are available immediately so long as the previous state has been stable for the timeout period. Otherwise the state will be updated as soon as the timeout period allows.</p>
  215. <ul>
  216. <li>Able to report acurate switch time normally with no delay.</li>
  217. <li>Use when accurate switch transition timing is important.</li>
  218. </ul>
  219. <div class="fragment"><div class="line">#define BOUNCE_WITH_PROMPT_DETECTION</div>
  220. </div><!-- fragment --> </div></div><!-- PageDoc -->
  221. </div><!-- contents -->
  222. <div class="ttc" id="aclass_bounce2_1_1_button_html"><div class="ttname"><a href="class_bounce2_1_1_button.html">Bounce2::Button</a></div><div class="ttdef"><b>Definition:</b> Bounce2.h:276</div></div>
  223. <div class="ttc" id="aclass_debouncer_html_a930bf3945e698d77b889f6309079857d"><div class="ttname"><a href="class_debouncer.html#a930bf3945e698d77b889f6309079857d">Debouncer::interval</a></div><div class="ttdeci">void interval(uint16_t interval_millis)</div><div class="ttdoc">Sets the debounce interval in milliseconds.</div><div class="ttdef"><b>Definition:</b> Bounce2.cpp:14</div></div>
  224. <div class="ttc" id="aclass_bounce2_1_1_button_html_a61541ae21354cb7f5cc5bc8c05db59dd"><div class="ttname"><a href="class_bounce2_1_1_button.html#a61541ae21354cb7f5cc5bc8c05db59dd">Bounce2::Button::pressed</a></div><div class="ttdeci">bool pressed() const</div><div class="ttdoc">Returns true if the button was physically pressed</div><div class="ttdef"><b>Definition:</b> Bounce2.h:320</div></div>
  225. <div class="ttc" id="aclass_debouncer_html_a72f3e8d483555031d2ac21b0b7702c06"><div class="ttname"><a href="class_debouncer.html#a72f3e8d483555031d2ac21b0b7702c06">Debouncer::update</a></div><div class="ttdeci">bool update()</div><div class="ttdoc">Updates the pin's state.</div><div class="ttdef"><b>Definition:</b> Bounce2.cpp:32</div></div>
  226. <div class="ttc" id="aclass_bounce_html"><div class="ttname"><a href="class_bounce.html">Bounce</a></div><div class="ttdoc">The Debouncer:Bounce class. Links the Deboucing class to a hardware pin. This class is odly named,...</div><div class="ttdef"><b>Definition:</b> Bounce2.h:184</div></div>
  227. <div class="ttc" id="aclass_bounce2_1_1_button_html_a3c88b7938b26bca9dc2c7e72aedc442e"><div class="ttname"><a href="class_bounce2_1_1_button.html#a3c88b7938b26bca9dc2c7e72aedc442e">Bounce2::Button::setPressedState</a></div><div class="ttdeci">void setPressedState(bool state)</div><div class="ttdoc">Set the electrical state (HIGH/LOW) that corresponds to a physical press. By default,...</div><div class="ttdef"><b>Definition:</b> Bounce2.h:299</div></div>
  228. <div class="ttc" id="aclass_debouncer_html_a80315de311b855f9787b4b6d5b7899e0"><div class="ttname"><a href="class_debouncer.html#a80315de311b855f9787b4b6d5b7899e0">Debouncer::fell</a></div><div class="ttdeci">bool fell() const</div><div class="ttdoc">Returns true if pin signal transitions from high to low.</div><div class="ttdef"><b>Definition:</b> Bounce2.cpp:128</div></div>
  229. <div class="ttc" id="aclass_bounce_html_aba08e592941465d033e3eba3dde66eaf"><div class="ttname"><a href="class_bounce.html#aba08e592941465d033e3eba3dde66eaf">Bounce::attach</a></div><div class="ttdeci">void attach(int pin, int mode)</div><div class="ttdoc">Attach to a pin and sets that pin's mode (INPUT, INPUT_PULLUP or OUTPUT).</div><div class="ttdef"><b>Definition:</b> Bounce2.cpp:149</div></div>
  230. <div class="ttc" id="aclass_debouncer_html_aa5fcde9156f700c362fd49af5dd92ecd"><div class="ttname"><a href="class_debouncer.html#aa5fcde9156f700c362fd49af5dd92ecd">Debouncer::read</a></div><div class="ttdeci">bool read() const</div><div class="ttdoc">Returns the pin's state (HIGH or LOW).</div><div class="ttdef"><b>Definition:</b> Bounce2.cpp:118</div></div>
  231. <div class="ttc" id="aclass_debouncer_html_a1bbd627f9d8809b5ee9e64e9fb21e248"><div class="ttname"><a href="class_debouncer.html#a1bbd627f9d8809b5ee9e64e9fb21e248">Debouncer::changed</a></div><div class="ttdeci">bool changed() const</div><div class="ttdoc">Returns true if the state changed on last update.</div><div class="ttdef"><b>Definition:</b> Bounce2.h:130</div></div>
  232. <!-- start footer part -->
  233. <hr class="footer"/><address class="footer"><small>
  234. Generated by &#160;<a href="http://www.doxygen.org/index.html">
  235. <img class="footer" src="doxygen.png" alt="doxygen"/>
  236. </a> 1.8.17
  237. </small></address>
  238. </body>
  239. </html>