I'm hoping the fcan help me identify what needs to be done to implement speed control of the robot motors. This is the section of the code for speed control.
The right motor speed can be controlled, however the left motor does not turn at all.
I'm new to Aurduino progamming so I'm sure its a simple problem. My goal is to implement blurtooth control in this code.
I need to implement speed control in the Android app. I've not come accross any Android apps that offers speed control,
just directional control. I appreciate feedback on how this codes needs to be modified for speed control and why the left motor fails to run.
Thanks so much,
Tom
//Turn on left motor
digitalWrite (LeftMotorBackward, LOW);
digitalWrite(LeftMotorForward, HIGH);
//set speed
analogWrite(LeftMotorSpeed, 50);
// turn on right motor
digitalWrite(RightMotorBackward, LOW);
digitalWrite(RightMotorForward, HIGH);
// set speed
analogWrite (RightMotorSpeed, 100);
Now I'm needing to vary the speed of the motors. The left motor does not move, however the right motor does. The analogwrite commands are on line 57 and 60 for speed control. My ultimate goal is to control the remote by Bluetooth or xbee, or perhaps recommendations from others. The remote device needs to offer speed control. I'm still learning the Aurduino and appreciate suggestions for speed control in this sketch and remote controllers.
Is MIT inventors kit a good place to start for Bluetooth control?
<pre>
<font color="#434f54">//Since we are using servos and ultrasonic sensors in the robot we will include some libraries written to make their use easier</font>
<font color="#000000">1#include</font> <font color="#434f54"><</font><b><font color="#d35400">Servo</font></b><font color="#434f54">.</font><font color="#000000">h</font><font color="#434f54">></font>
<font color="#000000">2#include</font> <font color="#434f54"><</font><b><font color="#d35400">NewPing</font></b><font color="#434f54">.</font><font color="#000000">h</font><font color="#434f54">></font>
<font color="#434f54">//Below are the symbolic constants. Instead of having to type in a non-sensical pin number each time we want to do something we can write an easy to understand name which represents the pin, the compiler will then replace the names with the numbers</font>
<font color="#000000">2</font> <font color="#5e6d03">#define</font> <font color="#000000">LeftMotorForward</font> <font color="#000000">8</font>
<font color="#000000">3</font> <font color="#5e6d03">#define</font> <font color="#000000">LeftMotorBackward</font> <font color="#000000">9</font>
<font color="#000000">4</font> <font color="#5e6d03">#define</font> <font color="#000000">LeftMotorSpeed</font> <font color="#000000">10</font>
<font color="#000000">5</font> <font color="#5e6d03">#define</font> <font color="#000000">RightMotorForward</font> <font color="#000000">6</font>
<font color="#000000">6</font> <font color="#5e6d03">#define</font> <font color="#000000">RightMotorBackward</font> <font color="#000000">7</font>
<font color="#000000">7</font> <font color="#5e6d03">#define</font> <font color="#000000">RightMotorSpeed</font> <font color="#000000">5</font>
<font color="#000000">8</font> <font color="#5e6d03">#define</font> <font color="#000000">USTrigger</font> <font color="#000000">3</font>
<font color="#000000">9</font> <font color="#5e6d03">#define</font> <font color="#000000">USEcho</font> <font color="#000000">2</font>
<font color="#000000">10</font> <font color="#5e6d03">#define</font> <font color="#000000">MaxDistance</font> <font color="#000000">100</font>
<font color="#000000">11</font> <font color="#5e6d03">#define</font> <font color="#000000">LED</font> <font color="#000000">13</font>
<font color="#434f54">//Here we have created two ‘objects’, one for the servo and one for the ultrasonic sensor</font>
<font color="#000000">12</font> <b><font color="#d35400">Servo</font></b> <font color="#000000">servo</font><font color="#000000">;</font>
<font color="#000000">13</font> <b><font color="#d35400">NewPing</font></b> <font color="#000000">sonar</font><font color="#000000">(</font><font color="#000000">USTrigger</font><font color="#434f54">,</font> <font color="#000000">USEcho</font><font color="#434f54">,</font> <font color="#000000">MaxDistance</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#434f54">//Below we are creating unsigned integer variables which we will use later on in the code. They are unsigned as they will only have postive values</font>
<font color="#000000">14</font> <font color="#00979c">unsigned</font> <font color="#00979c">int</font> <font color="#000000">duration</font><font color="#000000">;</font>
<font color="#000000">15</font> <font color="#00979c">unsigned</font> <font color="#00979c">int</font> <font color="#000000">distance</font><font color="#000000">;</font>
<font color="#000000">16</font> <font color="#00979c">unsigned</font> <font color="#00979c">int</font> <font color="#000000">FrontDistance</font><font color="#000000">;</font>
<font color="#000000">17</font> <font color="#00979c">unsigned</font> <font color="#00979c">int</font> <font color="#000000">LeftDistance</font><font color="#000000">;</font>
<font color="#000000">18</font> <font color="#00979c">unsigned</font> <font color="#00979c">int</font> <font color="#000000">RightDistance</font><font color="#000000">;</font>
<font color="#000000">19</font> <font color="#00979c">unsigned</font> <font color="#00979c">int</font> <font color="#000000">Time</font><font color="#000000">;</font>
<font color="#000000">20</font> <font color="#00979c">unsigned</font> <font color="#00979c">int</font> <font color="#000000">CollisionCounter</font><font color="#000000">;</font>
<font color="#000000">21</font> <font color="#00979c">void</font> <font color="#5e6d03">setup</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#434f54">//This block happens once on startup</font>
<font color="#000000">22</font> <font color="#000000">{</font>
<font color="#000000">23</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">begin</font><font color="#000000">(</font><font color="#000000">9600</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//I have included the serial initialize but commented it out, if you want to debug and print information to the serial monitor just uncomment</font>
<font color="#434f54">//Here we are setting the pin modes. As we will sending out signals from the pins we set them as outputs</font>
<font color="#000000">24</font> <font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">LeftMotorForward</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">25</font> <font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">LeftMotorBackward</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">26</font> <font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">LeftMotorSpeed</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">27</font> <font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">RightMotorForward</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">28</font> <font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">RightMotorBackward</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">29</font> <font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">RightMotorSpeed</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">30</font> <font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">LED</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">31</font> <font color="#000000">servo</font><font color="#434f54">.</font><font color="#d35400">attach</font><font color="#000000">(</font><font color="#000000">11</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//The servo is attached to pin 4</font>
<font color="#000000">32</font> <font color="#000000">}</font>
<font color="#000000">33</font> <font color="#00979c">void</font> <font color="#5e6d03">loop</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#434f54">//This block repeats itself while the Arduino is turned on</font>
<font color="#000000">34</font> <font color="#000000">{</font>
<font color="#000000">35</font> <font color="#000000">servo</font><font color="#434f54">.</font><font color="#d35400">write</font><font color="#000000">(</font><font color="#000000">90</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Rotate the servo to face the front </font>
<font color="#000000">36</font> <font color="#000000">scan</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Go to the scan function</font>
<font color="#000000">37</font> <font color="#000000">FrontDistance</font> <font color="#434f54">=</font> <font color="#000000">distance</font><font color="#000000">;</font> <font color="#434f54">//Set the variable FrontDistance to the value of the distance returned from the scan function</font>
<font color="#000000">38</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">“Front distance = “</font><font color=”#000000”>)</font><font color="#000000">;</font>
<font color="#000000">39</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#000000">distance</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">40</font> <font color="#5e6d03">if</font><font color="#000000">(</font><font color="#000000">FrontDistance</font> <font color="#434f54">></font> <font color="#000000">40</font> <font color="#434f54">||</font> <font color="#000000">FrontDistance</font> <font color="#434f54">==</font> <font color="#000000">0</font><font color="#000000">)</font> <font color="#434f54">//If there is nothing infront of the robot within 40cm or the distance value is 0 (which for the newping libary means no ping was returned) then…</font>
<font color="#000000">41</font> <font color="#000000">{</font>
<font color="#000000">42</font> <font color="#000000">moveForward</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Go to the moveForward function</font>
<font color="#000000">43</font> <font color="#000000">}</font>
<font color="#000000">44</font> <font color="#5e6d03">else</font> <font color="#434f54">//Else (if there is something infront of the robot within 40cm) then…</font>
<font color="#000000">45</font> <font color="#000000">{</font>
<font color="#000000">46</font> <font color="#000000">CollisionCounter</font> <font color="#434f54">=</font> <font color="#000000">CollisionCounter</font> <font color="#434f54">+</font> <font color="#000000">1</font><font color="#000000">;</font>
<font color="#000000">47</font> <font color="#000000">moveStop</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Go to the moveStop function</font>
<font color="#000000">48</font> <font color="#000000">navigate</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">49</font> <font color="#000000">}</font>
<font color="#000000">50</font> <font color="#000000">}</font>
<font color="#000000">51</font> <font color="#00979c">void</font> <font color="#000000">moveForward</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#434f54">//This function tells the robot to go forward </font>
<font color="#000000">52</font> <font color="#000000">{</font>
<font color="#000000">53</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">""</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">54</font> <b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">“Moving forward”</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#434f54">//Turn on left motor</font>
<font color="#000000">55</font> <font color="#d35400">digitalWrite</font> <font color="#000000">(</font><font color="#000000">LeftMotorBackward</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">56</font> <font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">LeftMotorForward</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#434f54">//set speed</font>
<font color="#000000">57</font> <font color="#d35400">analogWrite</font><font color="#000000">(</font><font color="#000000">LeftMotorSpeed</font><font color="#434f54">,</font> <font color="#000000">50</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#434f54">// turn on right motor</font>
<font color="#000000">58</font> <font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">RightMotorBackward</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">59</font> <font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">RightMotorForward</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#434f54">// set speed</font>
<font color="#000000">60</font> <font color="#d35400">analogWrite</font> <font color="#000000">(</font><font color="#000000">RightMotorSpeed</font><font color="#434f54">,</font> <font color="#000000">100</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">}</font>
<font color="#00979c">void</font> <font color="#000000">moveBackward</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#434f54">//This function tells the robot to move backward</font>
<font color="#000000">{</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">""</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">“Moving backward”</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">LeftMotorSpeed</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font> <font color="#000000">(</font><font color="#000000">RightMotorSpeed</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">LeftMotorForward</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">LeftMotorBackward</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">RightMotorForward</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">RightMotorBackward</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">}</font>
<font color="#00979c">void</font> <font color="#000000">moveLeft</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#434f54">//This function tells the robot to turn left</font>
<font color="#000000">{</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">""</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">“Moving left”</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">LeftMotorSpeed</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font> <font color="#000000">(</font><font color="#000000">RightMotorSpeed</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">LeftMotorForward</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">LeftMotorBackward</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">RightMotorBackward</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">RightMotorForward</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">}</font>
<font color="#00979c">void</font> <font color="#000000">moveRight</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#434f54">//This function tells the robot to turn right</font>
<font color="#000000">{</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">LeftMotorSpeed</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font> <font color="#000000">(</font><font color="#000000">RightMotorSpeed</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">""</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">“Moving right”</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">LeftMotorBackward</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">LeftMotorForward</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">RightMotorForward</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">RightMotorBackward</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">}</font>
<font color="#00979c">void</font> <font color="#000000">moveStop</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#434f54">//This function tells the robot to stop moving</font>
<font color="#000000">{</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">""</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">“Stopping”</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">LeftMotorSpeed</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font> <font color="#000000">(</font><font color="#000000">RightMotorSpeed</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">LeftMotorBackward</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">LeftMotorForward</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">RightMotorForward</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">RightMotorBackward</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">}</font>
<font color="#00979c">void</font> <font color="#000000">scan</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#434f54">//This function determines the distance things are away from the ultrasonic sensor</font>
<font color="#000000">{</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">""</font><font color="#000000">)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">“Scanning”</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">Time</font> <font color="#434f54">=</font> <font color="#000000">sonar</font><font color="#434f54">.</font><font color="#d35400">ping</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">distance</font> <font color="#434f54">=</font> <font color="#000000">Time</font> <font color="#434f54">/</font> <font color="#000000">US_ROUNDTRIP_CM</font><font color="#000000">;</font>
<font color="#d35400">delay</font><font color="#000000">(</font><font color="#000000">500</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">}</font>
<font color="#00979c">void</font> <font color="#000000">navigate</font><font color="#000000">(</font><font color="#000000">)</font>
<font color="#000000">{</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">“There’s an obstacle!”</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">servo</font><font color="#434f54">.</font><font color="#d35400">write</font><font color="#000000">(</font><font color="#000000">167</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Move the servo to the left (my little servos didn’t like going to 180 so I played around with the value until it worked nicely)</font>
<font color="#d35400">delay</font><font color="#000000">(</font><font color="#000000">1000</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Wait half a second for the servo to get there</font>
<font color="#000000">scan</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Go to the scan function</font>
<font color="#000000">LeftDistance</font> <font color="#434f54">=</font> <font color="#000000">distance</font><font color="#000000">;</font> <font color="#434f54">//Set the variable LeftDistance to the distance on the left</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">“Left distance = “</font><font color=”#000000”>)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#000000">distance</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">servo</font><font color="#434f54">.</font><font color="#d35400">write</font><font color="#000000">(</font><font color="#000000">0</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Move the servo to the right</font>
<font color="#d35400">delay</font><font color="#000000">(</font><font color="#000000">1000</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Wait half a second for the servo to get there</font>
<font color="#000000">scan</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Go to the scan function</font>
<font color="#000000">RightDistance</font> <font color="#434f54">=</font> <font color="#000000">distance</font><font color="#000000">;</font> <font color="#434f54">//Set the variable RightDistance to the distance on the right</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#00979c">“Right distance = “</font><font color=”#000000”>)</font><font color="#000000">;</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#000000">distance</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#5e6d03">if</font><font color="#000000">(</font><font color="#d35400">abs</font><font color="#000000">(</font><font color="#000000">RightDistance</font> <font color="#434f54">-</font> <font color="#000000">LeftDistance</font><font color="#000000">)</font> <font color="#434f54"><</font> <font color="#000000">5</font><font color="#000000">)</font>
<font color="#000000">{</font>
<font color="#000000">moveBackward</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Go to the moveBackward function</font>
<font color="#d35400">delay</font><font color="#000000">(</font><font color="#000000">200</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Pause the program for 200 milliseconds to let the robot reverse</font>
<font color="#000000">moveRight</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Go to the moveRight function</font>
<font color="#d35400">delay</font><font color="#000000">(</font><font color="#000000">100</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Pause the program for 200 milliseconds to let the robot turn right</font>
<font color="#000000">}</font>
<font color="#5e6d03">else</font> <font color="#5e6d03">if</font><font color="#000000">(</font><font color="#000000">RightDistance</font> <font color="#434f54"><</font> <font color="#000000">LeftDistance</font><font color="#000000">)</font> <font color="#434f54">//If the distance on the right is less than that on the left then…</font>
<font color="#000000">{</font>
<font color="#000000">moveLeft</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Go to the moveLeft function</font>
<font color="#d35400">delay</font><font color="#000000">(</font><font color="#000000">100</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Pause the program for half a second to let the robot turn</font>
<font color="#000000">}</font>
<font color="#5e6d03">else</font> <font color="#5e6d03">if</font><font color="#000000">(</font><font color="#000000">LeftDistance</font> <font color="#434f54"><</font> <font color="#000000">RightDistance</font><font color="#000000">)</font> <font color="#434f54">//Else if the distance on the left is less than that on the right then…</font>
<font color="#000000">{</font>
<font color="#000000">moveRight</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Go to the moveRight function</font>
<font color="#d35400">delay</font><font color="#000000">(</font><font color="#000000">100</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Pause the program for half a second to let the robot turn</font>
<font color="#000000">}</font>
<font color="#000000">}</font>
</pre>