Shooting Range Made From Lego Mindstorm NXT

5,040

19

3

Published

Introduction: Shooting Range Made From Lego Mindstorm NXT

About: Hi, I am Richard, Father of 1 son + 2 step sons, so we have a house full of Lego. I have had lego for decades, including Lego mindstorms. I like to make things. In my little garage I have a large CNC machin...
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.

http://www.youtube.com/watch?v=E1wt5ywUo9M
 
The Lego code (designed in Bricx Command Center):

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

task main()
{

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);

   }
}

Share

    Recommendations

    • Make it Move Contest

      Make it Move Contest
    • Woodworking Contest

      Woodworking Contest
    • Clocks Contest

      Clocks Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    3 Comments

    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.

    2 replies

    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?

    Thanks for your help.

    Scott
    the5evoys@frontier.com

    Hi,

    I almost never program using the visual language that comes with the lego nxt as I always get massively frustrated with it :-)

    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/

    They have all the instruction on the site hw to install it.

    When you have the software installed, just create a new program and copy my code in it and send it to the nxt.

    Hope you can work it out, if not feel free to ask me again.

    Regards,
    Richard