<h2 style="box-sizing: border-box; word-break: break-word; text-size-adjust: none; margin: 0px; padding: 0px; border: 0px; font-size: 16px; display: table-cell; vertical-align: middle; color: #333333; font-family: &quot;Noto Sans KR&quot;, dotum, sans-serif; background-color: #ffffff;">[FIT0450]TT 모터(엔코더 포함)(6V 160RPM 120:1)</h2>
<div class="tit_summery" style="box-sizing: border-box; word-break: break-word; text-size-adjust: none; margin: 0px; padding: 0px; border: 0px; position: relative; font-size: 13px; color: #333333; font-family: &quot;Noto Sans KR&quot;, dotum, sans-serif; background-color: #ffffff;">TT Motor with Encoder (6V 160RPM 120:1)</div>
<div class="tit_summery" style="box-sizing: border-box; word-break: break-word; text-size-adjust: none; margin: 0px; padding: 0px; border: 0px; position: relative; font-size: 13px; color: #333333; font-family: &quot;Noto Sans KR&quot;, dotum, sans-serif; background-color: #ffffff;">&nbsp;</div>
<div class="tit_summery" style="box-sizing: border-box; word-break: break-word; text-size-adjust: none; margin: 0px; padding: 0px; border: 0px; position: relative; font-size: 13px; color: #333333; font-family: &quot;Noto Sans KR&quot;, dotum, sans-serif; background-color: #ffffff;">
   <h2 id="target_0" style="box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 1.25; color: #24292e; margin-top: -60px; margin-bottom: 16px; padding-bottom: 0.3em; border-bottom: 1px solid rgb(234, 236, 239); padding-top: 60px;">Introduction</h2>
   <p style="box-sizing: border-box; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;">This is the DFRobot Micro&nbsp;<a href="https://www.dfrobot.com/category-110.html" style="box-sizing: border-box; background-color: transparent; color: #0366d6; text-decoration-line: none;" target="_blank">DC geared motor</a>&nbsp;with encoder. It is a motor with a 120:1 gearbox and an integrated quadrature encoder that provides a resolution of 8 pulse single per round giving a maximum output of 960 within one round. With an&nbsp;<a href="https://www.dfrobot.com/category-104.html" style="box-sizing: border-box; background-color: transparent; color: #0366d6; text-decoration-line: none;" target="_blank">Arduino controller</a>&nbsp;and&nbsp;<a href="https://www.dfrobot.com/category-105.html" style="box-sizing: border-box; background-color: transparent; color: #0366d6; text-decoration-line: none;" target="_blank">motor driver</a>, applications for this might include a closed-loop PID control or PWM motor speed control. This motor is an ideal option for mobile robot projects. The copper output shaft, embedded thread and reinforced connector greatly extends the motor's service</p>
   <h2 id="target_1" style="box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 1.25; color: #24292e; margin-top: -60px; margin-bottom: 16px; padding-bottom: 0.3em; border-bottom: 1px solid rgb(234, 236, 239); padding-top: 60px;">Specification</h2>
   <ul style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; padding-left: 2em; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;">
      <li style="box-sizing: border-box;">Gear ratio: 120:1</li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">No-load speed @ 6V: 160 rpm</li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">No-load speed @ 3V: 60 rpm</li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">No-load current @ 6V: 0.17A</li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">No-load current @ 3V: 0.14A</li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">Max Stall current: 2.8A</li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">Max Stall torque: 0.8kgf.cm</li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">Rated torque: 0.2kgf.cm</li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">Encoder operating voltage: 4.5~7.5V</li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">Motor operating voltage: 3~7.5V (Rated voltage 6V)</li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">Operating ambient temperature: -10 ~ +60℃</li>
      </ul>
   <h2 id="target_2" style="box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 1.25; color: #24292e; margin-top: -60px; margin-bottom: 16px; padding-bottom: 0.3em; border-bottom: 1px solid rgb(234, 236, 239); padding-top: 60px;">Pin Description</h2>
   <p style="box-sizing: border-box; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;"><img src="https://raw.githubusercontent.com/DFRobot/DFRobotMediaWikiImage/master/Image/FIT0450.png" width="600" style="box-sizing: content-box; border: 0px none; vertical-align: middle; max-width: 100%;" /></p>
   <table style="border-spacing: 0px; margin-top: 0px; margin-bottom: 16px; display: block; width: 661px; overflow: auto; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;">
      <thead style="box-sizing: border-box;">
         <tr style="box-sizing: border-box; border-top: 1px solid rgb(198, 203, 209);">
            <th style="box-sizing: border-box; padding: 6px 13px; text-align: left; border-style: solid; border-color: #dfe2e5;"><span style="box-sizing: border-box;">Grade</span></th>
            <th style="box-sizing: border-box; padding: 6px 13px; text-align: left; border-style: solid; border-color: #dfe2e5;"><span style="box-sizing: border-box;">Name</span></th>
            <th style="box-sizing: border-box; padding: 6px 13px; text-align: left; border-style: solid; border-color: #dfe2e5;"><span style="box-sizing: border-box;">Functional Description</span></th>
            </tr>
         </thead>
      <tbody style="box-sizing: border-box;">
         <tr style="box-sizing: border-box; border-top: 1px solid rgb(198, 203, 209);">
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">1</td>
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">Motor power supply pin +</td>
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">3-7.5V，Rated voltage6V</td>
            </tr>
         <tr style="box-sizing: border-box; background-color: #f6f8fa; border-top: 1px solid rgb(198, 203, 209);">
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">2</td>
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">Motor power supply pin -</td>
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;"></td>
            </tr>
         <tr style="box-sizing: border-box; border-top: 1px solid rgb(198, 203, 209);">
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">3</td>
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">Encoder A phase output</td>
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">Changes square wave with the output frequency of Motor speed</td>
            </tr>
         <tr style="box-sizing: border-box; background-color: #f6f8fa; border-top: 1px solid rgb(198, 203, 209);">
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">4</td>
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">Encoder B phase output</td>
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">Changes square wave with the output frequency of Motor speed(interrupt port)</td>
            </tr>
         <tr style="box-sizing: border-box; border-top: 1px solid rgb(198, 203, 209);">
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">5</td>
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">Encoder supply GND</td>
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;"></td>
            </tr>
         <tr style="box-sizing: border-box; background-color: #f6f8fa; border-top: 1px solid rgb(198, 203, 209);">
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">6</td>
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">Encoder supply +</td>
            <td style="box-sizing: border-box; padding: 6px 13px; border-style: solid; border-color: #dfe2e5;">4.5-7.5V</td>
            </tr>
         </tbody>
      </table>
   <h2 id="target_3" style="box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 1.25; color: #24292e; margin-top: -60px; margin-bottom: 16px; padding-bottom: 0.3em; border-bottom: 1px solid rgb(234, 236, 239); padding-top: 60px;">Tutorial</h2>
   <h3 id="requirements" style="box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 1.25; color: #24292e; margin-top: 24px; margin-bottom: 16px; font-size: 1.25em;">Requirements</h3>
   <ul style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; padding-left: 2em; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;">
      <li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 600;">hardware</span>
         <ul style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding-left: 2em;">
            <li style="box-sizing: border-box;"><a href="https://www.dfrobot.com/product-610.html" style="box-sizing: border-box; background-color: transparent; color: #0366d6; text-decoration-line: none;" target="_blank">ARduino UNO</a>&nbsp;x1</li>
            <li style="box-sizing: border-box; margin-top: 0.25em;">DC power supply x1</li>
            <li style="box-sizing: border-box; margin-top: 0.25em;"><a href="https://www.dfrobot.com/product-1180.html" style="box-sizing: border-box; background-color: transparent; color: #0366d6; text-decoration-line: none;" target="_blank">L298 2x2A Motor Shield for Arduino Twin</a>&nbsp;x1</li>
            </ul>
         </li>
      <li style="box-sizing: border-box; margin-top: 0.25em;"><span style="box-sizing: border-box; font-weight: 600;">software</span>
         <ul style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding-left: 2em;">
            <li style="box-sizing: border-box;">Arduino IDE&nbsp;<a href="https://www.arduino.cc/en/Main/Software" style="box-sizing: border-box; background-color: transparent; color: #0366d6; text-decoration-line: none;" target="_blank">Download Arduino IDE</a></li>
            </ul>
         </li>
      </ul>
   <h3 id="connection-diagram" style="box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 1.25; color: #24292e; margin-top: 24px; margin-bottom: 16px; font-size: 1.25em;">Connection Diagram</h3>
   <p style="box-sizing: border-box; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;"><img src="https://raw.githubusercontent.com/DFRobot/DFRobotMediaWikiImage/master/Image/FIT0450+UNO.png" width="600" style="box-sizing: content-box; border: 0px none; vertical-align: middle; max-width: 100%;" /></p>
   <p style="box-sizing: border-box; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;">This tutorial is about Encoder usage. We are using D2&amp;D3 as driving pins, you can select other ones, but it requires at least 1 interrupt pin. (We selected D2, Interrupt 0 in this tutorial).</p>
   <h3 id="interrupt-port-with-different-board" style="box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 1.25; color: #24292e; margin-top: 24px; margin-bottom: 16px; font-size: 1.25em;"><span style="box-sizing: border-box;">Interrupt Port with Different Board</span></h3>
   <p style="box-sizing: border-box; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 600;"><em style="box-sizing: border-box;">Notice:</em></span>&nbsp;attachInterrupt()</p>
   <p style="box-sizing: border-box; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;"><img src="https://raw.githubusercontent.com/DFRobot/DFRobotMediaWikiImage/master/Image/interrupt.png" width="600" style="box-sizing: content-box; border: 0px none; vertical-align: middle; max-width: 100%;" /></p>
   <p style="box-sizing: border-box; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;">If using an Arduino UNO and you want to use interrupt port 0 (Int.0), you need to connect digital pin D2 on the board. The following code is only used in UNO and Mega2560. If you want to use Arduino Leonardo, you should change digital pin D3 instead of digital pin D2.</p>
   <p style="box-sizing: border-box; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 600;"><em style="box-sizing: border-box;">See the link for details&nbsp;<a href="http://arduino.cc/en/Reference/AttachInterrupt" style="box-sizing: border-box; background-color: transparent; color: #0366d6; text-decoration-line: none;" target="_blank">http://arduino.cc/en/Reference/AttachInterrupt</a></em></span></p>
   <h3 id="sample-code-1" style="box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 1.25; color: #24292e; margin-top: 24px; margin-bottom: 16px; font-size: 1.25em;">Sample Code 1</h3>
   <div class="code-toolbar" style="box-sizing: border-box; position: relative; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;">
      <pre class=" language-arduino" style="box-sizing: border-box; overflow: auto; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 16px; margin-top: 0px; margin-bottom: 16px; line-height: 1.45; color: black; word-break: normal; overflow-wrap: normal; background: rgb(246, 248, 250); border: 1px solid rgb(204, 204, 204); border-radius: 3px; text-shadow: white 0px 1px; word-spacing: normal; tab-size: 4; hyphens: none;">
         <code class=" language-arduino" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0px; background: transparent; border-radius: 3px; text-shadow: white 0px 1px; word-spacing: normal; word-break: normal; overflow-wrap: normal; line-height: inherit; tab-size: 4; hyphens: none; margin: 0px; border: 0px; display: inline; overflow: visible;"><span class="token comment" style="box-sizing: border-box; color: slategray;">//The sample code for driving one way motor encoder</span>
const <span class="token keyword" style="box-sizing: border-box; color: #0077aa;">byte</span> encoder0pinA <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token number" style="box-sizing: border-box; color: #990055;">2</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//A pin -&gt; the interrupt pin 0</span>
const <span class="token keyword" style="box-sizing: border-box; color: #0077aa;">byte</span> encoder0pinB <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token number" style="box-sizing: border-box; color: #990055;">3</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//B pin -&gt; the digital pin 3</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">byte</span> encoder0PinALast<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">int</span> duration<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//the number of the pulses</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">boolean</span> Direction<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//the rotation direction</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">void</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">setup</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span><span class="token builtin" style="box-sizing: border-box; color: #669900;">Serial</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">begin</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token number" style="box-sizing: border-box; color: #990055;">57600</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//Initialize the serial port</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">EncoderInit</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//Initialize the module</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">void</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">loop</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span><span class="token builtin" style="box-sizing: border-box; color: #669900;">Serial</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">print</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token string" style="box-sizing: border-box; color: #669900;">"Pulse:"</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token builtin" style="box-sizing: border-box; color: #669900;">Serial</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">println</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>duration<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span>
  duration <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token number" style="box-sizing: border-box; color: #990055;">0</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">delay</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token number" style="box-sizing: border-box; color: #990055;">100</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">void</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">EncoderInit</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span>
  Direction <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token boolean" style="box-sizing: border-box; color: #990055;">true</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//default -&gt; Forward</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">pinMode</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>encoder0pinB<span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span><span class="token constant" style="box-sizing: border-box; color: #990055;">INPUT</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">attachInterrupt</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token number" style="box-sizing: border-box; color: #990055;">0</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span> wheelSpeed<span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span> CHANGE<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">void</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">wheelSpeed</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">int</span> Lstate <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">digitalRead</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>encoder0pinA<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">if</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>encoder0PinALast <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">==</span><span class="token constant" style="box-sizing: border-box; color: #990055;">LOW</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">&amp;&amp;</span> Lstate<span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">==</span><span class="token constant" style="box-sizing: border-box; color: #990055;">HIGH</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">int</span> val <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">digitalRead</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>encoder0pinB<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">if</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>val <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">==</span><span class="token constant" style="box-sizing: border-box; color: #990055;">LOW</span><span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">&amp;&amp;</span> Direction<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span>
      Direction <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token boolean" style="box-sizing: border-box; color: #990055;">false</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//Reverse</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">else</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">if</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>val <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">==</span><span class="token constant" style="box-sizing: border-box; color: #990055;">HIGH</span><span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">&amp;&amp;</span><span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">!</span>Direction<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span>
      Direction <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token boolean" style="box-sizing: border-box; color: #990055;">true</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//Forward</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span>
  encoder0PinALast <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span> Lstate<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">if</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">!</span>Direction<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span>  duration<span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">++</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">else</span>  duration<span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">--</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span></code>
         </pre>
      <div class="toolbar" style="box-sizing: border-box; position: absolute; top: 0.3em; right: 0.2em; transition: opacity 0.3s ease-in-out 0s; opacity: 0;">
         <div class="toolbar-item" style="box-sizing: border-box; display: inline-block;"><button style="margin: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 0.8em; font-family: inherit; color: #e88c42; overflow: visible; appearance: button; cursor: pointer; background: rgba(224, 224, 224, 0.2); border-width: 0px; border-style: initial; border-color: initial; padding: 0px 0.5em; user-select: none; box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">Copy</button></div>
         </div>
      </div>
   <p style="box-sizing: border-box; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 600;">Code 1 phenomenon:</span></p>
   <p style="box-sizing: border-box; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;">Explanation: Serial data, when the motor forward, the output value&gt; 0, when the motor reverse rotation, digital output &lt;0. The faster the motor speed, the greater the absolute value of number.</p>
   <p style="box-sizing: border-box; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;"><img src="https://raw.githubusercontent.com/DFRobot/DFRobotMediaWikiImage/master/Image/FIT0450_data_out.png" alt="FIT0450_data_out.png" title="FIT0450_data_out.png" style="box-sizing: content-box; border: 0px none; vertical-align: middle; max-width: 100%;" /></p>
   <h3 id="sample-code-2" style="box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 1.25; color: #24292e; margin-top: 24px; margin-bottom: 16px; font-size: 1.25em;">Sample Code 2</h3>
   <p style="box-sizing: border-box; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 600;">PID control：</span>&nbsp;PID algorithm to control the motor speed by L298P DC motor driver board</p>
   <ol style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; padding-left: 2em; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;">
      <li style="box-sizing: border-box;">Motor power port is connected to the L298 drive motor M1 port</li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">Download and install&nbsp;<a href="https://github.com/br3ttb/Arduino-PID-Library/" style="box-sizing: border-box; background-color: transparent; color: #0366d6; text-decoration-line: none;" target="_blank">Arduino PID</a></li>
      </ol>
   <div class="code-toolbar" style="box-sizing: border-box; position: relative; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px;">
      <pre class=" language-arduino" style="box-sizing: border-box; overflow: auto; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 16px; margin-top: 0px; margin-bottom: 16px; line-height: 1.45; color: black; word-break: normal; overflow-wrap: normal; background: rgb(246, 248, 250); border: 1px solid rgb(204, 204, 204); border-radius: 3px; text-shadow: white 0px 1px; word-spacing: normal; tab-size: 4; hyphens: none;">
         <code class=" language-arduino" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; padding: 0px; background: transparent; border-radius: 3px; text-shadow: white 0px 1px; word-spacing: normal; word-break: normal; overflow-wrap: normal; line-height: inherit; tab-size: 4; hyphens: none; margin: 0px; border: 0px; display: inline; overflow: visible;"><span class="token comment" style="box-sizing: border-box; color: slategray;">//The sample code for driving one way motor encoder</span><span class="token macro property" style="box-sizing: border-box; color: #990055;">#<span class="token directive keyword" style="box-sizing: border-box; color: #0077aa;">include</span><span class="token string" style="box-sizing: border-box; color: #669900;">&lt;PID_v1.h&gt;</span></span>
const <span class="token keyword" style="box-sizing: border-box; color: #0077aa;">byte</span> encoder0pinA <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token number" style="box-sizing: border-box; color: #990055;">2</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//A pin -&gt; the interrupt pin 0</span>
const <span class="token keyword" style="box-sizing: border-box; color: #0077aa;">byte</span> encoder0pinB <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token number" style="box-sizing: border-box; color: #990055;">3</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//B pin -&gt; the digital pin 3</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">int</span> E_left <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token number" style="box-sizing: border-box; color: #990055;">5</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//The enabling of L298PDC motor driver board connection to the digital interface port 5</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">int</span> M_left <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token number" style="box-sizing: border-box; color: #990055;">4</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//The enabling of L298PDC motor driver board connection to the digital interface port 4</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">byte</span> encoder0PinALast<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">double</span> duration<span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span>abs_duration<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//the number of the pulses</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">boolean</span> Direction<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//the rotation direction</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">boolean</span> result<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">double</span> val_output<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//Power supplied to the motor PWM value.</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">double</span> Setpoint<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">double</span> Kp<span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token number" style="box-sizing: border-box; color: #990055;">0.6</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span> Ki<span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token number" style="box-sizing: border-box; color: #990055;">5</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span> Kd<span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token number" style="box-sizing: border-box; color: #990055;">0</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span>
PID <span class="token function" style="box-sizing: border-box; color: #dd4a68;">myPID</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">&amp;</span>abs_duration<span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span><span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">&amp;</span>val_output<span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span><span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">&amp;</span>Setpoint<span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span> Kp<span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span> Ki<span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span> Kd<span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span> DIRECT<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">void</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">setup</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span><span class="token builtin" style="box-sizing: border-box; color: #669900;">Serial</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">begin</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token number" style="box-sizing: border-box; color: #990055;">9600</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//Initialize the serial port</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">pinMode</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>M_left<span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span><span class="token constant" style="box-sizing: border-box; color: #990055;">OUTPUT</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//L298P Control port settings DC motor driver board for the output mode</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">pinMode</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>E_left<span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span><span class="token constant" style="box-sizing: border-box; color: #990055;">OUTPUT</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span>
   Setpoint <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token number" style="box-sizing: border-box; color: #990055;">80</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//Set the output value of the PID</span>
   myPID<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">SetMode</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>AUTOMATIC<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//PID is set to automatic mode</span>
   myPID<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">SetSampleTime</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token number" style="box-sizing: border-box; color: #990055;">100</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//Set PID sampling frequency is 100ms</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">EncoderInit</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//Initialize the module</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">void</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">loop</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">advance</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//Motor Forward</span>
      abs_duration<span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">abs</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>duration<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span>
      result<span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span>myPID<span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">Compute</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//PID conversion is complete and returns 1</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">if</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>result<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span><span class="token builtin" style="box-sizing: border-box; color: #669900;">Serial</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">print</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token string" style="box-sizing: border-box; color: #669900;">"Pluse: "</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token builtin" style="box-sizing: border-box; color: #669900;">Serial</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">.</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">println</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>duration<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span>
        duration <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token number" style="box-sizing: border-box; color: #990055;">0</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//Count clear, wait for the next count</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">void</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">EncoderInit</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span>
  Direction <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token boolean" style="box-sizing: border-box; color: #990055;">true</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//default -&gt; Forward</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">pinMode</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>encoder0pinB<span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span><span class="token constant" style="box-sizing: border-box; color: #990055;">INPUT</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">attachInterrupt</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token number" style="box-sizing: border-box; color: #990055;">0</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span> wheelSpeed<span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span> CHANGE<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">void</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">wheelSpeed</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">int</span> Lstate <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">digitalRead</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>encoder0pinA<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">if</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>encoder0PinALast <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">==</span><span class="token constant" style="box-sizing: border-box; color: #990055;">LOW</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">&amp;&amp;</span> Lstate<span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">==</span><span class="token constant" style="box-sizing: border-box; color: #990055;">HIGH</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">int</span> val <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">digitalRead</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>encoder0pinB<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">if</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>val <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">==</span><span class="token constant" style="box-sizing: border-box; color: #990055;">LOW</span><span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">&amp;&amp;</span> Direction<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span>
      Direction <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token boolean" style="box-sizing: border-box; color: #990055;">false</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//Reverse</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">else</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">if</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>val <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">==</span><span class="token constant" style="box-sizing: border-box; color: #990055;">HIGH</span><span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">&amp;&amp;</span><span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">!</span>Direction<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span>
      Direction <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span><span class="token boolean" style="box-sizing: border-box; color: #990055;">true</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//Forward</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span>
  encoder0PinALast <span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">=</span> Lstate<span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">if</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">!</span>Direction<span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span>  duration<span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">++</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">else</span>  duration<span class="token operator" style="box-sizing: border-box; color: #9a6e3a; background: rgba(255, 255, 255, 0.5);">--</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">}</span><span class="token keyword" style="box-sizing: border-box; color: #0077aa;">void</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">advance</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token comment" style="box-sizing: border-box; color: slategray;">//Motor Forward</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">{</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">digitalWrite</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>M_left<span class="token punctuation" style="box-sizing: border-box; color: #999999;">,</span><span class="token constant" style="box-sizing: border-box; color: #990055;">LOW</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">;</span><span class="token function" style="box-sizing: border-box; color: #dd4a68;">analogWrite</span><span class="token punctuation" style="box-sizing: border-box; color: #999999;">(</span>E_left<span class="token punctuation" style="box-sizing: border-box; color: #99