UBASIC/Scripts: Universal motion detection


595pages on
this wiki
Add New Page
Add New Page Talk1

Multipurpose Motion Detector Edit

Written for/on: Powershot A610
May also work on other Powershots with minor modifications
Required CHDK build: Fingalo's build 119

The purpose of this script is to take pictures or record a movie when motion is detected. If you selected the movie mode, the script will automatically use continuous time settings for the recording time. If you selected continuous shooting in one of the photo modes, it will automatically switch the shooting parameters to continuous shooting (only when running, not visible in script settings)

The screen will be divided into a number of Rows and Columns. The Threshold value determines the threshold for detection. Recommended experimental values are 10=Lightning, 12=Sunshine, 24=Cloudy, 36=Dawn/Dusk. Motion can be detected in the following Modes: 0=U, 1=Y, 2=V, 3=R, 4=G, 5=B. The Pixel Block size determines how many pixels are seen as one block. Smaller blocks are more accurate but detection will be slower.

An important parameter is the Triggering Delay value. A value of 0 is meant for ultrafast motion detection (fi. lightning).

There is the possibility to choose to pre-focus or not. If pre-focus is selected, the shoot-button will be pressed half before detection is started, so the actual shooting is very fast. Disadvantage is that (in Auto-mode) the focus/aperture/shutter will be fixed before the moving object is detected (except when using all manual settings like manual focus/aperture/shutter). If pre-focus is not selected (=0), the shoot-button will not be half-pressed before detection, so the moving object's focus/aperture/shutter will be set after motion is detected. Important: you should be careful that the value is big enough to avoid shoot-looping after first detection. A value of at least 16 (=1.6 secs) will be ok.

If you want to motion-detect for longer periods, you should disable the power-save options of your camera, so that your camera will not power-off after a certain time.

It is also possible to mask some cells if you want the subject to be in the center, for example (to play with this parameter is the best way to understand/improve it).

ps. for long detection periods you should disable the Power Saving options of your camera. This can be done in the <ALT> menu. In the Miscellaneous Stuff menu item set the Disable LCD Off value to Script, so the display won't turn off while in <ALT> mode or while a script is running.

Documentation/Help (save as a small "Universal MD.txt" file to your /CHDK/SCRIPTS/ folder)

Tested on A610 only Requires Fingalo's build v 119 Use with caution! (Also tested on A710IS with AllBest #50 Apr 11 2008 build, works great!)

For better performance, turn off the shot review in camera menu. This script uses luminance variation to detect motion. The timeout is set by default to 170 sec (it can be modified). It means that if nothing is detected in 2 min 50 seconds, the detection "trap" is re-armed (after 3 minutes, my A610 sleeps). If you selected the video mode, it will take a short video instead of pictures, during the time selected for continuous shoot. To stop the script, press the shutter button anytime.


  • Number Of Columns (Default=6)
  • Number Of Rows (Default=4)
  • Threshold (0-255) (Default=10) Sensitivity of motion detection, recommended values: 10=Lightning, 12=Sunshine, 24=Cloudy, 36=Dawn/Dusk
  • Comparison Interval (mSec) (Default=1)
  • Trigger Delay (0.1 secs) (Default=0) recommended values: 0=Ultrafast(Lightning), 16=Normal, 32=Slow
  • Pixel block size (Default=6) number of pixels as one block (smaller size is more accurate but slower)
  • Detection Mode (Default=1) values: 1=luminance (Y), 0=blue – luminance (U), 2=red – luminance (V) or individual 3=Red, 4=Green or 5=Blue values
  • Initial Delay (Sec) (Default=1)
  • Shoot (0=f/1=nf/2=c) (Default=1) 0=shoot with auto-focusing. 1=shoot without auto-focusing. 2= continuous shooting (needs to be in continuous mode)
  • Continuous shoot (secs) (Default=5) time of continuous shooting or movie recording
  • Pre-focus (0=No) (Default=1) choose if the focus/aperture/shutter values will be fixed before (=1) or after (=0) the motion detection
  • Masking (0=No 1=Incl 2=Excl) (Default=0) masks or not some detection cells
  • Incl/Excl Cols & Rows (Default=0) define which cells are masked (try to understand)

Script Code (save as "Universal MD.bas" to your /CHDK/SCRIPTS/ folder)

rem Author ~SuperManu~
rem For A610
rem May work on others digicII

@title MD Universal
@param a Columns
@default a 6
@param b Rows
@default b 4
rem Threshold: 10=Lightning, 12=Sunshine, 24=Cloudy, 36=Dawn/Dusk
@param c Threshold (0-255)
@default c 10
@param d Comparison Interval (mSec)
@default d 1
rem Delay: 0=Ultrafast(Lightning), 16=Normal, 32=Slow
@param e Trigger Delay (0.1 secs)
@default e 1
@param f Pixel block size
@default f 6
rem 1=luminance (Y), 0=blue – luminance (U), 2=red – luminance (V) or individual 3=Red, 4=Green or 5=Blue values
@param h Measure Mode(0-U,1-Y,2-V)
@default h 1
@param i Masking (0=No 1=Incl 2=Excl)
@default i 0
@param j Incl/Excl Cols & Rows
@default j 0
rem on A610, the camera sleeps after 3 minutes, time-out (k) of 170 seconds ovoid this
rem in the case of pre-focusing (param u), it is nice to not have to long time-out, because exposure condition may change on time
rem feel free to set up k much higher if your camera stay awake a longer time
@param k Time-out period (s)
@default k 170
rem n is the number of shoots taken in continuous mode (continuous mode has to be selected before)
@param n Continuous shoot (s)
@default n 5
rem s value: f=direct shoot, nf=shoot without refocusing, c=continuous shoot without refocus
@param s Shoot (0=f/1=nf/2=c)
@default s 1
rem u value: Pre-focus before detection 0:No, normal shoot, 1:Yes, faster (e.g. lightning storm)
@param u Pre-focus (0=No)
@default u 1
if j>a/2 then j=a/2
if j>b/2 then j=b/2
if j<0 then j=0

if s<0 then let s=0
if s>2 then let s=2
get_prop 0 x
if x=18 then let s=3
get_prop 6 y
if s<>2 and y=1 then
	let s=2
else if s=2 and y<>1 then
	let s=0
if s=0 then let g=1 else let g=0
print "press Shutter Button to Stop"
while 1
	if x<>18 and u<>0 then gosub "make_focus"
	md_detect_motion a, b, h, k, d, c, 1, t, i, j+1, j+1, a-j, b-j, g, f, e
	if t>0 then print t, " cells detected"
	if t=0 then gosub "awake"
	if t>0 and u=0 and s=2 then gosub "make_focus"
	if t>0 and s=1 then click "shoot_full"
	if t>0 and s=2 then gosub "shoot_continuously"
	if t>0 and s=3 then gosub "record_video"
	if t>0 and x<>18 then
			get_prop 205 p
		until p<>1
		let t=0

	 press "shoot_half"
	   get_prop 205 p
	 until p=1

		press "shoot_full"
		sleep n*1000
		release "shoot_full"

		click "shoot_full"
		sleep n*1000
		click "shoot_full"

  release "shoot_half"
  if x<>18 and u=0 then
	click "set"
	click "set"


--SuperManu 09:26, 24 April 2008 (UTC)

Also on Fandom

Random Wiki