Here's some ideas that may help. I build various race car and dancing robots that follow a black line on a
light surface. To account for various robot designs, and parts, and especially room lighting, I do a short
"calibration" routine at the start of each program. The attached file, Ty_Dncr_31_Line_Contrast_Discover.nxc,
has code for 2 tasks that run concurrently to measure the lightness and darkness of the dark line and the
light background. The first task, task line_wiper(), rotates the robot about 60 degrees, in either direction,
over the light and dark areas. The second task, task line_contrast_discover(), measures the light intensity,
the minimum and maximum, as the robot moves back and forth. (I got some of the ideas for this code from
the site <
http://www.nxtprograms.com/>. This site shows NXT-G code, but the ideas are great.) I then calculate
the mid or average, and the range, and can use this in the main program as needed. I display the min, max,
mid, and range with the following code.
Code: Select all
void display_light_min_max()
{
unsigned short Bat_Lev;
TextOut(0, LCD_LINE1, "Volts = ", DRAW_OPT_CLEAR_WHOLE_SCREEN);
Bat_Lev = BatteryLevel();
NumOut((6 * StrLen("Volts = ")), LCD_LINE1, Bat_Lev);
TextOut(0, LCD_LINE4, "Current = ");
NumOut((6 * StrLen("Current = ")), LCD_LINE4, current_reflected_light_intensity);
TextOut(0, LCD_LINE5, "lite_min = ");
NumOut((6 * StrLen("lite_min = ")), LCD_LINE5, reflected_light_min);
TextOut(0, LCD_LINE6, "lite_max = ");
NumOut((6 * StrLen("lite_max = ")), LCD_LINE6, reflected_light_max);
TextOut(0, LCD_LINE7, "lite_mid = ");
NumOut((6 * StrLen("lite_mid = ")), LCD_LINE7, reflected_light_mid);
TextOut(0, LCD_LINE8, "lite_range = ");
NumOut((6 * StrLen("lite_range = ")), LCD_LINE8, reflected_light_range);
} // End - function - display_light_min_max
It takes some experimentation to determine how much to run the motor as the robots moves over the light and
dark, depending on whether your robot has wheels or treads, or whatever. Hope this helps.