UBASIC/Scripts/CanonA640: Motion+Lightning Detect

< UBASIC | Scripts

Revision as of 17:01, July 15, 2011 by (Talk)

593pages on
this wiki

Motion/Lightning Detect

Written for/on: Canon A640
Also works on: Other Canon AXXX (Not tested), Canon A720 (tested), Canon A710 (tested), Canon PowerShot S3 (tested), Canon A540 (tested), A620 (tested), Canon A610 (tested)
Doesn't work on: Canon A650IS - try UBASIC/Scripts:_Multipurpose_Motion_Detection

Motion/Lightning Detect by Johan Van Barel (

The purpose of this script is to take pictures when motion is detected. The screen will be divided into a number of Rows and Columns. The Treshold value determines the treshold 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 Blocksize 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). In this mode, 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 you choose a value > 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.

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 MotionDetect.txt" file to your /CHDK/SCRIPTS/ folder)

Select P, Tv, Av, M or C mode on your Canon A640.


  • Number Of Columns (Default=6)
  • Number Of Rows (Default=4)
  • Treshold (0-255) (Default=24) recommended values: 10=Lightning, 12=Sunshine, 24=Cloudy, 36=Dawn/Dusk
  • Comparison Interval (mSec) (Default=1)
  • Triggering Delay (.1 Sec) (Default=0) recommended values: 0=Ultrafast(Lightning), 16=Normal, 32=Slow
  • Pixel BlockSize (Default=8) number of pixels as one block (smaller size is more accurate but slower)
  • Detection Mode (Default=1) values: 0=U, 1=Y, 2=V, 3=R, 4=G, 5=B
  • Initial Delay (Sec) (Default=1)

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

rem Johan Van Barel 05-04-2008
rem Motion/Lightning-Detect for Canon A640

@title Motion Detect
@param a Cols
@default a 6
@param b Rows
@default b 4
rem Treshold: 10=Lightning, 12=Sunshine, 24=Cloudy, 36=Dawn/Dusk
@param c Threshold (0-255)
@default c 24
@param d Interval (mSec)
@default d 1
rem Delay: 0=Ultrafast(Lightning), 16=Normal, 32=Slow
@param e Delay (.1 Sec)(0=UltraFast)
@default e 0
@param f BlockSize
@default f 8
@param g Mode(U=0,Y=1,V=2)
@default g 1
@param h Initial Delay (Sec)
@default h 1

if a<1 then a=1
if b<1 then b=1
if c<0 then c=0
if e<0 then e=0
if f<1 then f=1
if g<0 then g=0
if g>5 then g=5
if r<0 then r=0
if h<0 then h=0


rem time in msecs md will detect

sleep h*1000

while 1
    if e=0 then
        press "shoot_half"
            get_prop 205 p
        until p=1
    md_detect_motion a, b, g, x, d, c, 1, t, 0, 1, 1, a, b, 0, f, e
    if t>0 then
        click "shoot_full"
            get_prop 205 p
        until p<>1
        print "Pic", i, "(blocks="; t; ")"

Around Wikia's network

Random Wiki