Teamwork
Creativity
Excellence

Nebo Elementary School
Department of Fine Arts



PTA Performances

Fine Arts ASP Weekly Schedule

Music Links

Exercises

Sub Plans

Computer Help

Nebo Music Curriculum Overview

Recorder

Nebo Specials Schedule

Nebo Music Calendar

Nebo Fine Arts Home

Rhythm Composing Project

4th Grade Instrument Project Template

5th Grade Composer Project Template

5th Grade Listening Links for Composer Project

Lego MINDSTORMS NXT and NXC Programming Language:
Link to Tutorial for Using NXC with Bricx Command Center
GaETC 2010

The Lego MINDSTORMS NXT Robotics kit uses motors, sensors, Lego Mindstorms building parts and a small computer called the "NXT Brick" to create Robots that one can program using the MINDSTORMS NXT software.  Like Scratch, MINDSTORMS NXT-G uses icons or blocks that represent programming commands. 

Another way to program the NXT Brick is to use a text based language such as NXC or Robot C.  Using a text based language gives the programmer finer control of the Robot's computing power.  In addition, NXC is an open source program, thus one can save on the costs of the site license for NXT-G. 

However, text based programming presents a different set of challenges.  The programmer must compile and "de-bug" the program if it does not compile or run properly on the Robot.  In addition, two sets of software, "NBC" and "T2N" must be used at the command line level to transfer the program to the NXT Brick. 

Definition of a Robot: A Robot is a machine that can be programmed to do a task without being directly controlled by a human being.   There are two parts to a robotic system.

1. Hardware: This is the mechanical robot. The motors, gears, wheels, parts, computers that make up the machine.  This is the part you build.

2. Software:  This is the instructions for the tasks and actions the robot will do.  This is the part you write.

NXC Language is used to create the software that controls the actions of the robot hardware.  NXC is a text based system of assembling instructions for the machine.  The flow of direction usually moves sequencially from top to bottom.  This software is an example of a compiled program.  Programs you write in NXC must be compiled with NBC and downloaded to the NXT Brick using T2N before the Robot will perform the program.  (Scratch and Python are "Interpreted Languages.")

Sample Robot Projects

Example 1: Sample NXC Program Driving the Robot Forward and Back

// This is a Basic Out and Back program

task main()
  {
       OnFwd(OUT_BC, 75);
       Wait(4000);
       Off(OUT_BC);
       OnRev(OUT_BC);
       Wait(4000);
       Off(OUT_BC);
   }

// End of Program

The above program has a main task that drives motors B and C at 75% power.  The next line (wait(4000)) tells the Robot to wait 4 seconds.  The Off(OUT_BC); command tells the motors to stop.  Next the robot moves the B and C motors in reverse.  The final line turns the motor off.

Here are some basic rules of NXC:

  • Comments go behind //.  Use a lot of comments in your program to tell the humans what you are trying to do.  The computer does not read or compile the comments.
  • Each NXC program as a "task main()."  This is what the Robot looks for and does first. 
  • Curly Brackets { }  surround tasks. 
  • Each task ends with a semi-colon;
  • Spacing is not as important (spacing is very important in Python).  But, curly brackets, semi-colons, and paranthesis are VERY important. 
  • Aways use a plain text editor (Kwrite, Notepad . . .) to create programs.  Save as ProgramName.nxc

Example 2:  Movement Codes (Codes that Control the Motors)

Basic Forward:  OnFwd(OUT_ABC, Power Level):

OnFwd(OUT_BC, 75);  

(Motors B and C move forward at 75% power level)

Basic Reverse: OnRev(OUT_ABC, Power Level):

OnRev(OUT_BC, 75);

(Motors B and C move backward at 75% power level)

Stopping the Motors:  Off(OUT_ABC);

Off(OUT_BC);

(Motors B anc C stop and brake)


Forward with Rotation Sensor: RotateMotor(OUT_ABC, Power Level, Degrees)

RotateMotor(OUT_BC, 75, 360);

(Motors B and C rotate at 75% power 360 degrees (1 time around)).

Gradual Turn: RotateMotorEx(OUT_ABC, Power Level, Degrees, Turn, Sync, Stop)

RotateMotorEx(OUT_BC, 75, 360, 45, true, true)

(Motors B and C rotate one time and the robot turns slightly 45 degrees to the right)


Compensate for Resistance: OnFwdReg(OUT_ABC, Power Level, OUT_REGMODE_SPEED);

OnFwdReg(OUT_BC, 75, OUT_REGMODE_SPEED);

(Motors B and C rotate and will apply more power if they meet resistance.)



Example 3: Looping (Iteration)

Repeat a task several times. 
 
repeat(4)
    {
         RotateMotor(OUT_BC, 75, 720);
         RotateMotor(OUT_B, 75, 360);
    }

(The above example repeats the Robot moving forward and doing a point turn 4 times.)

Infinite Loop.  Keeps the task going and going . . .

while(true)
    {
        OnFwd(OUT_BC, 75);
    }

(Keeps the motors on forever.  Or at least until you stop the Robot.)

Example 4: Conditional Statements

Conditionals are good in interactive programs.  For example, using the touch sensor to let the Robot know it has bumped into an object. 

if (SENSOR_1 == 1)
{
    OnRev(OUT_BC, 75);
    Wait(300);
    OnFwd(OUT_B, 75);
    Wait(300);
    OnFwd(OUT_BC, 75);
}

(If the Touch Sensor in port 1 is triggered, B and C motors move backwards to .3 seconds.  Then Motor B turns forwards for .3 seconds.  Then B and C motors move forward.)

Example 5: Light Sensor

The Light Sensor senses differences in light intensity (brightness).  This value can be used to sense the light around the robot or differences in reflected light off of surfaces like the floor. 

#define THRESHOLD 40

task main()
{
    SetSensorLight (IN_3);
    OnFwd(OUT_BC, 75);
    while (true)
    {
        if (Sensor(IN_3) > THRESHOLD)
        {
             OnRev(OUT_C, 75);
             Wait(100);
             until (Sensor(IN_3) <= THRESHOLD);
             OnFwd(OUT_BC, 75);
         }
     }
}


(The above program follows a black line a white surface. (Clockwise only)

Example 6: Motion/Ultrasonic Sensor

The Motion/Ultrasonic Sensor uses high frequency sound waves and echos to detect the distance between the sensor and other objects. 

#define NEAR 15 // measured in centimeters

task main()
{
    SetSensorLowspeed(IN_4);
    while(true)
        {
             OnFwd(OUT_BC, 50);
             while (SensorUS(IN_4)<NEAR)
                  {
                        Off(OUT_BC);
                        OnRev(OUT_C, 100);
                        Wait(800);
                  }
         }
}


Example 7: Touch Sensor

This button can sense in three modes, pressed, released, and pressed and released.  This enables the robot to sense when it is touching or bumping another object.

task main()
{
    SetSensorTouch(IN_1);
    OnFwd(OUT_BC, 75);
    while(true)
    {
         if (SENSOR_1 == 1)
         {
              OnRev(OUT_BC, 75);
              Wait(300);
              OnFwd(OUT_B, 75);
              Wait(300);
              OnFwd(OUT_BC, 75);
          }
    }
}


Example 8: Sound Sensor

This is a microphone that senses the intensity of sound near the Robot. 

#define THRESHOLD 40
#define MIC SENSOR_2

task main()
{
    SetSensorSound(IN_2);
    while(true)
    {
         until (MIC > THRESHOLD);
         OnFwd(OUT_BC, 75);
         Wait(300);
         until(MIC > THRESHOLD);
         Off(OUT_AC);
         Wait(300);
    }
}


Example Programs for basic vehicle robots:

1. Move Forward and Back

// This is a Basic Out and Back program

task main()
  {
       OnFwd(OUT_BC, 75);
       Wait(4000);
       Off(OUT_BC);
       OnRev(OUT_BC);
       Wait(4000);
       Off(OUT_BC);
   }

// End of Program


2. Move Forward, Spin, and Back

// Forward Spin and Back

task main()
  {
       RotateMotor(OUT_BC, 75, 1440);
       OnFwd(OUT_B, 75);
       OnRev(OUT_C, 75);
       Wait(1000);
       Off(OUT_BC);
       RotateMotor(OUT_BC, 75, 1440);
   }

//End of Program


3. Using Touch to allow the robot to move about the room and not bump into walls or other objects.

// Use Touch Sensor to detect objects
// This program uses the mutex variable to switch between tasks
// The robot draws a square and avoids objects in the square

mutex moveMutex;

task move_square()
{
    while(true)
    {
        Aquire(moveMutex);
        RotateMotor(OUT_BC, 75, 1440);
        RotateMotor(OUT_B, 75, 360);
        Release(moveMutex);
    }
}

task check_sensors()
{
    while(true)
    {
         if (SENSOR_1 == 1);
         {
               Acquire (moveMutex);
               RotateMotor(OUT_BC, 75 -720);
               RotateMotor(OUT_B, 75, 360);
               Release (moveMutex);
         }
     }
}

task main()
{
    Precedes(move_square, check_sensors);
    SetSensonTouch(IN_1);
}

// End of Program

                 
Writing a Program Using NXC:

1. Open your favorite Text Editor.  (Click on K Menu.  Select Debian->Apps->Editors->Kwrite)
2. Compose your program.
3. Save your program to your file area.  Make sure you use the extension .nxc (Example: LastnameForward.nxc)

Compiling an NXC Program
1. Navigate to Directory where you saved "filename.nxc"
    a. Click on the House (Home Directory)
    b. Click "NeboMusicWork"
    c. Click Your Grade
    d. Click Your Class
    e. Click on Your Name
2. Open a Bash Shell  (Press F4)
3. Use following command to compile:

nbc filename.nxc -O=filename.rxe

4. New file will appear in directory "filename.rxe"

Sending (Uploading) the program to the NXT Brick. 

1. In same directory as "filename.rxe" - open a Bash Shell. (Press F4)
2. Plug in Robot NXT brick to USB port of the computer.
3. Do following command:

t2n -v -put filename.rxe
  
4. Program should load into NXT brick.
5. Unplug the NXT brick and try the Robot!

Sample Robot Projects

Email Mr. Michaud





Chorus
ASP Dance
ASP Drama
ASP Orchestra
ASP Broadcast



Nebo Fine Arts Home
     Nebo Elementary Home                 Paulding County Public Schools