This is my robot that is programmed to hunt and kill its sworn enemy a red plastic cup it is the ballshooter build from the mindstorms NXT 2.0 set but with a little add on the NXTcam from Mindsensors.com it is a special camera for the Mindstorms brick that can return x,y coordinates and area of a pre-programmed color in this case it is Red. The program tells the robot to find the biggest red object and give me its coordinates, through that it determines whether the cup is to the left or right of the robot and moves accordingly. Once the robot has decided it is on target it adjusts for distance by backing up or going forwards. It can give a relative distance (But in no way accurate) by the objects area then once its lined  up and at the correct distance it checks to see if the object has moved again then if it hasn't it fires!

I also intend to comment my code and post it soon.
import java.awt.Point;<br> import java.awt.Rectangle;<br> import lejos.nxt.Button;<br> import lejos.nxt.LCD;<br> import lejos.nxt.Motor;<br> import lejos.nxt.SensorPort;<br> import lejos.nxt.Sound;<br> import lejos.nxt.addon.NXTCam;<br> <br> <br> public class follow {<br> <br> <br> &nbsp;public static void main(String[] args)throws Exception {<br> <br> &nbsp; int objnum = 0;<br> &nbsp; Point LOC;<br> &nbsp; int objA = 0;<br> &nbsp; int width = 0;<br> &nbsp; int height = 0;<br> &nbsp; NXTCam cam = new NXTCam(SensorPort.S2);<br> &nbsp; Sound.beep();<br> &nbsp; int X0 = 0;<br> &nbsp; int dis1 = 0;<br> &nbsp; float dis = 0;<br> &nbsp; long start = 0;<br> &nbsp; long total = 0;<br> &nbsp; Motor.A.setSpeed(200);<br>
Motor.B.setSpeed(200);<br> int shots = 0;<br> cam.sendCommand('A');<br> cam.sendCommand('E');<br> start = System.currentTimeMillis();<br> while(!Button.ESCAPE.isPressed()){<br> Rectangle rect = cam.getRectangle(0);<br> objnum = cam.getNumberOfObjects();<br> if (objnum != 0){<br> width = rect.width;<br> height = rect.height;<br> objA = width * height;<br> LCD.drawString(&quot;x: &quot;, 0, 0);<br> LOC = rect.getLocation();<br> X0 = (int) (LOC.x * 1);<br> LCD.drawInt(X0, 4, 0);<br> LCD.drawString(&quot;A: &quot;, 0, 1);<br> LCD.drawInt(objA, 4, 1);<br> if (shots &lt;= 6){<br> LCD.drawInt(7-shots,0 , 2);<br> LCD.drawString(&quot;out of 7 shots left&quot;, 2, 2);<br> }<br> else{<br> LCD.drawString(&quot;out of ammo&quot;, 0, 2);<br> }<br> LCD.drawString(&quot;Aprox. &quot;,0, 4);<br> dis = objA / 100;<br> dis1 = (int) (dis * 1);<br> LCD.drawInt(dis1,8,4);<br> LCD.drawString(&quot;in. away&quot;,0,5);<br> if (1==1){<br> if (X0 == 0) {<br> Motor.B.stop();<br> Motor.A.stop();<br> Sound.beep();<br> }<br> else if (X0 &lt; 90 &amp;&amp; X0 &gt; 75) {<br> Motor.B.stop();<br> Motor.A.stop();<br> if (objA &lt; 1400 &amp;&amp; objA &gt; 1200){<br> Motor.B.stop();<br> Motor.A.stop();<br> <br> total = System.currentTimeMillis()-start;<br> if (total&gt;1000){<br> if (shots &lt;= 6){<br> Sound.beep();<br> Sound.beep();<br> start = System.currentTimeMillis();<br> Motor.C.rotateTo(360);<br> shots = shots + 1;<br> Motor.C.resetTachoCount();<br> }<br> }<br> }<br> else if (objA &gt; 1200 || objA &lt; 1000){<br> start = System.currentTimeMillis();<br> if (objA &gt; 1200){<br> <br> Motor.B.backward();<br> Motor.A.backward();<br> }<br> else if (objA &lt; 900){<br> <br> Motor.A.forward();<br> Motor.B.forward();<br> }<br> }<br> }<br> else if (X0 &gt; 90) {<br> start = System.currentTimeMillis();<br> Motor.B.backward();<br> Motor.A.forward();<br> }<br> else if (X0 &lt; 75) {<br> start = System.currentTimeMillis();<br> Motor.B.forward();<br> Motor.A.backward();<br> }<br> <br> Thread.sleep(100);<br> LCD.clear();<br> }<br> }<br> }<br> }<br>}<br>

About This Instructable




More by Longboarding_Robot:Autonomous NXT Ballshooter w/ NXTcam 
Add instructable to: