UBASIC/Scripts: Multipurpose Motion Detection

565pages on
this wiki


Revision as of 19:06, November 7, 2012 by Waterwingz (Talk | contribs)

Multipurpose Motion Detector

Written for/on: Powershot S3IS
Also works on:Powershot A460, A470, A495, A530, A550, A560, A570, A590, A630, A650, A720IS, A810, SD700, SD750, SD870, SD950, SD1000, SD1100, SD1100is, SX100IS, SX200IS, SX230HS, S2IS, S5IS (works on A620 with AllBest build as well), G7,G9, Ixus 40/SD300 (but not in full-auto mode), IXUS 70, SX10 IS (using sx10-101a-0.9.8-760-full)

This is a script for motion detection based on the samples provided by MX3. The aim is just to make the life of people that don't know how to program easier.

Documentation/Help (save as a small "Motion Detection.bas" file to your /CHDK/SCRIPTS/ folder)

Tested on S3IS only Requires Fingalo's build v 119 Use with caution!

For better performance, turn off the shot review in camera menu. This script uses luminance variation to detect motion. The timeout of the md_detect_motion command is set to 600 sec. It means that if nothing is detected in 10 min, the detection "trap" is re-armed. To stop the script, press the shutter button anytime.


Shot (0=nf/1=f/2=c/3=t)

0 -> shoot without auto-focusing. It's like pressing the shutter button fully. Use with manual focus to improve speed.

1 -> shoot with auto-focusing. It's like pressing the shutter button half way to the camera find the proper settings and then fully to take the shot.

2 -> shoot holding the shutter button for a while. It needs to be in continuous mode.

3 -> test mode. It doesn't shoot, it just shows information on how many cells detected movement. Use it to tweak the settings.

Continuous shoot (secs)

How long the shutter button will be pressed in continuous mode (in seconds).

Threshold (0-255)

Sensitivity of motion detection. The lower the value, the more sensitive it is.

Compare Interval (msecs)

This is the sample rate in milliseconds. It tests if the image changed in the given interval and if so, a motion is detected.

Compare Interval (secs)

Same as above, but for longer test time. Just to make easier to input longer times. More than 600 secs is useless due to the 600 secs timeout.

Begin Delay (secs)

This defines the time, between the start of the script or the last shoot, and the next shoot triggered by detection. (If you have trouble with your cam making continuous shoots, increase this value.)

Pix step (speed/accuracy adj)

The lower the value, the more sensitive it becomes, but the longer the reaction time becomes. (1-use every pixel, 2-use every second pixel, etc).


Number of columns sensitive to motion (luminance variations) in the MD grid.


Number of rows sensitive to motion (luminance variations) in the MD grid.

Dead frame

This is the width of a frame that is not sensitive to motion. If you want that the motion is detected only when it occurs at the center of your camera display, put a value greater than zero here. Alter the value to 1 and you will see how the detection grid changes in your camera display, by removing the outer ring of detection squares. Increasing to 2 will decrease the rings further, and so on. If you go above the number of rows or columns available, it ignores the value.

Script Code (save as "Motion Detection.bas" to your /CHDK/SCRIPTS/ folder)

rem Author: MLuna - based om MX3 sample script
rem Tested on S3IS only
rem Requires Fingalo's build v 119
rem Use with caution!

@title Motion Detection

rem Shot without auto-focus/with auto-focus/continuously (need to put in continuous mode manually)
rem T implies test mode with MD cells drawing and no shots taken
@param a Shot (0=nf/1=f/2=c/3=t)
@default a 1

rem How long the shutter button will be pressed in continuous mode
@param b Continuos shoot (secs)
@default b 10

@param c Threshold (0-255)
@default c 5

@param d Compare Interval (msecs)
@default d 20

@param e Compare Interval (secs)
@default e 0

rem If this value is too small, the camera goes continuously shooting after the 1st shot.
rem Experiment with this value to find one fitted to your needs
@param f Begin Delay (secs)
@default f 5

@param g Pix step(speed/accuracy adj)
@default g 5

@param h Columns 
@default h 6

@param i Rows 
@default i 6

rem Frame width in which no MD is performed (in cell units)
@param j Dead frame 
@default j 0

if a<0 then let a=0
if a>3 then let a=3
if c<0 then let c=0
if d<0 then let d=0
if e<0 then let e=0
if g<1 then let g=1
if h<1 then let h=1
if i<1 then let i=1
if j<0 then let j=0

rem Conversions secs to msecs
let b=b*1000
let e=e*1000
let f=f*1000

let d=d+e

rem This is the timeout in msecs. After this period, the motion trap is rearmed.
let T=600000

rem Parameters for the Dead Frame
let J=j+1
let H=h-j
let I=i-j

let t=0

print "press Shutter Button to Stop"


	md_detect_motion h, i, 1, T, d, c, 1, t, 1, J, J, H, I, 0, g, f

	if a=0 and t>0 then click "shoot_full"
	if a=1 and t>0 then shoot
	if a=2 and t>0 then goto "continuos"
	if a=3 then goto "test"

	let t=0

goto "repete"

	let X=get_tick_count
	press "shoot_full"

		let U=get_tick_count
		let V=(U-X)
	if V<b then goto "contloop"

	release "shoot_full"
goto "repete"

	if t>0 then print "Detected cells: ",t else print "No detection in 10 min!"
	let t=0
goto "repete"



Note: I have an SD700 and I find that even when I lock focus and exposure and use mode 0 in the script, the camera still meters, as noted in this post,405.msg3216.html#msg3216. So I added

press "shoot_half"

right after the :repete line. This causes the camera to focus and meter right away before motion detection, yielding super fast snap times once motion is found. You would *not* want to do this if your application requires focusing/metering the shot when motion has occurred. I.e. to take a pic of the cat when it walks in front of the camera, you need to focus on the cat after motion is found. My change would give you only blurry cats unless you have guessed the focal distance correctly. But I am using this to catch lightning strikes, so focal length is inf., and shutter speed is locked.


There is just the same problem with A590, and I've solved it almost the same way:

if a=0 then press "shoot_half"

after the :repete line. Then the camera meters right before arming motion detection. You only need it when a==0, as this mode was initially intended to skip metering. This also enables you to get ultra-fast response when not using manual mode.


Turning off the AF Assist beam is a good idea, else after the first shot, it can detect the presence/absence of the beam as motion.

--Clayton, PowerShot SX10 IS

Had issues with the bottom left edge of my A560 detecting continuously. I set the "Dead frame" to 1 to avoid detecting around the edge, and the problem went away.

To test the motion detector response time, I wrote a simple VB app. It changes a large square from black to white, and runs a timer. Just point the camera at the monitor and start MD. Picture gives response time (10ms steps). App (with source) is at

--Ray, A720IS

Advertisement | Your ad here

Around Wikia's network

Random Wiki