The Four Basic Programs
Program 1
//prog1.nqc
//This
program turns motors A and C on forward for 5 seconds
task main ()
{
//Tells
motors A and C to go forward full speed, 5 seconds
OnFwd(OUT_A);
OnFwd(OUT_C);
Wait(500);
//Moves 5 seconds
Off(OUT_A);
Off(OUT_C);
Wait(100);
//Tells
motors A and C to go backward full speed, 5 seconds
OnRev(OUT_A
+ OUT_C); //
We can start both motors at once
Wait(500);
Off(OUT_A
+ OUT_C);
}
Program 2
//prog2.nqc
//Description:
This program will introduce the bump sensor.
task main ()
{
SetSensor(SENSOR_1,SENSOR_TOUCH);
// SENSOR_1 is specified as a bump sensor
while(true) {
// Repeatedly check the bump sensor
until
(SENSOR_1 == 1);
// wait until bump switch is pressed
OnFwd(OUT_A + OUT_C);
// then turn on the motors
until
(SENSOR_1 == 0);
// wait until bump switch is not pressed
Off(OUT_A + OUT_C);
// then turn off the motors
}
}
Program 3
//prog3.nqc
//Description:
This program will introduce the light sensor.
#define threshold 40
// Above threshold is light, below threshold is dark
task main ()
{
SetSensor(SENSOR_2,SENSOR_LIGHT);
// SENSOR_2 is specified as a light sensor
OnFwd(OUT_A +
OUT_C);
// Turn the motors on and
until (SENSOR_2
<= threshold);
// wait until light sensor sees dark
Off(OUT_A +
OUT_C);
// then turn off the motors
}
Program 4
//Prog4.nqc
#define threshold 38
task main ()
{
SetSensor(SENSOR_1,SENSOR_LIGHT);
SetSensor(SENSOR_3,SENSOR_LIGHT);
start left_sensor_watcher;
start right_sensor_watcher;
}
task left_sensor_watcher ()
// This sensor watcher repeatedly
{
// watches the left sensor.
while(true)
{
OnFwd(OUT_A);
// Turn on motor A, wait until sensor sees dark
until(SENSOR_1 < threshold);
Off(OUT_A);
// then turn off motor A. Wait
until sensor
until(SENSOR_1 >= threshold);
// sees light then repeat.
}
}
task right_sensor_watcher ()
// Similar to the left sensor watcher.
{
while(true)
{
OnFwd(OUT_C);
until(SENSOR_3 < threshold);
Off(OUT_C);
until(SENSOR_3 >= threshold);
}
}