I think my script could be too versatil for your application but here is one of my function.
Wait for cells : "0" wait for no motion, "1" wait for only one cell motion
Launch delay : msec waiting before scanning for motion
Grid resolution : "3" means 12x9, "4" means 16x12, detection matrix
Check period : msec between scan check
Cell sensitivity : motion threshold between 0 to 255; 255 means wait expiring timeout
No motion timeout : the expiring timeout, without detecting motion
-- in Lua
function motion(wait_for_cells, launch_delay, grid_resolution, check_period, cell_sensitivity, no_motion_timeout)
c=launch_delay
g=grid_resolution
k=4*g
l=3*g
e=check_period
f=cell_sensitivity
d=no_motion_timeout
r=0
repeat
r=md_detect_motion(k, l, 1, d, e, f, 1, m, 0, 1, 1, k, l, 16, 11-g, c)
until r >= wait_for_cells
return r
end