237
12/30/12 NXT 2.0 Instructions 1/237 arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program Categories BASH C Celestial Lego Various Building guide Programming guide Test guide Building guide Programming guide Test guide Building guide Programming guide Test guide Building guide Programming guide Test guide Building guide Programming guide Test guide Building guide Programming guide Test guide Building guide Programming guide Test guide Building guide Programming guide Test guide Building guide Programming guide Test guide Building guide Programming guide Test guide Building guide Programming guide Test guide Building guide Programming guide Test guide Building guide Programming guide Test guide Building guide Programming guide Test guide Building guide Programming guide Test guide Building guide Programming guide Test guide Contact Home Search LEGO Mindstorms NXT 2.0 instructions Instructions for building the Shooterbot, Robogator, Color Sorter, and Alpha Rex. Table of contents Introduction Programming the NXT with Linux Compiling an NXC program Shooterbot 1. Driving base 2. Color detection 3. Shooter 4. Locate objects Robogator 1. Jaws 2. Eyes 3. Legs 4. Tail Color Sorter 1. Dispenser 2. Color detector 3. Sorting tray 4. Color catapult Alpha Rex 1. Legs 2. Arms 3. Head 4. Color sensor Introduction The instructions for building the four main LEGO Mindstorms NXT 2.0 models are distributed only on software that does not support Linux. The instructions are reproduced here. Programming the NXT with Linux The accompanying programs supplied here are written in NXC. NXC is a simple C-like programming language supported by NBC. NBC allows you to write NXT programs and download them to the NXT. This configuration guide for NBC on Linux is useful. Compiling an NXC program A typical command to compile and download an NXC program by USB would be nbc -d -S=usb filename.nxc Shooterbot Can you build and program a Shooterbot to guard your room while you are away? 1. Driving base Building guide

NXT 2.0 Instructions

  • Upload
    saipol

  • View
    3.613

  • Download
    5

Embed Size (px)

DESCRIPTION

NXT 2.0 Instructions

Citation preview

Page 1: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

1/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Categories BASH C Celestial Lego Various

Building guide Programming guide Test guideBuilding guide Programming guide Test guideBuilding guide Programming guide Test guideBuilding guide Programming guide Test guide

Building guide Programming guide Test guideBuilding guide Programming guide Test guideBuilding guide Programming guide Test guideBuilding guide Programming guide Test guide

Building guide Programming guide Test guideBuilding guide Programming guide Test guideBuilding guide Programming guide Test guideBuilding guide Programming guide Test guide

Building guide Programming guide Test guideBuilding guide Programming guide Test guideBuilding guide Programming guide Test guideBuilding guide Programming guide Test guide

Contact Home Search

LEGO Mindstorms NXT 2.0 instructions

Instructions for building the Shooterbot, Robogator, Color Sorter, and Alpha Rex.

Table of contents

IntroductionProgramming the NXT with LinuxCompiling an NXC program

Shooterbot1. Driving base2. Color detection3. Shooter4. Locate objects

Robogator1. Jaws2. Eyes3. Legs4. Tail

Color Sorter1. Dispenser2. Color detector3. Sorting tray4. Color catapult

Alpha Rex1. Legs2. Arms3. Head4. Color sensor

Introduction

The instructions for building the four main LEGO Mindstorms NXT 2.0 models are distributed only on software that doesnot support Linux. The instructions are reproduced here.

Programming the NXT with Linux

The accompanying programs supplied here are written in NXC. NXC is a simple C-like programming languagesupported by NBC. NBC allows you to write NXT programs and download them to the NXT. This configuration guide forNBC on Linux is useful.

Compiling an NXC program

A typical command to compile and download an NXC program by USB would be

nbc -d -S=usb filename.nxc

Shooterbot

Can you build and program a Shooterbot to guard your room while you are away?

1. Driving base

Building guide

Page 2: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

2/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Can you build a Shooterbot to move forward and then move back again?

Page 3: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

3/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 4: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

4/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 5: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

5/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 6: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

6/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 7: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

7/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 8: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

8/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 9: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

9/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 10: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

10/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 11: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

11/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 12: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

12/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you program a Shooterbot to move forward on the test pad and then move in reverse to its start position?

// shooterbot_1.nxc// Move forward and then reverse.

task main (){ // Move forward at 75% power for 2 seconds OnFwd (OUT_BC ,75); Wait (2000);

// Reverse at 75% power for 2 seconds OnRev (OUT_BC ,75); Wait (2000);

// Brake Off (OUT_BC);

}

Page 13: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

13/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

}

Test guide

Download your program. Place the Shooterbot on the test pad and run the program.

2. Color detection

Building guide

Can you build a Shooterbot to test colors on the test pad?

Page 14: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

14/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you program a Shooterbot to move forward, find the red line, turn around, and then move back to its start

position?

// shooterbot_2.nxc// Move forward to the red line, turn around, and move back to the start// position.

// Adjust this number to suit your surface / battery power#define TURN_TIME 1750

task main (){ long t0, t1;

// Bring the color sensor on-line

Page 15: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

15/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

// Bring the color sensor on-line SetSensorColorFull (S3);

// Record the time when the forward movement starts t0 = CurrentTick ();

// Move forward to the red line OnFwd (OUT_BC ,75); until (INPUT_REDCOLOR == SENSOR_3); t1 = CurrentTick (); // Record the time when the forward movement finishes Off (OUT_BC);

// Turn 180 degrees to the right OnFwd (OUT_C, 75); OnRev (OUT_B, 75); Wait (TURN_TIME); Off (OUT_BC);

// Move back to the start OnFwd (OUT_BC ,75); Wait (t1 - t0); Off (OUT_BC);}

Test guide

Download your program. Place the Shooterbot on the test pad and run the program.

3. Shooter

Building guide

Can you build the Shooterbot to shoot balls at objects? You will have to build the shooter on the front of your

Shooterbot.

Page 16: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

16/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 17: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

17/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 18: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

18/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 19: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

19/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 20: NXT 2.0 Instructions
Page 21: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

21/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 22: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

22/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you program Shooterbot to drive around and shoot while showing different colored lights?

// shooterbot_3.nxc// Drive around and shoot while showing different colored lights.

#define BULLETS 7#define FLASH_DURATION 300#define TURN_TIME 3000

int bullets_remaining = BULLETS;bool fire = false;

task flashing_lights (){ int red_flashes;

while (bullets_remaining > 0) { // Alternate between the red, green, and blue lamps SetSensorColorRed (IN_3); Wait (FLASH_DURATION); SetSensorColorGreen (IN_3); Wait (FLASH_DURATION); SetSensorColorBlue (IN_3); Wait (FLASH_DURATION); }

// Indicate that no bullets remain for (red_flashes = 1; red_flashes <= 10; red_flashes++) { SetSensorColorRed (IN_3); Wait (FLASH_DURATION / 2); SetSensorColorNone (IN_3); Wait (FLASH_DURATION / 2); }

// Stop the program StopAllTasks ();}

task shoot (){ while (true) { // Check whether to fire a bullet if (true == fire) { // Reset the 'fire a bullet' flag to false fire = false;

Page 23: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

23/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

// Decrement the number of bullets remaining bullets_remaining--;

// To fire, make motor A do a single 360 degree rotation RotateMotorEx (OUT_A, 75, 360, 0, false, true); } }}

task move (){ while (bullets_remaining > 0) { // Do a random turn if (0 == Random (2)) { // Left turn OnFwd (OUT_B, 75); OnRev (OUT_C, 75); Wait (Random (TURN_TIME)); Off (OUT_BC); Wait (1000); fire = true; Wait (1000); } else { // Right turn OnFwd (OUT_C, 75); OnRev (OUT_B, 75); Wait (Random (TURN_TIME)); Off (OUT_BC); Wait (1000); fire = true; Wait (1000); } }

}

task main (){ // Make these tasks execute simultaneously after main () is finished Precedes (flashing_lights, shoot, move);}

Test guide

Download the program. Place the Shooterbot in the middle of the test pad and run the program.

4. Locate objects

Building guide

Can you build the Shooterbot to protect your room? You will have to add the Ultrasonic Sensor to your Shooterbot.

Page 24: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

24/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you program the Shooterbot to scan your room and detect objects? If an object is closer than 40cm, the

Shooterbot should shine a warning light and then shoot if the object does not move away.

// shooterbot_4.nxc// If an object is to close, shine a warning light and then shoot if// the object does not move away.

#define BULLETS 7#define FLASH_DURATION 200#define THRESHOLD 40#define WARNING_TIME 4000

#define start_turning(power) OnFwd (OUT_C, power); OnRev (OUT_B, power);

task main ()

Page 25: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

25/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

task main (){ long t0; int bullets_remaining = BULLETS;

SetSensorLowspeed (S4); // Bring the ultrasonic sensor on-line SetSensorColorGreen (S3); // Set the lamp to show all-clear

while (bullets_remaining > 0) { start_turning (50); until (SensorUS (S4) < THRESHOLD); // Scan for close objects Off (OUT_BC); // Stop turning t0 = CurrentTick ();

while ((CurrentTick () - t0) < WARNING_TIME) { // Emit the warning - a flashing red lamp SetSensorColorNone (S3); Wait (FLASH_DURATION); SetSensorColorRed (S3); Wait (FLASH_DURATION); }

if (SensorUS (S4) < THRESHOLD) { bullets_remaining--; RotateMotorEx (OUT_A, 75, 360, 0, false, true); // Shoot Wait (1000); // Give motor A time to shoot }

SetSensorColorGreen (S3); // Set the lamp to show all-clear }

Off (OUT_BC); // Stop turning TextOut (8, LCD_LINE4, "OUT OF BULLETS", true); Wait (7000); StopAllTasks (); // Stop the program}

Test guide

Download the program. Place the Shooterbot in the middle of the test pad and run the program.

Robogator

Can you build and program a Robogator that attacks objects and protects its territory?

1. Jaws

Building guide

Can you build a robot with jaws that open and close?

Page 26: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

26/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 27: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

27/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 28: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

28/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 29: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

29/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 30: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

30/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 31: NXT 2.0 Instructions
Page 32: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

32/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 33: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

33/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 34: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

34/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 35: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

35/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you program your robot to open and close its jaws?

// robogator_1.nxc// Open and close the jaws

// Sample the motor rotation speed every this number of milliseconds#define ROTATION_SAMPLE_RESOLUTION 50

// The approximate 'degrees per second' rotation of motor Along dps_a = 0;

task motor_speed_monitor (){ long mrc_a_0, mrc_a_1; // mrc == motor rotation count unsigned long t_a_0, t_a_1;

Page 36: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

36/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

while (true) { // Record the rotation count and the time mrc_a_0 = MotorRotationCount (OUT_A); t_a_0 = CurrentTick ();

Wait (ROTATION_SAMPLE_RESOLUTION);

// Again, record the rotation count and the time mrc_a_1 = MotorRotationCount (OUT_A); t_a_1 = CurrentTick ();

// Calculate the average rotation rate over that time interval dps_a = ((mrc_a_1 - mrc_a_0) * 1000) / (t_a_1 - t_a_0); }}

task bite (){ while (true) { // Assume the jaws are closed to begin with - open them OnFwd (OUT_A, 40); Wait (100); // Wait > ROTATION_SAMPLE_RESOLUTION until (10 > dps_a); // Wait until the jaws have opened and are practically still Off (OUT_A);

// Close the jaws OnRev (OUT_A, 40); Wait (100); until (-10 < dps_a); // Reverse rotations are negative Off (OUT_A); }}

task main (){ Precedes (bite, motor_speed_monitor);}

Test guide

Download and run the program. Your Robogator jaws should open and close.

2. Eyes

Building guide

Can you make your Robogator see objects and attack them if they get too close?

Page 37: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

37/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 38: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

38/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you program your robot to bite when objects get too close?

// robogator_2.nxc// Open and close the jaws when objects are close

// Sample the motor rotation speed every this number of milliseconds#define ROTATION_SAMPLE_RESOLUTION 50

// Bite objects which come closer than this#define THRESHOLD 40

// The approximate 'degrees per second' rotation of motor Along dps_a = 0;

task motor_speed_monitor ()

{

Page 39: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

39/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

{ long mrc_a_0, mrc_a_1; // mrc == motor rotation count unsigned long t_a_0, t_a_1;

while (true) { mrc_a_0 = MotorRotationCount (OUT_A); t_a_0 = CurrentTick ();

Wait (ROTATION_SAMPLE_RESOLUTION);

mrc_a_1 = MotorRotationCount (OUT_A); t_a_1 = CurrentTick ();

dps_a = ((mrc_a_1 - mrc_a_0) * 1000) / (t_a_1 - t_a_0); }}

task bite (){ SetSensorLowspeed (S4); // Bring the ultrasonic sensor on-line

while (true) { until (SensorUS (S4) < THRESHOLD); // Scan for close objects

// Assume the jaws are closed to begin with - open them OnFwd (OUT_A, 40); Wait (100); // Wait > ROTATION_SAMPLE_RESOLUTION until (10 > dps_a); // Wait until the motor stops Off (OUT_A);

// Close the jaws OnRev (OUT_A, 40); Wait (100); until (-10 < dps_a); // Reverse rotations are negative Off (OUT_A); }}

task main (){ Precedes (bite, motor_speed_monitor);}

Test guide

Download and run the program. Move your hand towards the Robogator's eyes. The Robogator's jaws shouldstart biting. Move your hand away and the Robogator should stop biting.

3. Legs

Building guide

Can you build a robot that can walk?

Page 40: NXT 2.0 Instructions
Page 41: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

41/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 42: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

42/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 43: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

43/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 44: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

44/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 45: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

45/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 46: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

46/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 47: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

47/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 48: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

48/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 49: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

49/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 50: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

50/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 51: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

51/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 52: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

52/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 53: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

53/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 54: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

54/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 55: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

55/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 56: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

56/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 57: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

57/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 58: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

58/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 59: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

59/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 60: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

60/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 61: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

61/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 62: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

62/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 63: NXT 2.0 Instructions
Page 64: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

64/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 65: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

65/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 66: NXT 2.0 Instructions
Page 67: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

67/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 68: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

68/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 69: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

69/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 70: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

70/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 71: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

71/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 72: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

72/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you program the Robogator to walk forward and start biting when objects get too close?

// robogator_3.nxc// Open and close the jaws and walk forward when objects are close

// Sample the motor rotation speed every this number of milliseconds#define ROTATION_SAMPLE_RESOLUTION 50

// Bite objects which come closer than this#define THRESHOLD 40

// Walk forward for up to this many steps when an object is close#define MAX_STEPS_FORWARD 3

// The approximate 'degrees per second' rotation of motor Along dps_a = 0;

task motor_speed_monitor (){ long mrc_a_0, mrc_a_1; // mrc == motor rotation count unsigned long t_a_0, t_a_1;

while (true) { mrc_a_0 = MotorRotationCount (OUT_A); t_a_0 = CurrentTick ();

Wait (ROTATION_SAMPLE_RESOLUTION);

mrc_a_1 = MotorRotationCount (OUT_A); t_a_1 = CurrentTick ();

dps_a = ((mrc_a_1 - mrc_a_0) * 1000) / (t_a_1 - t_a_0); }}

task bite (){ while (true) { until (SensorUS (S4) < THRESHOLD); // Scan for close objects

// Assume the jaws are closed to begin with OnFwd (OUT_A, 40); Wait (100); // Wait > ROTATION_SAMPLE_RESOLUTION until (10 > dps_a); // Wait until the motor stops Off (OUT_A);

OnRev (OUT_A, 40); Wait (100);

Page 73: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

73/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

OnRev (OUT_A, 40); Wait (100); until (-10 < dps_a); // Reverse rotations are negative Off (OUT_A); }}

task walk (){ int steps;

while (true) { until (SensorUS (S4) < THRESHOLD); // Scan for close objects

// Walk forward while an object is close steps = 0; while (SensorUS (S4) < THRESHOLD && steps < MAX_STEPS_FORWARD) { RotateMotorEx (OUT_BC, -100, 360, 0, true, true); steps += 1; } }}

task main (){ SetSensorLowspeed (S4); // Bring the ultrasonic sensor on-line

Precedes (bite, motor_speed_monitor, walk);}

Test guide

Download the program. Place your alligator on the test pad and run the program.

4. Tail

Building guide

Can you build the tail and add sensors to the Robogator so it knows where its legs are?

Page 74: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

74/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 75: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

75/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 76: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

76/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 77: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

77/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 78: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

78/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 79: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

79/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 80: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

80/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 81: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

81/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you program Robogator to protect its territory: lunge forward, bite, and crawl back?

// robogator_4.nxc// When an object is close: open and close the jaws, walk forward,// and then move back

// Sample the motor rotation speed every this number of milliseconds#define ROTATION_SAMPLE_RESOLUTION 50

// Bite objects which come closer than this#define THRESHOLD 60

// Walk forward for up to this many steps when an object is close#define MAX_STEPS_FORWARD 3

// The approximate 'degrees per second' rotation of motor A

Page 82: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

82/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

// The approximate 'degrees per second' rotation of motor Along dps_a = 0;

task motor_speed_monitor (){ long mrc_a_0, mrc_a_1; // mrc == motor rotation count unsigned long t_a_0, t_a_1;

while (true) { mrc_a_0 = MotorRotationCount (OUT_A); t_a_0 = CurrentTick ();

Wait (ROTATION_SAMPLE_RESOLUTION);

mrc_a_1 = MotorRotationCount (OUT_A); t_a_1 = CurrentTick ();

dps_a = ((mrc_a_1 - mrc_a_0) * 1000) / (t_a_1 - t_a_0); }}

task bite (){ while (true) { until (SensorUS (S4) < THRESHOLD); // Scan for close objects

// Assume the jaws are closed to begin with OnFwd (OUT_A, 40); Wait (100); // Wait > ROTATION_SAMPLE_RESOLUTION until (10 > dps_a); // Wait until the motor stops Off (OUT_A); OnRev (OUT_A, 40); Wait (100); until (-10 < dps_a); // Reverse rotations are negative Off (OUT_A); }}

task walk (){ int i, steps;

// Use the touch sensors to align the legs initially OnFwd (OUT_B, -100); until (1 == SENSOR_1); Off (OUT_B); RotateMotor (OUT_B, -100, 45);

OnFwd (OUT_C, -100); until (1 == SENSOR_2); Off (OUT_C); RotateMotor (OUT_C, -100, 45);

while (true) { // Walk forward while an object is close steps = 0; while (SensorUS (S4) < THRESHOLD && steps < MAX_STEPS_FORWARD) { RotateMotorEx (OUT_BC, -100, 360, 0, true, true); steps += 1; }

// Walk back to the original position Wait (1000); while (steps > 0) { RotateMotorEx (OUT_BC, 90, 360, 0, true, true); steps -= 1; } }

}

task main (){ SetSensorLowspeed (S4); // Bring the ultrasonic sensor on-line SetSensorTouch (S1); // Bring the touch sensors on-line SetSensorTouch (S2);

Page 83: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

83/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Precedes (bite, motor_speed_monitor, walk);}

Test guide

Download the program. Place Robogator on the test pad and run the program.

Color Sorter

Can you build and program the Color Sorter to sort balls by color?

1. Dispenser

Building guide

Can you build a dispenser that separates balls one by one?

Page 84: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

84/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 85: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

85/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 86: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

86/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 87: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

87/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 88: NXT 2.0 Instructions
Page 89: NXT 2.0 Instructions
Page 90: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

90/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 91: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

91/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 92: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

92/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 93: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

93/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 94: NXT 2.0 Instructions
Page 95: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

95/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 96: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

96/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 97: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

97/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 98: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

98/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 99: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

99/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 100: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

100/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 101: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

101/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 102: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

102/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 103: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

103/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 104: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

104/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 105: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

105/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 106: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

106/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 107: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

107/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 108: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

108/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 109: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

109/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 110: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

110/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 111: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

111/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 112: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

112/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 113: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

113/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 114: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

114/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 115: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

115/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 116: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

116/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 117: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

117/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 118: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

118/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 119: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

119/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 120: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

120/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 121: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

121/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 122: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

122/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 123: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

123/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 124: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

124/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 125: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

125/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 126: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

126/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 127: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

127/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 128: NXT 2.0 Instructions
Page 129: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

129/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 130: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

130/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 131: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

131/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 132: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

132/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 133: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

133/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 134: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

134/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you program the dispenser to tilt and dispense one ball at a time?

// colorsorter_1.nxc// Dispense one ball at a time

// The number of balls to be sorted#define BALLS 4

// Get the dispenser in a standard positionvoid reset_dispenser (){ OnRev (OUT_B, 80); until (1 == SENSOR_1); Off (OUT_B); Wait (1000);}

// Dispense one ballvoid dispense_ball (){ reset_dispenser (); RotateMotor (OUT_B, 60, 720); Wait (1000);}

task main (){ int i;

// Bring the tilting mechanism's touch sensor on-line SetSensorTouch (S1);

i = 0; while (i++ < BALLS) { dispense_ball (); }

}

Test guide

Page 135: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

135/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Download your program. Place the Color Sorter in the middle of the test pad. Put a couple of balls in thedispenser and run the program.

2. Color detector

Building guide

Can you build a color detector that can recognise the different ball colors?

Page 136: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

136/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 137: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

137/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you make a program that can tell the difference between the colors of the balls?

// colorsorter_2.nxc// Dispense one ball at a time - display the color of each ball

// The number of balls to be sorted#define BALLS 4

// Get the dispenser in a standard positionvoid reset_dispenser (){ OnRev (OUT_B, 80); until (1 == SENSOR_1); Off (OUT_B); Wait (1000);

}

Page 138: NXT 2.0 Instructions

}

// Get a ball into the dispenser slotvoid load_ball (){ RotateMotor (OUT_B, 60, 360); Wait (1000);}

void dispense_ball (){ RotateMotor (OUT_B, 60, 360); Wait (1000);}

void show_color (){ string ball_color = "";

switch (SENSOR_3) { case INPUT_BLUECOLOR: ball_color = "BLUE"; break; case INPUT_REDCOLOR: ball_color = "RED"; break; case INPUT_GREENCOLOR: ball_color = "GREEN"; break; case INPUT_YELLOWCOLOR: ball_color = "YELLOW"; break; default: ball_color = "NONE"; break; }

// Show the ball color TextOut (30, LCD_LINE3, ball_color, true); Wait (1000);}

task main (){ int i;

// Bring the tilting mechanism's touch sensor on-line SetSensorTouch (S1);

// Bring the color sensor on-line SetSensorColorFull (S3);

i = 0; while (i++ < BALLS) { // Show the number of balls left to sort TextOut (0, LCD_LINE1, "Balls remaining: ", true); NumOut (50, LCD_LINE3, BALLS - (i-1));

reset_dispenser (); load_ball (); show_color (); dispense_ball (); }

}

Test guide

Download your program. Place the Color Sorter in the middle of the test pad. Run the program.

3. Sorting tray

Page 139: NXT 2.0 Instructions

Building guide

Can you build a sorting tray that allows the Color Sorter to sort the four different colored balls into their own

basket?

Page 140: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

140/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 141: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

141/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 142: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

142/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 143: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

143/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 144: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

144/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 145: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

145/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 146: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

146/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 147: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

147/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you program the sorting tray to spin around and place balls of the same color in the same basket?

// colorsorter_3.nxc// Place balls of the same color in the same basket

// Assume there are no balls remaining after this number of retries#define RETRIES 2

// Reset the dispensing tray and the basket when these are set to truebool reset_dispenser_flag = false;bool reset_basket_flag = false;

// Get a ball into the dispenser slotvoid load_ball (){

Wait (2000);

Page 148: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

148/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Wait (2000); // Shake up the balls in the dispenser RotateMotor (OUT_B, 90, 400); RotateMotor (OUT_B, -90, 400); Wait (500);}

void dispense_ball (){ RotateMotor (OUT_B, -90, 360); Wait (1000);}

// Show the color of the ball on the NXT display// Return the value of the color of the ball to the calling taskstring show_color (){ string ball_color = "";

switch (SENSOR_3) { case INPUT_BLUECOLOR: ball_color = "BLUE"; break; case INPUT_REDCOLOR: ball_color = "RED"; break; case INPUT_GREENCOLOR: ball_color = "GREEN"; break; case INPUT_YELLOWCOLOR: ball_color = "YELLOW"; break; default: ball_color = "NONE"; break; }

// Show the ball color TextOut (30, LCD_LINE3, ball_color, true); Wait (500);

return ball_color;}

void align_basket (string ball_color){ // RED, YELLOW, GREEN, BLUE if (ball_color == "YELLOW") { RotateMotor (OUT_C, 30, 90); }else if (ball_color == "GREEN") { RotateMotor (OUT_C, 30, 180); }else if (ball_color == "BLUE") { RotateMotor (OUT_C, -30, 90); }}

// Get the dispenser in a standard positiontask reset_dispenser (){ while (true) { if (true == reset_dispenser_flag) { // Get the dispenser into a standard position using the touch sensor OnRev (OUT_B, 80); until (1 == SENSOR_1); Off (OUT_B); reset_dispenser_flag = false; } }}

task reset_basket (){ while (true)

Page 149: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

149/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

{ if (true == reset_basket_flag) { // Get the basket into a standard position using the touch sensor OnFwd (OUT_C, 30); until (0 == SENSOR_2); // Unset the touch sensor if it is set until (1 == SENSOR_2); Off (OUT_C); RotateMotor (OUT_C, -30, 30); reset_basket_flag = false; } }}

task main (){ int i; string ball_color;

// Bring the tilting mechanism's touch sensor on-line SetSensorTouch (S1); // Bring the sorting mechanism's touch sensor on-line SetSensorTouch (S2);

// Bring the color sensor on-line SetSensorColorFull (S3);

StartTask (reset_basket); StartTask (reset_dispenser);

while (true) { // Clear the display ResetScreen ();

i = 0; do { // Reset the positions of the dispenser and the baskets in parallel reset_dispenser_flag = true; reset_basket_flag = true;

// Wait until the reset tasks are both finished until (false == reset_dispenser_flag && false == reset_basket_flag);

load_ball (); // Place a ball in the dispenser exit slot ball_color = show_color ();

// Check for a ball in the dispenser exit slot } while (++i < RETRIES && "NONE" == ball_color)

if ("NONE" == ball_color) { // Assume no balls are remaining TextOut (0, LCD_LINE3, "*** FINISHED ***", true); Wait (4000); break; }

align_basket (ball_color); dispense_ball (); }

StopAllTasks ();}

Test guide

Download your program. Place the Color Sorter in the middle of the test pad. Run the program.

4. Color catapult

Building guide

Page 150: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

150/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Can you build a color catapult that throws balls in different directions?

Page 151: NXT 2.0 Instructions
Page 152: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

152/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 153: NXT 2.0 Instructions
Page 154: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

154/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 155: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

155/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 156: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

156/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 157: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

157/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 158: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

158/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 159: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

159/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 160: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

160/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you program the color catapult to throw balls of the same color in the same direction?

// colorsorter_4.nxc// Chuck balls of the same color in the same direction

// Assume there are no balls remaining after this number of retries#define RETRIES 2

// Reset the dispensing tray and the catapult when these are set to truebool reset_dispenser_flag = false;bool reset_catapult_flag = false;

// Get a ball into the dispenser slotvoid load_ball (){

Wait (2000);

Page 161: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

161/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Wait (2000); // Shake up the balls in the dispenser RotateMotor (OUT_B, 90, 400); RotateMotor (OUT_B, -90, 400); Wait (500);}

void dispense_ball (){ RotateMotor (OUT_B, -90, 360); Wait (1000);}

// Show the color of the ball on the NXT display// Return the value of the color of the ball to the calling taskstring show_color (){ string ball_color = "";

switch (SENSOR_3) { case INPUT_BLUECOLOR: ball_color = "BLUE"; break; case INPUT_REDCOLOR: ball_color = "RED"; break; case INPUT_GREENCOLOR: ball_color = "GREEN"; break; case INPUT_YELLOWCOLOR: ball_color = "YELLOW"; break; default: ball_color = "NONE"; break; }

// Show the ball color TextOut (30, LCD_LINE3, ball_color, true); Wait (500);

return ball_color;}

void align_catapult (string ball_color){ // RED, YELLOW, GREEN, BLUE if (ball_color == "RED") { RotateMotor (OUT_C, 30, 45); }else if (ball_color == "YELLOW") { RotateMotor (OUT_C, 30, 90); }else if (ball_color == "GREEN") { RotateMotor (OUT_C, -30, 45); }else if (ball_color == "BLUE") { RotateMotor (OUT_C, -30, 90); }}

// Get the dispenser in a standard positiontask reset_dispenser (){ while (true) { if (true == reset_dispenser_flag) { // Get the dispenser into a standard position using the touch sensor OnRev (OUT_B, 80); until (1 == SENSOR_1); Off (OUT_B); reset_dispenser_flag = false; } }}

task reset_catapult ()

Page 162: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

162/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

{ while (true) { if (true == reset_catapult_flag) { // Get the catapult into a standard position using the touch sensor OnFwd (OUT_C, 30); // Unset the touch sensor if it is set by moving toward the front side until (0 == SENSOR_2);

OnFwd (OUT_C, -30); // Move back until the touch sensor activates until (1 == SENSOR_2); Off (OUT_C);

RotateMotor (OUT_C, -30, 15); // A slight adjustment for perfect alignment reset_catapult_flag = false; } }}

task main (){ int i; string ball_color;

// Bring the tilting mechanism's touch sensor on-line SetSensorTouch (S1); // Bring the sorting mechanism's touch sensor on-line SetSensorTouch (S2);

// Bring the color sensor on-line SetSensorColorFull (S3);

StartTask (reset_catapult); StartTask (reset_dispenser);

while (true) { // Clear the display ResetScreen ();

i = 0; do { // Reset the positions of the dispenser and the catapult in parallel reset_dispenser_flag = true; reset_catapult_flag = true;

// Wait until the reset tasks are both finished until (false == reset_dispenser_flag && false == reset_catapult_flag);

load_ball (); // Place a ball in the dispenser exit slot ball_color = show_color ();

// Check for a ball in the dispenser exit slot } while (++i < RETRIES && "NONE" == ball_color)

if ("NONE" == ball_color) { // Assume no balls are remaining TextOut (0, LCD_LINE3, "*** FINISHED ***", true); Wait (4000); break; }

dispense_ball (); align_catapult (ball_color);

// Fire the catapult OnFwd (OUT_A, 100); Wait (500); Off (OUT_A); OnFwd (OUT_A, -100); Wait (500); Off (OUT_A);

// Move the catapult back over to the front side, ready for resetting its position if (ball_color == "GREEN") { RotateMotor (OUT_C, 30, 45+90); }else if (ball_color == "BLUE") { RotateMotor (OUT_C, 30, 90+90);

Page 163: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

163/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

}

}

StopAllTasks ();}

Test guide

Download your program. Place the Color Sorter in the middle of the test pad and make sure the axle isplaced in the centre. Run the program.

Alpha Rex

Can you build and program the Alpha Rex to walk?

1. Legs

Building guide

Can you build a pair of legs that can walk?

Page 164: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

164/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 165: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

165/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 166: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

166/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 167: NXT 2.0 Instructions
Page 168: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

168/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 169: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

169/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 170: NXT 2.0 Instructions
Page 171: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

171/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 172: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

172/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 173: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

173/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 174: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

174/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 175: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

175/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 176: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

176/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 177: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

177/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 178: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

178/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 179: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

179/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 180: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

180/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 181: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

181/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 182: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

182/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 183: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

183/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 184: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

184/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 185: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

185/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 186: NXT 2.0 Instructions
Page 187: NXT 2.0 Instructions
Page 188: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

188/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 189: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

189/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 190: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

190/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 191: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

191/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 192: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

192/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 193: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

193/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 194: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

194/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 195: NXT 2.0 Instructions
Page 196: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

196/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 197: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

197/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 198: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

198/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 199: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

199/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you build a pair of legs that test walk in place, and then walk forwards and backwards?

// alpharex_1.nxc// Walk in place, and forwards, and backwards.

// Three directions for walking#define INPLACE 0#define FORWARDS 1#define BACKWARDS 2

// Get the legs in a standard position// Motor B & touch sensor 1 ---> right leg// Motor C & touch sensor 2 ---> left legvoid reset_legs ()

{

Page 200: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

200/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

{ if (1 == SENSOR_1) { OnFwd (OUT_B, 50); Wait (300); until (0 == SENSOR_1); Off (OUT_B); } OnFwd (OUT_B, 50); until (1 == SENSOR_1); Off (OUT_B);

if (1 == SENSOR_2) { OnFwd (OUT_C, 50); Wait (300); until (0 == SENSOR_2); Off (OUT_C); } OnFwd (OUT_C, 50); until (1 == SENSOR_2); Off (OUT_C);}

void walk (int direction, unsigned long duration){ unsigned long t0;

reset_legs (); Wait (2000);

RotateMotor (OUT_B, 70, 45); // Put right leg down RotateMotor (OUT_C, -70, 150); // Put left leg up

t0 = CurrentTick ();

switch (direction) { case INPLACE:

while (CurrentTick () - t0 < duration) { OnFwd (OUT_B, 70); OnFwd (OUT_C, -70); } break;

case FORWARDS:

while (CurrentTick () - t0 < duration) { OnFwdSync (OUT_BC, 50, 0); } break;

case BACKWARDS:

while (CurrentTick () - t0 < duration) { OnRevSync (OUT_BC, 50, 0); } break; }

Off (OUT_BC);}

task main (){ // Bring the touch sensors on-line SetSensorTouch (S1); SetSensorTouch (S2);

walk (INPLACE, 5000); Wait (2000); walk (BACKWARDS, 5000); Wait (2000); walk (FORWARDS, 5000); Wait (2000);

}

Page 201: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

201/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Test guide

Download the program. Place the Alpha Rex on the test pad. Run the program.

2. Arms

Building guide

Can you build the Alpha Rex with a pair of arms and moveable hands?

Page 202: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

202/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 203: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

203/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 204: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

204/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 205: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

205/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 206: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

206/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 207: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

207/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 208: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

208/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 209: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

209/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 210: NXT 2.0 Instructions
Page 211: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

211/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 212: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

212/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 213: NXT 2.0 Instructions
Page 214: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

214/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 215: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

215/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 216: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

216/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 217: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

217/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 218: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

218/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 219: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

219/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 220: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

220/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 221: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

221/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 222: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

222/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 223: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

223/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 224: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

224/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 225: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

225/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 226: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

226/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you program the arms to move back and forth while the hands open and close?

// alpharex_2.nxc// Move the arms back and forth

task main (){ while (true) { // turn right - open left hand RotateMotor (OUT_A, 100, 2*60);

// back to center RotateMotor (OUT_A, -100, 2*60);

Wait (1000);

// turn left - open right hand RotateMotor (OUT_A, -100, 2*60);

// back to center RotateMotor (OUT_A, 100, 2*60);

Wait (1000); }}

Test guide

Download the program. Place the Alpha Rex on the test pad. Run the program.

3. Head

Building guide

Can you build the Alpha Rex to see? You will have to build a head on it!

Page 227: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

227/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 228: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

228/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 229: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

229/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 230: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

230/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 231: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

231/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 232: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

232/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Page 233: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

233/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you program the Alpha Rex to move forward when it sees an object?

// alpharex_3.nxc// Move forward when an object is visible

// Walk forward when objects come closer than this#define THRESHOLD 50

// Three directions for walking#define INPLACE 0#define FORWARDS 1#define BACKWARDS 2

// Get the legs in a standard position// Motor B & touch sensor 1 ---> right leg// Motor C & touch sensor 2 ---> left legvoid reset_legs (){ if (1 == SENSOR_1) { OnFwd (OUT_B, 50); Wait (300); until (0 == SENSOR_1); Off (OUT_B); } OnFwd (OUT_B, 50); until (1 == SENSOR_1); Off (OUT_B);

if (1 == SENSOR_2) { OnFwd (OUT_C, 50); Wait (300); until (0 == SENSOR_2); Off (OUT_C); } OnFwd (OUT_C, 50); until (1 == SENSOR_2); Off (OUT_C);}

void walk (int direction, unsigned long duration){ unsigned long t0;

RotateMotor (OUT_B, 70, 45); // Put right leg down

Page 234: NXT 2.0 Instructions

RotateMotor (OUT_B, 70, 45); // Put right leg down RotateMotor (OUT_C, -70, 150); // Put left leg up

t0 = CurrentTick ();

switch (direction) { case INPLACE:

OnFwd (OUT_B, 50); OnFwd (OUT_C, -50); until (CurrentTick () - t0 > duration); break;

case FORWARDS:

OnFwdSync (OUT_BC, 50, 0); until (CurrentTick () - t0 > duration); break;

case BACKWARDS:

OnRevSync (OUT_BC, 50, 0); until (CurrentTick () - t0 > duration); break; }

Off (OUT_BC); reset_legs ();}

task main (){ SetSensorTouch (S1); SetSensorTouch (S2); // Bring the touch sensors on-line

SetSensorLowspeed (S4); // Bring the ultrasonic sensor on-line

reset_legs (); Wait (500);

while (true) { until (SensorUS (S4) < THRESHOLD); walk (FORWARDS, 5000); Wait (2000); }

}

Test guide

Download the program. Place the Alpha Rex on the test pad. Run the program.

4. Color sensor

Building guide

Can you make the Alpha Rex detect colors by adding the Color Sensor?

Page 235: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

235/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

Programming guide

Can you program the Alpha Rex to ask for the green ball and then identify it? The Alpha Rex should identify andreject balls in other colors.

// alpharex_4.nxc// Ask for a green ball

// TODO Use a voice to ask for a green ball when nxc adds// support for sound files on Linux

// Ask for a ball when an object is closer than this#define THRESHOLD 50

// Three directions for walking#define INPLACE 0

#define FORWARDS 1

Page 236: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

236/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

#define FORWARDS 1#define BACKWARDS 2

void drop_ball (){ RotateMotor (OUT_A, -100, 2*60); Wait (800); // back to center RotateMotor (OUT_A, 100, 2*60);}

// Get the legs in a standard position// Motor B & touch sensor 1 ---> right leg// Motor C & touch sensor 2 ---> left legvoid reset_legs (){ if (1 == SENSOR_1) { OnFwd (OUT_B, 50); Wait (300); until (0 == SENSOR_1); Off (OUT_B); } OnFwd (OUT_B, 50); until (1 == SENSOR_1); Off (OUT_B);

if (1 == SENSOR_2) { OnFwd (OUT_C, 50); Wait (300); until (0 == SENSOR_2); Off (OUT_C); } OnFwd (OUT_C, 50); until (1 == SENSOR_2); Off (OUT_C);}

void walk (int direction, unsigned long duration){ unsigned long t0;

RotateMotor (OUT_B, 70, 45); // Put right leg down RotateMotor (OUT_C, -70, 150); // Put left leg up

t0 = CurrentTick ();

switch (direction) { case INPLACE:

OnFwd (OUT_B, 50); OnFwd (OUT_C, -50); until (CurrentTick () - t0 > duration); break;

case FORWARDS:

OnFwdSync (OUT_BC, 50, 0); until (CurrentTick () - t0 > duration); break;

case BACKWARDS:

OnRevSync (OUT_BC, 50, 0); until (CurrentTick () - t0 > duration); break; }

Off (OUT_BC); reset_legs ();}

task main (){ SetSensorTouch (S1); SetSensorTouch (S2); // Bring the touch sensors on-line SetSensorLowspeed (S4); // Bring the ultrasonic sensor on-line SetSensorColorFull (S3); // Bring the color sensor on-line

Page 237: NXT 2.0 Instructions

12/30/12 NXT 2.0 Instructions

237/237arbitrarycreations.co.uk/lego/nxt_2_instructions/#robogator_jaws_program

reset_legs (); Wait (500);

while (true) { until (SensorUS (S4) < THRESHOLD);

// Display a message TextOut (0, LCD_LINE1, " GIVE ME A ", true); TextOut (0, LCD_LINE3, " *** GREEN *** ", false); TextOut (0, LCD_LINE5, " BALL ", false);

// Wait until a ball is placed in the right hand until (INPUT_BLACKCOLOR != SENSOR_3);

Wait (2000); ResetScreen ();

if (INPUT_GREENCOLOR == SENSOR_3) { // Do a dance walk (INPLACE, 5000); // Release the ball drop_ball (); } else { // Move forward walk (FORWARDS, 5000); // Drop the ball drop_ball (); // Move back walk (BACKWARDS, 5000); }

Wait (1000); }}

Test guide

Download the program. Place the Alpha Rex on the test pad. Before running the program, the Alpha Rex'shands should be closed and pointed forward (do this by turning the gear on the right side of its back). Run theprogram.