Here is the general form of the command, to set a sensor so that it begins reading:
SetSensor(sensorNumber,sensorType)
The command requires two parameters, 'sensorNumber', and 'sensorType'.
sensorNumber can be any of the following pre-defined constants:
SENSOR_1 -- left touch sensor
SENSOR_2 -- center light sensor
SENSOR_3 -- right touch sensorsensoryType can be any of the following:
SENSOR_LIGHT
SENSOR_TOUCHHere's an example of a SetSensor function call to set the light sensor to on.
SetSensor(SENSOR_2,SENSOR_LIGHT);
Generally, you'll "set" the sensors you need at the top of the main task, e.g.,
task main()
{SetSensor(SENSOR_1,SENSOR_TOUCH);
// .. rest of program
Once a touch sensor has been set, the system keeps its value at either 0 or 1, 0 meaning off and 1 meaning on. We call such 0/1 values booleans.
Generally, you'll check the value of a sensor with an 'if' or 'while' condition, e.g.,
while (SENSOR_1==0) // two equals signs are used to compare two values
{
// do something
}Light sensors provide a reading between 0 and 100. So we can check a light sensor with a statement such as:
while (SENSOR_2>50)
{
// do something
}
Problems
Let's use NQC to redo some of the programs we wrote with the Robot Invention System:
1. Write an NQC program that causes the robot to turn around when either sensor hits something.
2. Write an NQC program that causes the robot to drive along the black line until it hits white.
3. Write an NQC program that causes the robot to drive around the black circle continuously.