#pragma config(Sensor, S2, lightSensor, sensorLightActive) #pragma config(Sensor, S4, sonarSensor, sensorSONAR) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// /* M U L T I T A S K I N G D E M O Ron Kessler 9/27/2018 PURPOSE: This demo shows how to split the CPU's time such that it runs two tasks. This makes more efficient use of the CPU and allows better performance for large tasks/functions. Here I am monitoring the light and sonar sensors. When the wait statements are commented out, you will notice that in TaskMonitor, the tasks are hogging all the CPU time by being run one after the other. With the wait statements active, the CPU is idle most of the time. This leaves it available to do other things. SETUP: Create a separate task for the sonar much like we did for a custom function. Create a variable that gets updated inside an infinite loop. Comment out the wait statement. Open the Robot | Debugger Windows | Task Status window (must be in super user menu mode) Compile after making any changes! Run the program, click start, and view the Window NXT devices (Robot | Debugger Windows) As you move your hand in front of the sonar the readins change and when the robot moves over different colors the light sensor readings change also. Observe in task Monitor what is happening with the CPU time on each task. The wait statements allow the CPU to show idle or wait mode most of the time. Check out the help section (View | Function Library) to see how to control tasks. */ task checkSonar() { int sonarReading = 0; while(true) { sonarReading = SensorValue[sonarSensor]; //constantly update sonarReading variable //wait1Msec(5); } } //---program always starts here task main() { StartTask(checkSonar); //now start the other task. Only needed once. And yes, you can stop it also. You can also set priority levels. int lightReading = 0; while(true) { lightReading = SensorValue[lightSensor]; //constantly update lightReading variable //wait1Msec(5); } }