Variables are numbers with no single, specific, predefined value. They can be mathematically performed on to have the value change. Here are a couple examples.
Code: Select all
int x,y,z; //this is where you make variables to work with. The int means that they are integers, and by the NXC/NBC (possibly all C) definition, they are 16 bit signed (positive or negative) values in the range of -32768 to 32767.
x=10; //x now has the value of 10
y=5; //y now has the value of 5
z=x+y //z equals x (10) plus y (5), so z now equals 15
Do you want to know how to use files with an NXC program? Based on your current understanding level, I think it is a little early to discuss specifics.
What is it that you want to know about the NXT sensors? Before using them in a program, you must initialize them, so the NXT knows how to read them. I use the SetSensor commands as follows.
Code: Select all
SetSensorTouch(S1); //Set the port up for a touch sensor. It starts it reading the analog pin, in inactive mode.
SetSensorLight(S1); //Set the port up for a light sensor. It starts it reading the analog pin, in inactive mode. Also uses pin 5 to turn on and off the light.
SetSensorSound(S1); //Set the port up for a sound sensor. It starts it reading the analog pin, in inactive mode. Also uses pins 5 and 6.
SetSensorLowspeed(S1); //Set the port up for a digital (I2C) sensor.
SetSensorUltrasonic(S1); //I can only assume it is similar to the SetSensorLowspeed command. I have never tested this function.
SetSensorColorFull(S1); //For the 2.0 color sensor. It turns it on in multicolor mode, so it reads all three colors.
SetSensorColorRed(S1); //For the 2.0 color sensor. It turns on the red light.
SetSensorColorGreen(S1); //For the 2.0 color sensor. It turns on the green light.
SetSensorColorBlue(S1); //For the 2.0 color sensor. It turns on the blue light.
SetSensorColorNone(S1); //For the 2.0 color sensor. It turns off all the lights.
The values returned by the analog sensors are scaled to whatever they are set up for. For example, if it is set up as a touch sensor, only the value of 0 or 1 will be returned. If you want to read the RAW value (0-1023), use the following.
To access the values from the sensors, you can usually use SENSOR_1 (or 2 or 3 or 4). Here is an example of a few of the last concepts together.
Code: Select all
task main()
{
SetSensorLight(S1); //Set it up as a light sensor
SetSensorMode(S1, IN_MODE_RAW); //Set it up to return the RAW values, 0-1023
//SetSensorType(S1, IN_TYPE_LIGHT_INACTIVE); //use this line to turn the light off
//SetSensorType(S1, IN_TYPE_LIGHT_ACTIVE); //use this line to turn the light on
while (true) //repeat forever
{
Wait (50); //slow down a little
ClearScreen(); //clear the screen
NumOut(0,LCD_LINE1,SENSOR_1); //display the value being returned by the light sensor (it will be the RAW value).
}
}
Define, is used like this.
It sets the value of "LeftMotor" to that of "OUT_B". Now, instead of coding OnFwd(OUT_B, 100);, you would use OnFwd(LeftMotor, 100);. This way, you can have a huge number of commands, that when coded, used "LeftMotor" to refer to the left motor. When you are redesigning mechanically, and you are required to move the wire for the left motor to port OUT_C, all you have to do is change #define LeftMotor OUT_B to #define LeftMotor OUT_C, and that is ALL you have to change in the code. You won't have to scroll down, looking for all the OUT_B constants, and changing them to OUT_C.
Include is used like this.
It includes another part of a program, or library, and compiles both together at download. It can also be used for other types of files. Note, the file MUST be saved in the same directory on your computer as the program that "#include"s it.
IIRC, sub is a part of a program that is in a different place on the screen when programming, but can be called upon with a single line of code. This way, you can have a lot of "junk" on it, and not have to look at it in the task you want it in.
void seems to be similar to sub, but IIRC, it is a better method to use. Again, you can run the function with a single line of code. like the "sub", it contains the "junk" code that doesn't need to be an eyesore in the task, and it can also be used to shorten the code. For example, you can have 10 lines of code, but if you need access to the exact code in 5 different places, you can just call it from a single function (using a single line of code).
A switch is similar to an array of if-else statements. Instead if using the following:
Code: Select all
if (1==x){}
else{if(2==x){}
else{if(3==x){}
else{if(4==x){}
else{if(5==x){}
}
}
}
}
you can use
Code: Select all
switch (x){
case 1:{}break;
case 2:{}break;
case 3:{}break;
case 4:{}break;
case 5:{}break;
}
Hopefully that answers some of your questions. Also note, that none of my code has been tested, so it likely has bugs.