Presentation is loading. Please wait.

Presentation is loading. Please wait.

Preprocessor Directives (last modified 9/19/05 2:47pm) Statements beginning with # are directives to the preprocessor. –They DO NOT end with ; –Before.

Similar presentations


Presentation on theme: "Preprocessor Directives (last modified 9/19/05 2:47pm) Statements beginning with # are directives to the preprocessor. –They DO NOT end with ; –Before."— Presentation transcript:

1 Preprocessor Directives (last modified 9/19/05 2:47pm) Statements beginning with # are directives to the preprocessor. –They DO NOT end with ; –Before the program is compiled, the preprocssor copies these definitions into the code. –These definitions are not required, but make the code easier to write and more readable –if you want to change the port on which a sensor is connected, and that appears multiple times in the program, then a single change in the #define is all you need to make the change.

2 // tankbot2.nqc - drive and turn // motors #define LEFT OUT_A #define RIGHT OUT_C // how much time to spend turning or forward #define TURN_TIME 200 #define STRAIGHT_TIME 100 // speed to run a turned motor #define TURN_POWER 3 task main() { // start with both motors on On(LEFT+RIGHT); // repeat the following steps forever while(true) { …… }

3 Programming Style Use indentation to visually group program blocks. Use comments to document the program

4 // tankbot2.nqc - drive and turn // motors #define LEFT OUT_A ……… task main() { // start with both motors on On(LEFT+RIGHT); // repeat the following steps forever while(true) { // turn right by slowing down the right tread SetPower(RIGHT, TURN_POWER); Wait(TURN_TIME); // resume going straight SetPower(RIGHT, OUT_FULL); Wait(STRAIGHT_TIME); // turn left SetPower(LEFT, TURN_POWER); Wait(TURN_TIME); // resume going straight SetPower(LEFT, OUT_FULL); Wait(STRAIGHT_TIME); }

5 Time NQC is a real-time programming language. Time is measured in 1/100 of a second #define TURN_TIME 200 Will define the turning time to be 2 seconds

6 Using Time to Control Motion Wait(300); The program will wait at that statement for 3 seconds. The following program will drive straight for 4 seconds, then if OUT_A is attached to the left tread, the left tread will stop and the right tread will continue to drive for 1second causing the tankbot to stear toward the left. On(OUT_A + OUT_B); Wait(400); Off(OUT_A); Wait(100);

7 Generating a Random Number NQC has a random number generating function Random(100); Generates a random integer between 0 and 100. All numbers in the range are equally likely (uniform distribution) 0100

8 NQC Functions So far we have seen several NQC functions. They have been associated with: Motors: On(arguments); Off(arguments); SetPower(arguments); OnFwd(arguments); Sensors SetSensor(arguments); Mathematical Random(arguments);

9 All NQC functions begin with a capital letter. As in SetSensor, two capitals are used to make them more readable. The number of arguments depend on the function. Appendix D (Braun) provides a Quick Reference to the language. The functions are grouped into categories. NOTE the restrictions. NOTE THERE ARE A FEW DIFFERENCES BETWEEN RCX AND RCX2.0. Which system do you have?


Download ppt "Preprocessor Directives (last modified 9/19/05 2:47pm) Statements beginning with # are directives to the preprocessor. –They DO NOT end with ; –Before."

Similar presentations


Ads by Google