|
Re: Programming Light Sensor
The light sensor will return true if light is reflected back into it, and false if no light is reflected. In other words, when the light on it is green, it is false, and if the light is orange, it is a true. All you need is a simple if statement:
if(LightWhite->Get()) {
//the sensor is on the line
}
or
if(!LightWhite->Get()) {
//The sensor is not on the line
}
Depending on which sensors are on and which are off, you can find out where the robot is relative to the line, and move accordingly.
Another useful feature of c++ is that you have the ability to use booleans as ints (either a 1 or a 0) and add them together. The following would set "Lines" to the number of sensors that are triggered:
int Lines = WhiteLightOne->Get() + WhiteLightTwo->Get();
|