As we enjoy shooting out Nerf Blasters, I thought I would make a shooting range from Lego. The motors of Lego Mindstorm NXT have build in rotation detection, so they can be used and output an input. First the program at random picks one of the 3 motors and placed the target up. Then using the rotation detection it will detect a 'hit'. This happens with the slightest movement (1 degree or more). When the hit is detected the motor turn the target all the way down and a new random target is selected.

The Lego code (designed in Bricx Command Center):

#define Power 75
#define DownTime 200
#define UpTime 200
#define TimePerTarget 2000

{

long rotate; /* the variable for recording angle */
long nothit;
int welke;
int m;
int score;
long time,time2;

TextOut(0,LCD_LINE2,"Angle on PortA");
score =0;
TextOut(0,LCD_LINE7,"Score:");
NumOut(40,LCD_LINE7,score);

while (true) {

OnFwd(OUT_ABC,Power);
Wait(DownTime);
Off(OUT_ABC);

Wait(2000);

welke = Random(3);
NumOut(0,LCD_LINE4,welke);

if (welke ==0) { m=OUT_A; }
if (welke ==1) { m=OUT_B; }
if (welke ==2) { m=OUT_C; }

OnRev(m,Power);
Wait(UpTime);
Off(m);

Wait(400);
nothit = MotorRotationCount (m);
rotate = nothit;
NumOut(0,LCD_LINE6,nothit);

time = CurrentTick();

while (nothit == rotate) {
rotate=MotorRotationCount (m); /* Read Motor angle [deg]*/
TextOut(0,LCD_LINE5," ");
NumOut(0,LCD_LINE5,rotate);
TextOut(40,LCD_LINE5,"deg");

//if ((time+TimePerTarget) < CurrentTick())
// { rotate = rotate + 10; }

}

OnFwd(m,Power);
Wait(DownTime);
Off(m);

score = score +1;

TextOut(0,LCD_LINE7,"Score:");
NumOut(40,LCD_LINE7,score);

}
}

The code is not completely finished yet. I already created a push button, so I can build a start/stop of the game. And I need to make the screen display nicer. But the listed code does already work for you to play with.
This instructable looks awesome....can't wait to try it. My son and I are novices when it comes to programming mindstorm. How do I take this code and put it in the program? <br> <br>Thanks for your help. <br> <br>Scott <br>the5evoys@frontier.com
Hi, <br> <br>I almost never program using the visual language that comes with the lego nxt as I always get massively frustrated with it :-) <br> <br>I use Bricx command center. It is not too complicated, just different software and uses a more normal style of programming. You can download it from here: http://bricxcc.sourceforge.net/ <br> <br>They have all the instruction on the site hw to install it. <br> <br>When you have the software installed, just create a new program and copy my code in it and send it to the nxt. <br> <br>Hope you can work it out, if not feel free to ask me again. <br> <br>Regards, <br>Richard