Using the Light Sensor LED
|
Programming the LED
Remember when we configured the light sensor and added a sensor called "lightLED" in port dgtl3? We haven't used it just yet, but it works exactly like every other LED. The easiest command you can give the light sensor is to simply turn the LED on and keep it on, for whatever reason this is necessary. Simply insert this little line of code before the rest of your program in the task main ():
SensorValue[lightLED]=true; |
So, if we wanted to turn the light on during the program to find a line and stop at it, we would change the code to look something like this:
#pragma config(CircuitBoardType, typeCktBoardUNO) #pragma config(PluginCircuitBoard, typeShieldDFRobotMotor) #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) #pragma config(Sensor, anlg0, lightSensor, sensorReflection) #pragma config(Sensor, dgtl3, lightLED, sensorDigitalOut) #pragma config(Motor, motor_5, rightServo, tmotorInternalHBridgeSinglePWM, openLoop, reversed, IOPins, dgtl5, dgtl4) #pragma config(Motor, motor_6, leftServo, tmotorInternalHBridgeSinglePWM, openLoop, reversed, IOPins, dgtl6, dgtl7) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// task main () { SensorValue[lightLED]=true; // Turns the light sensor LED on while (SensorValue[lightSensor] < 60) // While the light sensor is detecting light ground, not the line (value may need to be adjusted based on the darkness of your surfaces) { //Drive Forward motor[leftServo]=100; motor[rightServo]=100; } //when no longer detecting a light surface, stop motor[leftServo]=0; motor[rightServo]=0; } |
There's a problem though, right? Your robot keeps blowing past the lines and never stops. Unfortunately for us, the light sensor returns a different range of values whenever the LED is on. So instead of being able to simply turn on the LED, we also have to adjust its values.
Here, for example is a modified version of the light sensor maze designed to use the LED.
#pragma config(CircuitBoardType, typeCktBoardUNO) #pragma config(PluginCircuitBoard, typeShieldDFRobotMotor) #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) #pragma config(Sensor, anlg0, lightSensor, sensorReflection) #pragma config(Sensor, anlg1, ambientSensor, sensorReflection) #pragma config(Sensor, dgtl3, lightLED, sensorDigitalOut) #pragma config(Motor, motor_5, rightServo, tmotorInternalHBridgeSinglePWM, openLoop, reversed, IOPins, dgtl5, dgtl4) #pragma config(Motor, motor_6, leftServo, tmotorInternalHBridgeSinglePWM, openLoop, reversed, IOPins, dgtl6, dgtl7) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// void DriveBackward() { motor[leftServo] = -100; motor[rightServo] = -100; wait1Msec(400); } void TurnRight(int time) { motor[leftServo] = 100; motor[rightServo] = -100; wait1Msec(time); } void TurnLeft(int time) { motor[leftServo] = -100; motor[rightServo] = 100; wait1Msec(time); } void Stop() { motor[leftServo] = 0; motor[rightServo] = 0; } void driveForwardUntilDark () { while (SensorValue[lightSensor] < 41) //notice that we have significantly lowered the threshold to accommodate the light sensor's range { //drive forward until the sensor detects a line motor[leftServo]=100; motor[rightServo]=100; } Stop(); //make sure to put stops in between actions to check the momentum of the last move DriveBackward(); // back away from the line in order to get room to turn Stop(); } task main() { //Turn the LED on SensorValue[lightLED]=true; //slight drive forward to give the light sensor time to normalize motor[leftServo]=100; motor[rightServo]=100; wait1Msec(300); driveForwardUntilDark(); TurnLeft(805); //will probably need to adjust time for your robot Stop(); driveForwardUntilDark(); TurnRight(1000); //will probably need to adjust time for your robot Stop(); driveForwardUntilDark(); TurnRight(950); //will probably need to adjust time for your robot Stop(); driveForwardUntilDark(); TurnLeft(705); //will probably need to adjust time for your robot Stop(); driveForwardUntilDark(); TurnLeft(655); //will probably need to adjust time for your robot Stop(); driveForwardUntilDark(); //One last straight drive to get us into the end zone motor[leftServo]=100; motor[rightServo]=100; wait1Msec(2000); } |
One word of caution about using the LED, however: It tends to make the range of values on the light sensor very small. Which means that if you aren't working with black and white (and sometimes even if you are), the difference between your light and dark may not be big enough for the sensor to reliably give out values that are more than a point or two different. In turn, the lack of difference will mess up your while() loops and destroy your program. We do not advise you to use the LED unless you are in a situation where the light sensor absolutely cannot function without it.