<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>Robot is Happy &#187; Light-Seeking Robot</title>
	<atom:link href="http://www.robotishappy.com/category/projects/light-seeking-robot/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.robotishappy.com</link>
	<description>creating our robot overlords one day at a time</description>
	<lastBuildDate>Tue, 19 Oct 2010 13:29:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<!-- podcast_generator="podPress/8.8" - maintenance_release="8.8.5.3" -->
	<copyright>2006-2007 </copyright>
	<managingEditor>chris@robotishappy.com (Robot is Happy)</managingEditor>
	<webMaster>chris@robotishappy.com (Robot is Happy)</webMaster>
	<category>posts</category>
	<image>
		<url>http://www.robotishappy.com/wp-content/plugins/podpress/images/powered_by_podpress.jpg</url>
		<title>Robot is Happy &#187; Light-Seeking Robot</title>
		<link>http://www.robotishappy.com</link>
		<width>144</width>
		<height>144</height>
	</image>
	<itunes:subtitle></itunes:subtitle>
	<itunes:summary>creating our robot overlords one day at a time</itunes:summary>
	<itunes:keywords></itunes:keywords>
	<itunes:category text="Society &amp; Culture" />
	<itunes:author>Robot is Happy</itunes:author>
	<itunes:owner>
		<itunes:name>Robot is Happy</itunes:name>
		<itunes:email>chris@robotishappy.com</itunes:email>
	</itunes:owner>
	<itunes:block>no</itunes:block>
	<itunes:explicit>no</itunes:explicit>
	<itunes:image href="http://www.robotishappy.com/wp-content/plugins/podpress/images/powered_by_podpress_large.jpg" />
		<item>
		<title>Light-Seeking Robot using the Arduino</title>
		<link>http://www.robotishappy.com/2009/12/light-seeking-robot-using-the-arduino/</link>
		<comments>http://www.robotishappy.com/2009/12/light-seeking-robot-using-the-arduino/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 00:08:21 +0000</pubDate>
		<dc:creator>Christopher Hazlett</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Light-Seeking Robot]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Physical Computing]]></category>

		<guid isPermaLink="false">http://www.integratechange.com/?p=379</guid>
		<description><![CDATA[I've always been interested in physical computing.  Even though I love creating software, I've always felt that creating websites and software doesn't go far enough to satiate my creative desire.  Enter the Arduino, a great open-source micro processor and prototyping board for around $30. This post marks the first, in what I hope will be a long line of tutorials and projects using the Arduino to develop robots and sensor driven applications that I hope to release into the wild.]]></description>
			<content:encoded><![CDATA[<blockquote><p>This is a repost from my (Chris's) old blog www.integratechange.com on May 5, 2009.</p></blockquote>
<p>I've always been interested in <a href="http://en.wikipedia.org/wiki/Physical_computing">physical computing</a>.  Even though I love creating software, I've always felt that creating websites and software doesn't go far enough to satiate my creative desire.  Enter the <a href="http://www.arduino.cc">Arduino</a>, a great open-source micro processor and prototyping board for around $30. This post marks the first, in what I hope will be a long line of tutorials and projects using the Arduino to develop robots and sensor driven applications that I hope to release into the wild.  This post details a light-seeking robot with an Arduino brain, kind of the "Hello World!" application for autonomous robotics.</p>
<h2>The Parts</h2>
<p>I got a number of parts from several different sources.  I had some parts lying around, but I'll also provide links where you too can buy yourself all the light-seeking robot parts your heart desires.</p>
<ul>
<li><a href="http://www.adafruit.com/index.php?main_page=product_info&amp;cPath=17&amp;products_id=50">Arduino (1 @ $30.00 from Adafruit.com)</a> - I actually got the Arduino Starter kit from Adafruit.com which also came with the 9V battery holder you see in the pictures and videos below.</li>
<li><a href="http://www.adafruit.com/index.php?main_page=product_info&amp;cPath=17_21&amp;products_id=81">Arduino Motor Shield (1 @ $19.50 from Adafruit.com)</a> - You don't actually need the motor shield, but it provides a great base for prototyping and I recommend it if you're doing repeated prototyping with motors and servers.</li>
<li><a href="http://www.pololu.com/catalog/product/1060">RP5 Tracked Chassis (1 @ 49.95 from Pololu.com)</a> - I picked this chassis because it has all the necessary parts: geared motor assembly, tracks (who doesn't like tracks), battery pack, etc.  You can make your own chassis, of course, but for those interested, this is a nice one that's virtually plug and play.</li>
<li><a href="http://www.adafruit.com/index.php?main_page=product_info&amp;cPath=35&amp;products_id=161">CDS Photoresistors (2 @ $1.50 from Adafruit.com)</a> - For the eyes. You can get these from anywhere, but if you order from Adafruit.com, you might as well get some of these too.</li>
<li>10 Kohms Resistors (4) - You can get these from anywhere.</li>
<li><a href="http://www.radioshack.com/product/index.jsp?productId=2062539">Momentary Switch - (1 @ $3.95 for a Pack of 4 from Radio Shack)</a> - It's a tiny little push button switch, and you can use any you would like...this is just the one I used.</li>
</ul>
<h2>Prototype I - Proof of Concept</h2>
<p>While I was waiting for my chassis to arrive, I put together an initial prototype to test the circuit and code.  The first prototype was rough, to say the least, and it didn't move, but the code worked.  I used the prototype board you see in the picture (available at Radio Shack) as a base to mount (tape) the motors to.  I put tape on the axles to see if the code was actually making the motors turn the right way.  It was.</p>
<p><img src="http://www.integratechange.com/wp-content/uploads/2009/05/prototype-i.jpg" alt="Initial Prototype and Code Testing Platform" /></p>
<p>And here's a video, showing it work in all it's glory.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="340" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/5if-FJXeK6M&amp;hl=en&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="340" src="http://www.youtube.com/v/5if-FJXeK6M&amp;hl=en&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>As it says in the video, I initially gave the robot seeking and avoiding behavior (you can see it in the code below in the function foundSource()), but I realized that there are times when the robot will never find it's source (especially if that source is the sun) and that it was needless behavior.  I left the runAway variable in the code through so you could easily turn the robot into a photophobe.  You could also mount a switch on the robot that changes the behavior from photovore to photophobe as well.</p>
<h2>Prototype II</h2>
<p>Once I got the chassis from Pololu, I put together a better prototype, one that moves and turns around when it runs into something...you know, typical robot behavior.  In the following pictures, you can see the very crude method of assembly, but it works...and that was really the point.<br />
<img src="http://www.integratechange.com/wp-content/uploads/2009/05/img_0003.jpg" alt="Prototype II on the Chassis" /><br />
I literally moved the prototype board in the first picture above to the top of the chassis and hooked up the motors to the motor shield.  I added a small switch for the motor power so I could stop screwing and unscrewing the motor's power supply.  Everything is just taped down.  This is, again, more of a proof of concept than a finished robot.  I plan on making a custom motor driver for the chassis and better mount for the Arduino and other sensors.</p>
<p><img src="http://www.integratechange.com/wp-content/uploads/2009/05/img_0004.jpg" alt="Back-up sensor under the plow" /><br />
To give the push button more surface area, I added a plow to the front, with an "ultra-strong" electrical tape hinge.  Obviously, when the robot runs into something, it pushes the button and the turnAround() function executes.</p>
<p><img src="http://www.integratechange.com/wp-content/uploads/2009/05/img_0012.jpg" alt="Inside of the tracked chassis" /><br />
There's not a lot of room in the chassis, but you could replace the AA battery pack with a 9V to give you some more space.  But there are mounting holes on the corners which is probably a preferable location for your robot's brains.</p>
<p>Here it is in action:<br />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="340" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/4PIAs0rxOsA&amp;hl=en&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="340" src="http://www.youtube.com/v/4PIAs0rxOsA&amp;hl=en&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<h2>The Circuit</h2>
<p>The circuit is fairly simple, especially because I'm using the Motor Shield.  This circuit diagram illustrates the sensor circuitry and status led you see on the white bread board above.  The motors are attached to the M1 and M2 points on the motor shield (positive leads toward the inside of the board).  I used OmniGraffle to draw up the circuit, but I'm still looking for better templates so my circuits are prettier, more accurate, etc, but this drawing should illustrate the connections just fine.</p>
<p><img src="http://www.integratechange.com/wp-content/uploads/2009/05/light-seeking-robot.jpg" alt="Light Seeking Robot" width="613" height="491" /></p>
<h2>The Code</h2>
<p>You program the Arduino in Wiring, which is basically a library in C++.  Forgive me any trespasses below.  C++ is not a language I've worked in a lot, and the code below could most likely use some optimization.  I originally had many delays written into the loop because that's what I saw a lot of other code doing.  That resulted in erratic behavior and a lot of running into stuff even though the object's shadow should have caused the robot to avoid it.  When I removed the delay and averaged the light sensor readings, I got a very responsive robot.  I've also left a lot of code in the source because I'm working on building a robot base that I can use over and over again as I install more sensors and create more interesting behavior.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;AFMotor.h&gt;</span>
<span style="color: #339933;">#define NUMREADINGS 5</span>
<span style="color: #339933;">#define LEFTSENSOR 3</span>
<span style="color: #339933;">#define RIGHTSENSOR 5</span>
<span style="color: #339933;">#define BUMPSENSOR 2</span>
<span style="color: #339933;">#define TOPSPEED 200</span>
&nbsp;
<span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> valLefat<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> valRight<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> valCenter<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> oldLeft <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> oldRight <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> sensitivity <span style="color: #339933;">=</span> <span style="color: #0000dd;">40</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> threshold <span style="color: #339933;">=</span> <span style="color: #0000dd;">50</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> topThreshold <span style="color: #339933;">=</span> <span style="color: #0000dd;">650</span><span style="color: #339933;">;</span>
boolean runAway <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> leftReadings<span style="color: #009900;">&#91;</span>NUMREADINGS<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> rightReadings<span style="color: #009900;">&#91;</span>NUMREADINGS<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> index <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>                            <span style="color: #666666; font-style: italic;">// the index of the current reading</span>
<span style="color: #993333;">int</span> leftTotal <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>                            <span style="color: #666666; font-style: italic;">// the running total</span>
<span style="color: #993333;">int</span> leftAverage <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>                          <span style="color: #666666; font-style: italic;">// the average</span>
<span style="color: #993333;">int</span> rightTotal <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>                            <span style="color: #666666; font-style: italic;">// the running total</span>
<span style="color: #993333;">int</span> rightAverage <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>                          <span style="color: #666666; font-style: italic;">// the average</span>
&nbsp;
AF_DCMotor rightMotor<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> MOTOR12_8KHZ<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// create motor #2, 64KHz pwm</span>
AF_DCMotor leftMotor<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> MOTOR12_8KHZ<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// create motor #1, 64KHz pwm</span>
&nbsp;
<span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  Serial.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">9600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  rightMotor.<span style="color: #202020;">setSpeed</span><span style="color: #009900;">&#40;</span>TOPSPEED<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  leftMotor.<span style="color: #202020;">setSpeed</span><span style="color: #009900;">&#40;</span>TOPSPEED<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  pinMode<span style="color: #009900;">&#40;</span>BUMPSENSOR<span style="color: #339933;">,</span> INPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  moveForward<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> NUMREADINGS<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    leftReadings<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>                      <span style="color: #666666; font-style: italic;">// initialize all the readings to 0</span>
    rightReadings<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  checkForBump<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  averageReadings<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  checkLightandMove<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> checkLightandMove<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>valLeft <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> threshold <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> valRight <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> threshold<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>valLeft <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> valRight<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>valLeft <span style="color: #339933;">-</span> valRight <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> sensitivity<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>runAway <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        turnRight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
        turnLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>valLeft <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> valRight<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>valRight <span style="color: #339933;">-</span> valLeft <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> sensitivity<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>runAway <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        turnLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
        turnRight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
        moveForward<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//turnAround();</span>
    stop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>valLeft <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> topThreshold <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> valRight <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> topThreshold<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    runAway <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #666666; font-style: italic;">//delay(500);</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> checkForBump<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> bumped <span style="color: #339933;">=</span> digitalRead<span style="color: #009900;">&#40;</span>BUMPSENSOR<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>digitalRead<span style="color: #009900;">&#40;</span>BUMPSENSOR<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>bumped <span style="color: #339933;">==</span> HIGH<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    turnAround<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
boolean foundSource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> oldLeft <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> valLeft <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> oldRight <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> valRight<span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//the robot has reached the source of the light, or the point of maximum brightness</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> moveForward<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Move Forward&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  rightMotor.<span style="color: #202020;">run</span><span style="color: #009900;">&#40;</span>FORWARD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  leftMotor.<span style="color: #202020;">run</span><span style="color: #009900;">&#40;</span>FORWARD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> speedUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">==</span>TOPSPEED<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    rightMotor.<span style="color: #202020;">setSpeed</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    leftMotor.<span style="color: #202020;">setSpeed</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> slowToStop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span>TOPSPEED<span style="color: #339933;">;</span> i<span style="color: #339933;">==</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    rightMotor.<span style="color: #202020;">setSpeed</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    leftMotor.<span style="color: #202020;">setSpeed</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  rightMotor.<span style="color: #202020;">run</span><span style="color: #009900;">&#40;</span>RELEASE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  leftMotor.<span style="color: #202020;">run</span><span style="color: #009900;">&#40;</span>RELEASE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> turnLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Turn Left&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  rightMotor.<span style="color: #202020;">run</span><span style="color: #009900;">&#40;</span>BACKWARD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  leftMotor.<span style="color: #202020;">run</span><span style="color: #009900;">&#40;</span>FORWARD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> turnRight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Turn Right&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  rightMotor.<span style="color: #202020;">run</span><span style="color: #009900;">&#40;</span>FORWARD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  leftMotor.<span style="color: #202020;">run</span><span style="color: #009900;">&#40;</span>BACKWARD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> stop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Stop&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  rightMotor.<span style="color: #202020;">run</span><span style="color: #009900;">&#40;</span>RELEASE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  leftMotor.<span style="color: #202020;">run</span><span style="color: #009900;">&#40;</span>RELEASE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> turnAround<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   slowToStop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   moveBackward<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   turnLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   stop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #666666; font-style: italic;">//runAway = true;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> moveBackward<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Move Backward&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  rightMotor.<span style="color: #202020;">run</span><span style="color: #009900;">&#40;</span>RELEASE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  leftMotor.<span style="color: #202020;">run</span><span style="color: #009900;">&#40;</span>RELEASE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  rightMotor.<span style="color: #202020;">run</span><span style="color: #009900;">&#40;</span>BACKWARD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  leftMotor.<span style="color: #202020;">run</span><span style="color: #009900;">&#40;</span>BACKWARD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> averageReadings<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  leftTotal <span style="color: #339933;">-=</span> leftReadings<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>               <span style="color: #666666; font-style: italic;">// subtract the last reading</span>
  leftReadings<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>LEFTSENSOR<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// read from the sensor</span>
  leftTotal <span style="color: #339933;">+=</span> leftReadings<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>               <span style="color: #666666; font-style: italic;">// add the reading to the total</span>
&nbsp;
  rightTotal <span style="color: #339933;">-=</span> rightReadings<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>               <span style="color: #666666; font-style: italic;">// subtract the last reading</span>
  rightReadings<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>RIGHTSENSOR<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// read from the sensor</span>
  rightTotal <span style="color: #339933;">+=</span> rightReadings<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>               <span style="color: #666666; font-style: italic;">// add the reading to the total</span>
&nbsp;
  index <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>index <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                    <span style="color: #666666; font-style: italic;">// advance to the next index</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>index <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span> NUMREADINGS<span style="color: #009900;">&#41;</span>               <span style="color: #666666; font-style: italic;">// if we're at the end of the array...</span>
    index <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>                            <span style="color: #666666; font-style: italic;">// ...wrap around to the beginning</span>
&nbsp;
  valLeft <span style="color: #339933;">=</span> leftTotal <span style="color: #339933;">/</span> NUMREADINGS<span style="color: #339933;">;</span>
  valRight <span style="color: #339933;">=</span> rightTotal <span style="color: #339933;">/</span> NUMREADINGS<span style="color: #339933;">;</span>
&nbsp;
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>valLeft<span style="color: #339933;">,</span> DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// prints the left sensor value</span>
  Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; | &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>valRight<span style="color: #339933;">,</span> DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// prints the right sensor value</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.robotishappy.com/2009/12/light-seeking-robot-using-the-arduino/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

