I improved your code a bit. It's quite a good start.
`setup
sync on
sync rate 0
autocam off
randomize timer()
position camera 0,0,-20
hide mouse
`create vectors to be used to find distance
null=make vector3(1)
null=make vector3(2)
`make spheres
for i=1 to 20 step 2
size=rnd(100)+1
make object sphere i,size,10,10
make object sphere i+1,size,5,5
x=rnd(500)-250
z=rnd(500)-250
position object i,x,0,z
position object i+1,x,0,z
exclude object on i+1
SET OBJECT i, 0, 1, 1
SET OBJECT i+1, 0, 1, 1
next i
`make plain
make object plain 22,500,500,1
move object down 22,50
xrotate object 22,-90
lvlofdet=1
delay=0
`main loop
do
`display info
text 0,0,"Screen fps: "+str$(screen fps())
if lvlofdet=1
a$="on"
else
a$="off"
endif
text 0,20,"Level of detail function is "+a$
text 0,40,"Press spacekey to switch culling"
`move camera
free_flight(0,0.25)
`if function is active then execute function for each sphere
if lvlofdet=1
set vector3 1,camera position x(),camera position y(),camera position z()
for i=1 to 20 step 2
Lod(i,i+1,120)
next i
endif
`if space changes mode to show how it affects frame rate
if spacekey()=1 and timer()-delay>200
delay=timer()
lvlofdet=lvlofdet*-1
`if mode is set to not control the level of detail then everything is shown in its high detail
if lvlofdet=-1
for i=1 to 20 step 2
exclude object off i
exclude object on i+1
next i
endif
endif
sync
loop
function Lod(hires,lowres,changedist#)
set vector3 2,object position x(hires),object position y(hires),object position z(hires)
subtract vector3 2,1,2
dist#=length vector3(2)
if dist#>changedist#
exclude object on hires
exclude object off lowres
else
exclude object on lowres
exclude object off hires
endif
endfunction
function free_flight(cam,speed#)
move camera cam,speed#*(upkey()-downkey())
x#=camera angle x(cam)
rotate camera cam,0,camera angle y(cam)+90,0
move camera cam,speed#*(rightkey()-leftkey())
rotate camera cam,x#+mousemovey()/2.0,camera angle y(cam)-90+mousemovex()/2.0,0
if camera angle x(cam)>90 then xrotate camera cam,90
if camera angle x(cam)<-90 then xrotate camera cam,-90
endfunction