// The following are example nqc programs. You will need to copy and paste
// each one into a text file and then compile and download it to the RCX
// speed.nqc -- sets motor power, goes forward, waits, goes backwards
task main()
{
SetPower(OUT_A+OUT_C,2);
OnFwd(OUT_A+OUT_C);
Wait(400);
OnRev(OUT_A+OUT_C);
Wait(400);
Off(OUT_A+OUT_C);
}
------------------------------------------------------------------------
// spiral.nqc -- Uses repeat & variables to make robot move in a spiral
#define TURN_TIME 100
int move_time; // define a variable
task main()
{
move_time = 20; // set the initial value
repeat(50)
{
OnFwd(OUT_A+OUT_C);
Wait(move_time); // use the variable for sleeping
OnRev(OUT_C);
Wait(TURN_TIME);
move_time += 5; // increase the variable
}
Off(OUT_A+OUT_C);
}
-----------------------------------------------------------------------
// random.nqc -- uses a while loop & random() to vary move/turn time
int move_time, turn_time;
task main()
{
while(true)
{
move_time = Random(60);
turn_time = Random(40);
OnFwd(OUT_A+OUT_C);
Wait(move_time);
OnRev(OUT_A);
Wait(turn_time);
}
}
-----------------------------------------------------------------------
// Use of if-else to make robot turn in arbitrary direction
#define MOVE_TIME 100
#define TURN_TIME 85
task main()
{
while(true)
{
OnFwd(OUT_A+OUT_C);
Wait(MOVE_TIME);
if (Random(1) == 0)
{
OnRev(OUT_C);
}
else
{
OnRev(OUT_A);
}
Wait(TURN_TIME);
}
}
------------------------------------------------------------------------
// Use of touch sensors
task main()
{
SetSensor(SENSOR_1,SENSOR_TOUCH);
OnFwd(OUT_A+OUT_C);
while (true)
{
if (SENSOR_1 == 1)
{
OnRev(OUT_A+OUT_C); Wait(30);
OnFwd(OUT_A); Wait(30);
OnFwd(OUT_A+OUT_C);
}
}
}
------------------------------------------------------------------------
// Use of a light sensor to make robot go forward until it "sees" black,
// then turn until it's over white
#define THRESHOLD 37
task main()
{
SetSensor(SENSOR_2,SENSOR_LIGHT);
OnFwd(OUT_A+OUT_C);
while (true)
{
if (SENSOR_2 < THRESHOLD)
{
OnRev(OUT_C);
Wait(10);
until (SENSOR_2 >= THRESHOLD);
OnFwd(OUT_A+OUT_C);
}
}
}
-----------------------------------------------------------------------
// Tasking
task main()
{
SetSensor(SENSOR_1,SENSOR_TOUCH);
start check_sensors;
start move_square;
}
task move_square()
{
while (true)
{
OnFwd(OUT_A+OUT_C); Wait(100);
OnRev(OUT_C); Wait(85);
}
}
task check_sensors()
{
while (true)
{
if (SENSOR_1 == 1)
{
stop move_square;
OnRev(OUT_A+OUT_C); Wait(50);
OnFwd(OUT_A); Wait(85);
start move_square;
}
}
}
-----------------------------------------------------------------------
// Subroutines
sub turn_around()
{
OnRev(OUT_C); Wait(400);
OnFwd(OUT_A+OUT_C);
}
task main()
{
OnFwd(OUT_A+OUT_C);
Wait(100);
turn_around();
Wait(200);
turn_around();
Wait(100);
turn_around();
Off(OUT_A+OUT_C);
}
-----------------------------------------------------------------------
// Inline function
void turn_around(int turntime)
{
OnRev(OUT_C); Wait(turntime);
OnFwd(OUT_A+OUT_C);
}
task main()
{
OnFwd(OUT_A+OUT_C);
Wait(100);
turn_around(200);
Wait(200);
turn_around(50);
Wait(100);
turn_around(300);
Off(OUT_A+OUT_C);
}
----------------------------------------------------------------------
// Playing preprogrammed sounds
task main()
{
PlaySound(0); Wait(100);
PlaySound(1); Wait(100);
PlaySound(2); Wait(100);
PlaySound(3); Wait(100);
PlaySound(4); Wait(100);
PlaySound(5); Wait(100);
}
----------------------------------------------------------------------
// Playing tones
task music()
{
while (true)
{
PlayTone(262,40); Wait(50);
PlayTone(294,40); Wait(50);
PlayTone(330,40); Wait(50);
PlayTone(294,40); Wait(50);
}
}
task main()
{
start music;
while(true)
{
OnFwd(OUT_A+OUT_C); Wait(300);
OnRev(OUT_A+OUT_C); Wait(300);
}
}
----------------------------------------------------------------------
// Inline functions -- by-reference parameters
task main()
{
int count=0;
while (count<=5)
{
PlaySound(SOUND_CLICK);
Wait(count*20);
increment(count);
}
}
void increment(int& n)
{
n++;
}
----------------------------------------------------------------------
// Macros
#define turn_right(s,t) SetPower(OUT_A+OUT_C,s);OnFwd(OUT_A);OnRev(OUT_C);Wait(t);
#define turn_left(s,t) SetPower(OUT_A+OUT_C,s);OnRev(OUT_A);OnFwd(OUT_C);Wait(t);
#define forwards(s,t) SetPower(OUT_A+OUT_C,s);OnFwd(OUT_A+OUT_C);Wait(t);
#define backwards(s,t) SetPower(OUT_A+OUT_C,s);OnRev(OUT_A+OUT_C);Wait(t);
task main()
{
forwards(1,200);
turn_left(7,85);
forwards(4,100);
backwards(1,200);
forwards(7,100);
turn_right(4,85);
forwards(1,200);
Off(OUT_A+OUT_C);
}
----------------------------------------------------------------------