
Handling Lego colour sensor sensitivity
Hello
I'm new to RobotC but I'm quickly warming to it as a way of programming Lego NXT robots. I'm experimenting with a few things using the Lego colour sensor, including a robot that rolls about over a floor with coloured patches on it (the floor, not the robot

)and does various actions depending on colour it encounters. I have a program that looks like this:
 |  |  |
 | Code: #pragma config(Sensor, S3, colorSensor, sensorCOLORFULL) #pragma config(Motor, motorB, , tmotorNXT, PIDControl, reversed, encoder) #pragma config(Motor, motorC, , tmotorNXT, PIDControl, reversed, encoder) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
void TurnRight(); void TurnLeft(); void Reverse(); void ChangeSpeed(); void TurnAround();
const int cFast = 60; const int cNormal = 30;
int nPower = cNormal; int nDirection = 0; // 0 = forwards, 1 = reverse int nSpeed = 0; // 0 = normal, 1 = fast
task main() { // wait a couple of seconds before starting wait1Msec(2000);
// all motors off motor(motorC) = 0; motor(motorB) = 0;
nDirection = 0; nSpeed = 0;
motor(motorC) = nPower; motor(motorB) = nPower;
while(true) { switch(SensorValue(colorSensor)) { case REDCOLOR: TurnRight(); break; case BLUECOLOR: TurnLeft(); break; case GREENCOLOR: Reverse(); break; case YELLOWCOLOR: ChangeSpeed(); break; case BLACKCOLOR: TurnAround(); break; case WHITECOLOR: break; default: nxtDisplayCenteredBigTextLine(0, "????????"); } // end switch
} // end of main while loop
} // end of task main
|  |
 |  |  |
The other functions, TurnLeft, TurnRight, Reverse, ChangeSpeed, TurnAround contain commands that control the motors. Hopefully the names are self-explanatory
So my questions so far are:
- Is this design more or less how others do similar things? Several of the sample programs look similar.
- I guess the constants REDCOLOR etc effectively match ranges of the raw values returned by the colour sensor? I notice that sometimes it does the wrong thing, for example it gives blue when rolling over a green patch (I am in a well lit room, using fairly even, mid-range colour patches). I guess this might be because as it rolls over the border between two colours, the sensor value is the average of the white area it is leaving and the green area it is rolling on to, and it is interpreting that as blue? What's a good way to accommodate that in the code?
Thanks for any suggestions