Monday, July 20, 2009

Using light sensors


I was interested in using light sensors to achieve the Leader-Follower algorithm. I used two light sensors on each side of the NXT to detect an LED light that is mounted on the NXT. I started with a simple algorithm, coded it and tested it. It worked okay but modifications were needed so I changed the code to send more specific directions to the NXT. More modifications were made to achieve better results. At the end, this algorithm didn’t show enough potential to compete with the NXTCam. The code below shows the three different stages I went through coding and improving my algorithm.

1. task main (){

while (true) {

wait1Msec(50); //the program waits 50 millisecond to initialize the light sensor

while((SensorValue(lightSensor1) - SensorValue(lightSensor2)) {

motor[motorB] = 40;

motor[motorC] = 40;

}

while(SensorValue(lightSensor1) < style=""> {

motor[motorB] = 40; //motor A is run at a 100 power level

motor[motorC] = -40; //motor B is run at a 100 power level

}

while(SensorValue(lightSensor1) > SensorValue(lightSensor2) ) {

motor[motorB] = -40; //motor A is run at a 100 power level

motor[motorC] = 40; //motor B is run at a 100 power level

} } }

2.

task main (){

while (true) {

wait1Msec(50); //the program waits 50 millisecond to initialize the light sensor

int right = SensorValue(lightSensor1);

int left = SensorValue(lightSensor2);

motor[motorB] = (left - right) * 5 ;

motor[motorC] = (right - left) * 5 ;

} }

3.

task main (){

while (true) {

wait1Msec(50); //the program waits 50 millisecond to initialize the light sensor

int right = SensorValue(lightSensor1);

int left = SensorValue(lightSensor2);

motor[motorB] = left * 5 ;

motor[motorC] = right * 5 ;

} }

Time: 7 hrs.

No comments:

Post a Comment