Page 33
Code
/* For this program, point the camera at an 8-quadrant grid that fills
the KIPR Link screen. Each motor light corresponds to an area of the grid
in the pattern
G G G G
R R R R
i.e. if the center of the largest object on channel 0 is over a particular
grid location, that motor light will be turned on */
int
main()
{
int
x, y, quad, xMax =
160
, yMax =
120
;
camera_open(LOW_RES);
// activate camera
camera_update();
// get most recent camera image and process it
while
(side_button()==
0
) {
x = get_object_center(
0
,
0
).x;
// get image center x data
y = get_object_center(
0
,
0
).y;
// and y data
if
(get_object_count(
0
) >
0
) {
// there is a blob in view
display_printf(
0
,
1
,
"Center of largest blob: (%d,%d) "
,x,y);
ao();
// turn off all motor lights
// determine the horizontal quadrant to be lighted
if
(x < xMax/
4
) {
quad =
0
;}
else
if
(x >= xMax/
4
&& x < xMax/
2
) {
quad =
1
;}
else
if
(x >= xMax/
2
&& x <
3
*xMax/
4
) {
quad =
2
;}
else
if
(x >=
3
*xMax/
4
) {
quad = 3;}
if
(y < yMax/
2
) {
// green row
fd(quad);
// quad's green motor light on
display_printf(
0
,
2
,
"Green quadrant %d "
, quad);
}
else
{
// red row
bk(quad);
// quad's red motor light on
display_printf(
0
,
2
,
"Red quadrant %d "
, quad);
}
}
else
{
display_printf(
0
,
1
,
"No object in sight "
);
ao();
// lights off
}
msleep(
200
);
// don't rush print statement update
camera_update();
// get new image data before repeating
}
ao();
// clean up
camera_close();
}