UBASIC User Guide D0 3
UBASIC User Guide D0 3
(from ‘UBASIC/TutorialScratchpad’
@
CHDK Wiki)
DanielF
(dfnojunk at shoalhaven dot net dot au)
Page 4 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
Starting Out
Keep these things in mind when writing your scripts:
• Use any text editor to compose your scripts. Make sure it is saving the file as TEXT-
ONLY. Do NOT use programs like Word or other advanced editors. These insert
hidden header info and use non-standard ASCII characters for Return/Line-Feed
commands, quotation marks, and others. The simplest of text-editors will suffice, but
even then watch out not to use TAB for indenting (Notepad or [preferably] Crimson in
Windows, nano in Linux, for example.) Mac users, make sure your script is in UTF-8
encoding, see this special note concerning Macs and Script Files.
• Keep all commands in lower-case. Variables are case-sensitive (a and A are not the
same).
• You are limited to 8k (8192 bytes) for the size of your script in CHDK Build 119 or
later, 2k (2048 bytes) in CHDK Build 116 or earlier.
• Be aware that not all commands work on all cameras, if you plan on sharing your
script try to keep it as generic as possible unless you have a special need for the
camera-specific commands. Try to also provide a more generic version so that all may
benefit from it.
• If using earlier CHDK Builds some commands listed here will not be available to you
and cause errors. Try to keep your version of CHDK up-to-date, from the Wikia
website.
• Keep your script concise and efficient! It takes 10ms of time for each line of your
script to be interpreted by tiny uBASIC. If your script is 10 lines long this takes
1/10th of a second, 100 lines takes a full second, etc. This can greatly impact high-
speed uses. Even rem statements take 10ms to be processed, use them sparingly. See
this section in the discussion area for script timing test results for further info. In
versions 0.5.5 and later, up to 100 rem statements and labels will be executed before a
10ms wait is required.
• Keep in mind the scripting and CHDK works by stealing time from a cooperative
multitasking system – you’re not writing in a time-slicing system like Linux or
Windoze. You need to yield back to the camera with a ‘sleep…’ so the camera can do
its stuff reliably as well. For example, some sleep after print gives the camera time to
write to the LCD without corrupting what’s already on the display. ‘Sleep 100’ seems
to be a commonly-used value, but depending on the camera and other factors, you may
need to sleep for 300–400 ms after certain operations. Failure to do this may result in
(intermittent!) camera lock-ups or shutdowns (there’s a ‘watchdog’ in the camera turns
it off when something goes badly wrong).
• If you write an interesting script, please share it with the rest of us on the User Written
Scripts pages so we may learn from you! Beginner script-writers can be some of the
most creative!
• See these pages for some ideas and examples if you are just starting out: User Written
Scripts
• Two new Scripts Menu options have been added to some special builds, read about
them in the Special Builds Features on the firmware usage page. By using these two
options in conjunctions with these scripts, you are able to execute any script when first
powering on your camera. This allows you an unlimited number of favourite Custom
shooting modes and USB-Remote functionality. You may want to write your scripts
taking these extra features into account.
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 5 of 60
The Script Header
When viewing scripts you’ll often see the opening section look something like this:
@title Interval shooting
@param a Shoot count
@default a 5
@param b Interval (Minutes)
@default b 0
Let’s break down what each of those lines mean and how they are used by CHDK.
@title Your Script Title
This is the title that will appear when you have the script loaded in CHDK and go to
“Scripting Parameters” menu selection. It will appear under the line “----Current Script----“
as well as in the lower-left corner of your viewfinder when you are in <ALT> mode. Keep
your title short (20 characters or less). Otherwise the title will cover up the <ALT> label.
@param x (label)
@default x n
This is where you will define the beginning values of any variables used in your script. These
are often used to set how many exposures you want, how long of a delay you want, how many
bracketing steps you want, etc. These variables can be changed by the end-user from the
“Scripting Parameters” menu items. In that sub-menu, they will appear under the line
“----Script Parameters----“
@param x (label)
The “x” in that line will be one of any lower-case Latin letter from a to j. The (label) is the
text string that will appear in your “----Script Parameters----“ list, to let the end user know
which of the variables they are changing (i.e. number of shots, how many steps, etc.).
Maximum length of label text is 26 characters (including spaces) (to fit in Parameters Menu
space).
Up to 10 @param statements (user-controllable variables) may be used in any one script.
Note: The latest builds of CHDK now allow you to have up to 52 variables, a to z and A to Z.
But the user-definable variables must be in lower-case if used for that purpose. Also be
aware that lower and uppercase variables are unrelated. If you use a lower-case j for a
variable, it is not the same as using J, and vice-versa.
@default x n
This statement sets up the default, or beginning value of your (lower-case letter) variable,
where “x” is the variable from the @param statement above, and “n” is the default value to
start with. This value is only used when a script is loaded for the first time.
Notes:
If there is no @title command the filename of script is used. If there are no @param
commands CHDK assumes that there are three adjustable variables: a, b and c. Remember –
when naming @param variables, use only a character from a thru z.
After your default variable values have been defined here, it is good to add some lines right
after this section to ensure those numbers will be used in case the user has input 0 (zero) for
some value that needs to be higher (or lower). You will see this in scripts as:
if a<2 then let a=5
Page 6 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
If your default value that you wanted the user to start out at for parameter variable a was 5,
then if they left that setting at 0 (zero) or 1, then this will automatically increase that
variable’s value back up to 5.
After you have set up your variable parameters, then comes the crux of your script, the part
that does the actual work and tells the camera what to do, when to do it, and what buttons or
commands need to be executed. Since we are working with a very small subset of the larger
uBASIC programming language, it might be good to list and explain only those that are
available to the CHDK script writer.
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 7 of 60
The first line “for n=2 to a” means that the “for / to / next” loop will run while variable-n
equals the sequence of numbers of 2 up to whatever the number variable-a has been assigned
to. The commands that take place in the loop are contained between the FOR statement and
the NEXT statement. “next n” tells the loop to go back to the beginning “for ...” statement
until the a value has been reached.
For example:
for n=1 to 10
print “This is line number”, n
next n
This will produce the sequence of:
This is line number 1
This is line number 2
This is line number 3
.
.
.
This is line number 9
This is line number 10
and then that loop will end and go on to the next sequence of commands.
Do / Until Loops
Another method of creating loops for repetitive instructions or when waiting for some
condition to be true. Code within a Do/Until loop will always be executed at least once
(unlike While/Wend loops)
Usage:
do
statement
statement
statement
...
until relation
Where relation may be any logical expression. When it is true, the loop will exit.
Example:
rem set some starting values for the variables
y=0
x=5
rem start do-loop
do
rem increment x by 10 each time
x=x+10
rem increment y by 1 each time
y=y+1
rem print results to viewfinder mini-console
print “This DO loop happened”, y; “times.”
rem repeating do-loop until x is equal to the value of 55
until x=55
end
Page 8 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
While / Wend Loops
Similar to the DO / UNTIL loops. The loop will continue to execute while some statement
remains true, and will end, wend (while-end), when that statement is no longer true. Unlike
Do/Until loops, code within a While/Wend loop may never be run, if the test condition is
already false when the While statement is first reached.
Usage:
while relation
statement
statement
statement
...
wend
Example:
x=0
while x<200
x=x+25
print “The value of x is”, x
wend
This loop will increment the value of x by 25 each time and print the value of x, as long as
(while) the variable x remains less-than 200
Sub-Routines
For complex programming tasks, it is often helpful to split the program into smaller
subroutines that can be called with gosub and goto commands. A sub-routine can be nearly
anything but it is generally used for a set of commands that will be called-up more than once.
Instead of writing the same set of commands over and over again you put that code into a
subroutine and then call it up from within the main program by using gosub ”label” or
goto ”label”. Subroutines are generally placed after the main code. A labelled subroutine
that will be called by gosub “label” needs to end with the return command, to tell the script
to jump out of that section of code and return back to from where it was called.
GOSUB and GOTO are similar but you should refrain from using GOTO unless you know
what you are doing. GOSUB will always return from a subroutine as soon as it reaches the
RETURN command. GOTO does not behave this way. GOTO should only be used when
you are going to jump to a section of the script one time and under special circumstances.
:display
print x
return
A longer example that would capture 3 images with increased ISO settings would look
something like this:
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 9 of 60
shoot
for i=1 to 3
gosub “incISO”
shoot
next i
for i=1 to 3
gosub “decISO”
next
end
:incISO
click “menu”
[some more clicks]
return
:decISO
click “menu”
[some more clicks]
return
An example using the GOTO command taken from an endless intervalometer script. NOTE:
This situation creates an endless loop. Until you manually override the script it will continue.
This is generally considered BAD FORM! Any script should include/end-with all the
commands to reset the camera to its original configuration prior to running the script, and
properly end with the END command. Do not do this kind of thing unless you have a special
need for it and know what you are doing.
@title Interval Shooting Non-stop
@param a Interval (Minutes)
@default a 0
@param b Interval (Seconds)
@default b 5
@param c Interval (10th Seconds)
@default c 0
t=a*60000+b*1000+c*100
n=1
sleep 1000
:shot
print “Shot number”, n
shoot
n=n+1
sleep t
goto “shot”
Page 10 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
Note: You are limited to 25 characters being displayed (without wrapping) in any one line of
text. You may also include the values of variables or integer-equations in your print
statement. CHR$() is not supported (indeed, it crashes CHDK!), nor is PRINT USING…
Examples:
rem Print total duration of interval to viewfinder
print “Total time:”, t*a/60000; “min”, t*a%60000/1000; “sec”
sleep 1000
rem Start actual camera operation in a loop
print “Shoot 1 of”, a
shoot
for n=2 to a
sleep t
print “Shoot”, n, “of”, a
shoot
next n
Note that the comma (,) is replaced in the output with a space while a semicolon (;) results in
no whitespace.
Example:
print “C”,”H”,”D”,”K”
print “C”;”H”;”D”;”K”
will result in
C H D K
CHDK
However, note that a semicolon at the end of a print statement, e.g.
print “c=“, c;
does not suppress the newline (CR/LF) like it should!
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 11 of 60
Example:
@title printscreen test
@param a None
@default a 0
@param n FileNum
@default n 1
print_screen n
print “START“, n
print_screen 0
n=n+1
print “Not written to file”
print_screen n
print “This should be written to next file.”
print “a=“;a
print_screen 0
end
This would create two files with the following contents:
\CHDK\LOGS\LOG_0001.TXT:
START 1
\CHDK\LOGS\LOG_0002.TXT:
This should be written to next file.
a=0
Page 12 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
Usage:
t = get_day_seconds
For a simple example using this function to wait until a specific time of day before
continuing, see get_day_seconds_example. (That’s a fairly ‘messy’ example; a better
example might be ‘Nite&Day.bas’.)
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 13 of 60
The “end” Command
This should be the last line in your script. It tells the script to cease all operations and return
camera control back to you. Before ending a script, it is good form to always reset any
camera settings that the script took control of during initialization or running of your routine,
so that the end user doesn’t have to undo all the key-presses and menu changes that the script
created.
Fingalo’s Builds
Available from: Fingalo’s CHDK2
end
If using the default values this simple script will start out at the Near Focus value of 2500mm,
increment that value by 100mm every time, shoot an image, and exit when the focus has
reached or passed 4500mm.
Page 14 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
IF / THEN / ELSE / ENDIF – Multiple Statements
Fingalo reports: “Seems to have some bug when not using the else in nested if constructs!”
Usage:
if relation then
statement
statement
statement
...
else
statement
statement
statement
...
endif
The standard single-statement if...then...else... loop still works, but it cannot be used inside
the if...then...else...endif loops.
NOTE: nesting levels for all loop methods are currently set to 4 for all new constructs.
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 15 of 60
hdr_bracket_1/3_ev_steps 3
each_photo_darker
“ Press switch”
wait_for_switch_press
“ Each image darker”
start_custom_timer_sequence
wait_until_done
uBASIC variables
Variables are represented by single letters of the Latin alphabet: a–z (in some versions, A–Z
also). All variables are 32-bit signed integers (–2147483648 to +2147483647). However,
due to a bug in the current main release, values greater than 6 decimal digits cannot be
assigned (i.e. 999,999 maximum).
Labels
A label must be the only statement in a line and start with a colon (:). Maximum string
length??
‘Restore’ label
There’s one important pre-defined label in uBASIC:
:restore
uBASIC tries to jump to label “restore” when you fully press the shutter button (which is
how you terminate a script prematurely).
The purpose of this label is to allow scriptwriters to code some ‘clean-up’ routine to restore
the camera to ‘normal’ settings after their script has forced some other settings. This could be
important, for example, if your script uses AF lock, where aborting prematurely may leave the
camera in a state where it cannot focus (without cycling power). So by using the restore
label you could include code to clear the AF lock and turn the display on.
Page 16 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
Example: Let’s say you have computed a number to equal how many seconds
something will take (its duration). Such as s=(some math expression) Where s is
being assigned the number of seconds computed.
Now you want to display that as minutes and seconds. You will need a print statement
that effectively does this:
print “Total Time:” , s/60; “min”, (the remainder of s/60); “sec”
There is a very simple way to do this using the % command. Think of % as “the
remainder of s being divided by”. So all you need to do is have this print statement:
print “Total Time:” , s/60; “min”, s%60; “sec”
If s had the value of 328 seconds, then this would compute like this:
Total Time: (328/60)=5 min (the remainder of 328/60)=28 sec
and thus print like this:
Total Time: 5 min 28 sec
Some further notes:
< Less Than
> Greater Than
= Equal
<= Less Than or Equal
>= Greater Than or Equal
<> Not Equal
are relational operators, while
& And
| Or
^ Xor
are bitwise operators, not logic operators. (The logic operators of and, or, and not have been
added to CHDK build #144 or later.) Example use of the bitwise (&, |, and ^) binary
operators are:
e=5|3
print e
will return “7”.
5&3 will result in “1”
5^3 will result in “6”
For an explanation, refer to bitwise operators
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 17 of 60
are being correctly calculated, although one would preferably use parentheses just to
understand what is being calculated.
Also priority for “&” and “|” has been changed the same way.
NOTE: Multiple relational operators are allowed!
click “button-name”
Presses the button momentarily, used for one time, instantaneous commands. This
will be the most often used method of issuing a camera command.
press “button-name”
Presses and HOLDS the required camera button, it remains pressed until the same
button-name is given the release command. Some camera commands can only be
accessed when a button is held down during use.
Example: In Manual Focus in the S-series cameras the MF button needs to be held
down while the focus commands are being issued. Or when shooting in high-speed
burst mode, then the shutter button must be held down during its needed duration with
the press “shoot_full” command.
release “button-name”
Page 18 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
sleep 2000
press “shoot_half”
sleep 1000
click “erase”
click “down”
release “shoot_half”
shoot
Records an image.
This command is similar to the click “shoot_full” command (see below), but it waits for the
camera to perform some normally automatic actions, such as auto-focusing, charging the
flash, etc. For example: if in AUTO, P, Tv, Av, or any SCN modes, using the “shoot”
command causes the camera to check focus and exposure for each shot. When “shoot” is
used in intervalometer scripts this far surpasses the camera’s own built-in intervalometer in
that the camera only sets exposure and focus once for the initial exposure, as if it was only
using the “click ‘shoot_full’” command. This “shoot” command in an intervalometer script
allows it to compensate for all the things that can change over the course of many minutes and
hours. For more precise manual control of the camera in scripts, see the click “shoot_half”,
click “shoot_full”, when used in conjunction with the get_tv, set_tv, set_tv_rel, get_av,
set_av, set_av_rel commands below.
click/press/release “set”
Actuates the set button.
Note: press and release would not normally be used with this button, but without knowing
each and every camera model’s functions and the creative ways some might use scripts, these
two command-methods are also mentioned.
click/press/release “shoot_half”
Actuates the shutter-release in the half-press position. This is often used to lock focus,
exposure, or other camera settings.
(Note: In dim light it can sometimes take up to 2+ seconds for a camera to hunt for focus. If
your script is using this command to set auto-focus, and is designed for or intended to also be
used in low-light conditions, it would be good to follow a press “shoot_half” command with
a sleep x command, where x can have a value from 1500 to 2500.)
click/press/release “shoot_full”
Actuates the shutter-release button completely, regardless of whether the camera has finished
charging the flash or other normally automatic camera operations.
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 19 of 60
using click the S-series cameras implement this command very slowly. Here’s an example of
how it may be used in a loop:
for s=2 to a print “Shoot”, s, “of”, a
for n=1 to b shoot
print “Zooming-in “, n; “...” next s
click “zoom_in”
sleep 600
next n
Note the 0.6 second sleep command after each zoom_in step.
click/press/release “menu”
Actuates the menu button.
This is used to alter some of the cameras settings that can only be set through the record
menus, to set up the camera before or during a script session.
Note: press and release would not normally be used with this button, but without knowing
each and every camera model’s functions and the creative ways some might use scripts, these
two command methods are also mentioned.
Example:
:slowsync click “down”
click “menu” sleep 400
sleep 400 click “right”
click “down” sleep 400
sleep 400 click “menu”
click “down” sleep 400
sleep 400 return
This :slowsync” sub-routine will initialize the camera’s flash setting into slow-sync mode.
Note also the sleep commands, giving your camera time to respond to the new settings
between each directional button-press. Button-press delay times may be camera specific.
(Meaning it might be a good idea to set up a user-defined variable for these in some scripts to
save on script-size and make the script more adaptable to more makes and models of cameras.
A note could be made in the accompanying script’s documentation on what button-press
delays are needed per make and model of camera.)
click/press/release “display”
Actuates the camera’s display button.
Note: press and release would not normally be used with this button, but without knowing
each and every camera model’s functions and the creative ways some might use scripts, these
two command-methods are also mentioned.
click/press/release “print”
Actuates the camera’s print button. (Note: actuates the shortcut button for S-series cameras.)
Note: press and release would not normally be used with this button, but without knowing
each and every camera model’s functions and the creative ways some might use scripts, these
two command-methods are also mentioned.
Page 20 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
click/press/release “erase”
Actuates the camera’s erase button. (Note: actuates the FUNC (function) button for S-series
cameras.)
This will often be used to select some shooting parameters like exposure-compensation,
movie frame-rates, white-balance settings, ... any of the options that can be reached by
pressing this button on your camera. It is then used in conjunction with directional button-
presses to choose the desired settings.
Note: press and release would not normally be used with this button, but without knowing
each and every camera model’s functions and the creative ways some might use scripts, these
two command-methods are also mentioned.
Example:
@title EXP bracketing
@param a Number of ±steps
@default a 2
@param b Step size (1/3EV)
@default b 3
sleep 1000
print “Preparing...”
click “erase”
for n=1 to a*b
click “left”
next n
print “Finalizing...”
for n=1 to a*b
click “left”
next n
click “erase”
end
In this “Exposure Bracketing” script, if you follow the embedded button-presses, you’ll see
that your Exposure Compensation setting is being selected by using the click “erase”
command. The click “right” and click “left” commands are moving the Exposure
compensation settings to the right and left (more exposure and less exposure), just as you
would if you were doing this manually from one shot to the next.
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 21 of 60
click/press/release “iso”, “flash”, “mf”, “macro”, “video”, “timer”
(S-series only)
Actuates the S-series specific buttons.
(This will need to be added to with a few examples, specifically in using the new press/release
commands with some of these.)
Page 22 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
manual mode, probably also in Av and Tv modes). There’s a test script for these commands
in the “user written scripts”
The commands are
get_tv target
set_tv_rel val
set_tv val
get_av target
set_av_rel val
set_av val
Target is the name of a variable (a, b, … z), val is an expression.
An example of setting and printing the values.
:set_get
set_av c
set_tv b
print “AV,TV set to”,c,b
sleep 1000
click “shoot_half”
sleep 100
get_av n
get_tv m
print “AV,TV got”,n,m
end
You can change the settings relative to existing values (this might make bracketing easier and
faster):
rem increase light (1/3+1/3 steps)
set_tv_rel 0-1
set_av_rel 0-1
shoot
end
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 23 of 60
commands will not alter the shutter-speed or aperture, try moving them to a position just
before any press “shoot_half/full” or click “timer” (unique s-series) commands. For an
example see the “Lightning Photography” scripts for where the set_tv command had to be
placed before it would work. It was tried in all other locations before the actual shooting was
to begin, setting the shutter-speed in other locations in the script wouldn’t work otherwise.
Canon firmware uses these ‘?v’ values in many ways, depending on operating mode. The
most usual way is that they are set by auto-exposure during half press. Thus to override auto-
exposure these prop_cases typically must be set after auto-exposure is finished. Otherwise
Canon firmware would just overwrite it and the command wouldn’t have any effect.
Page 24 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
The set_focus and get_focus commands
(CHDK Build 125 or greater)
Syntax:
set_focus x (where x is the distance in mm)
get_focus x (the distance value placed in variable x)
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 25 of 60
md_detect_motion
This command is the main crux of setting all feature parameters.
/--/-COLUMNS, ROWS to split picture into
| | MEASURE MODE (Y,U,V R,G,B) – U=0, Y=1, V=2, R=3, G=4, B=5
| | | TIMEOUT
| | | | COMPARISON INTERVAL (msec)
| | | | | THRESHOLD (difference in cell to trigger detection)
| | | | | | DRAW GRID (0=no, 1=yes)
| | | | | | | RETURN VARIABLE number of cells with motion detected
| | | | | | | | OPTIONAL PARAMETERS:
| | | | | | | | REGION (masking) mode: 0=no regions, 1=include, 2=exclude
| | | | | | | | |
| | | | | | | | | REGION FIRST COLUMN
| | | | | | | | | | REGION FIRST ROW
| | | | | | | | | | | REGION LAST COLUM
| | | | | | | | | | | | REGION LAST ROW
| | | | | | | | | | | | | PARAMETERS: 1=make immediate shoot,
| | | | | | | | | | | | | | 2=log debug information into file (* see note below!),
| | | | | | | | | | | | | | 4=dump liveview image from RAM to a file,
| | | | | | | | | | | | | | 8=on immediate shoot, don’t release shutter.
| | | | | | | | | | | | | | OR-ed values are accepted, e.g. use 9 for
| | | | | | | | | | | | | | immediate shoot & don’t release shutter
| | | | | | | | | | | | | | PIXELS STEP – Speed vs Accuracy adjustments
| | | | | | | | | | | | | | | (1=use every pixel,
| | | | | | | | | | | | | | | 2=use every second pixel, etc)
| | | | | | | | | | | | | | | MILLISECONDS DELAY to begin triggering
| | | | | | | | | | | | | | | | Can be useful for calibration with
| | | | | | | | | | | | | | | | DRAW-GRID option.
| | | | | | | | | | | | | | | |
md_detect_motion a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p
Page 26 of 48 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
The minimum number of variables that must be set with this command are:
md_detect_motion a, b, c, d, e, f, g, h
Timeout (d): [mx3] is time in milliseconds for which md_detect_motion will block execution
of next uBASIC commands, if during this period no motion is detected. This parameter is
useful if you want to execute periodically some other uBASIC commands together with MD.
E.g. MD routine waits for changes for 1 second; if no motion detected, script can continue to
execute some other code and then, if required, can resume motion detection by again calling
md_detect_motion. So timeout is just the time for which MD routine will wait for changes.
In practice, this TIMEOUT value (parameter d) should be greater than the MILLISECONDS
DELAY (parameter p), or else you will always get RETURN VARIABLE (parameter h) = 0.
Comparison Interval (e): The time delay in milliseconds in which to check for a change in a
cell’s values. If you need to filter out small changes made frequently by faster moving objects
(leaves in the wind, or flying insects, for example) you would increase this value so that timed
samples are further apart. Very useful when trying to detect changes in very slow moving
subjects, e.g. snails, slime-moulds, a slow-moving criminal trying to avoid motion detection
devices , etc.
h – RETURNED VARIABLE: this variable is used for deciding whether you want to shoot.
It contains a count of cells where the change is more than the specified threshold value.
Example: if h>0 then shoot
n=2 (debug mode): Since build #684 (Jan 18th 2009), this debug feature has been removed to
save RAM. To use it, a custom CHDK version must now be built (OPT_MD_DEBUG=1 in
makefile.inc will enable motion detector debug).
(Insert more information on variable parameter functions and uses as they become known or
more familiar.)
md_get_cell_diff
[mx3] This is an optional procedure for those people who want to know where in the scene
detection actually happened. This procedure is designed for scene change advanced analysis.
I’m not sure many people will need to use it. Most people will use the “h” variable from
above example to trigger shooting.
Usage: md_get_cell_diff (column), (row), x
where x will be difference of 0 to 255 between the last and present change in that cell.
Triggering a script to shoot on this value may be done by detecting no change, or
however much sensitivity you would like to detect in that cell.
Examples:
If you would like to have the camera shoot an image when all motion stops, use:
if x=0 then “shoot”
To shoot an image when any motion is detected at all use:
if x>0 then “shoot”
Interesting use of MD:
The following was copied from a post where MX3 mentions a feature of md_get_cell_diff
that was never documented before.
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 27 of 60
Nobody tried to use MD to get overall luminosity to automatically adjust shutter speed
override?
MD setup:
set delay_interval to 2–3 secs
timeout=delay_interval+1
threshold=255 (so it will not trigger)
cols=1
rows=1
md_get_cell_diff 1, 1, overall_luminosity
shutter_override_time = some_formula(overall_luminosity)
I don’t have camera nearby to test it.
I have thought about time-lapse movie script that would automatically override shutter speed
at night. I’m planning to make 2 days time-lapse movie (it seems 8GB SD card and power
adapter will help also )
NOTE: when MD stops working on timeout, cells contain absolute values instead of
difference.
The most important info is contained in that final “NOTE”!
Referring to the ‘md_detect_motion’ command-parameters in the WIKI article, ‘a’ and ‘b’
define the number of rows and columns to split the screen into. (If values less than zero are
entered or if total number of cells is greater than 1024, it defaults to 3 x 3.)
Parameter ‘g’ determines if the grid showing the detected cells is displayed.
Parameters ‘j, k, l, m’ define a sub-area of the screen where motion-detection is restricted-to
or excluded-from.
Parameter ‘i’ determines if the region is inclusion/exclusion or do not use regions.
You may detect motion based on changes of luminance (Y), blue-luminance (U), red-
luminance (V) or individual R, G or B values.
Parameter ‘c’ sets that mode.
(For an example of an image split into it’s YUV components, see the WIKI article.)
For non-specialised use, luminance (c = 1) will be used.
You then need to set a threshold-value (in parameter ‘f’) for the desired mode that will not
result in triggering in ‘normal’ operation.
The motion-detection event may be triggered by quick or slow changes in the screen image;
set a suitable value with parameter ‘e’.
The greatest accuracy of movement-detection results when every pixel is sampled, but a faster
response (suitable for some applications) may be obtained with a larger pixel-step.
Set an appropriate value in parameter ‘o’.
Set a maximum-time for a motion-detection event to occur with parameter ‘d’ so that after
that time the script-command terminates.
Page 28 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
Motion-detection Parameters:
columns, input parameter. Number of columns to split screen into
rows, input parameter. Number of rows to split screen into
pixel_measure_mode, input parameter. 1 for Y, 2 for U, 3 for V, 4 for gray, 5 for R, 6 for G,
7 for B
detection_timeout, input parameter. Number of milliseconds to abort detection.
detected_cells_count will be 0 for timeout condition
measure_interval, input parameter. Number of milliseconds between comparison of two
pictures
threshold, input parameter. Difference value for which procedure will trigger detection of
changes
draw_grid, Boolean input parameter. True (1) to draw grid(detected sectors/cells). False (0)
to not display grid/detected sectors
detected_cells_count, output parameter. Count of cells where pixel values differs enough to
trigger motion detection
clipping, allows to exclude some region from motion detection triggering, or use only selected
area to make motion detection
I’m not sure that following parameters are required but using them anyway
clipping_region_mode, input parameter. 0 = no clipping regions, 1 = excludes selected region
from motion detection, 2 = use only this region to make motion detection
clipping_region_column1, input parameter.
clipping_region_row1, input parameter. This is top-left corner of clipping region
clipping_region_column2, input parameter.
clipping_region_row2, input parameter. This is right bottom corner of clipping region)
function md_get_cell_diff (col [in] = column of the cell we are requesting, row [in] = row of
the cell we are requesting, val [out] = value of difference between measurements/
comparisons)
Reserved parameters clipping regions, pixel_measure_mode, draw_grid
Fingalo’s Builds
Available from: Fingalo’s CHDK2
Added commands: SET_LED, GET_VBATT, SET_RAW, and SET_PROP / GET_PROP
Used to control the external LED lamps, read the battery voltage, turn RAW image recording
on and off from scripts, and to set/read “property-case” values (respectively).
NOTE: Fingalo has also included the amazing Motion-Detection command from MX3 as
outlined above. See MX3’s Motion Detection Commands
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 29 of 60
Fingalo says, “ONLY for S3 (and S2 I guess)”
I tried with A560 and it worked, but there is no LED 10 (and no 6). And I think these work
with any A500-series camera.
Parameter a is the LED-lamp as follows:
a LED Lamp
4 GREEN (by power switch on S3 and A560)
5 YELLOW (by power switch on S3 and under green LED on A560 )
6 (not used)
7 ORANGE (red LED on back of S3 and same place than green on A560)
8 BLUE
9 Focus Assist/Auto-Focus Lamp/AF Lamp (bright green on S3 & bright orange on A560)
10 Timer/Tally Lamp (bright orange lamp in front on S3)
Parameter b
0 LED is off, 1 LED is on
Parameter c (optional) is brightness
0–200, (Fingalo says, “Seems to work only on the blue LED.”)
(LEDs work on A560, but brightness doesn’t work for any of them.)
Example:
rem Turn on AF_Lamp, Focus Assist Lamp
set_led 9 1
Page 30 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
SET_RAW enable/disable RAW Recording
(Fingalo’s builds only [AllBest too])
Usage:
set_raw a
where: a = 0 RAW recording is OFF; a = 1 RAW recording is ON
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 31 of 60
EXAMPLE: A570IS ISO is at #149, not #21 like on the S3IS, and the white
balance control is #268, not #206. Furthermore, some Digic III cameras (G7,
SD800IS, SD900) use the Digic II property set.
You are advised to confirm that these properties are appropriate for your
camera before you attempt to change them.
Usage:
set_prop propid value
get_prop propid value
where propid may be any of the following (for S3IS?? – see later tables for Digic II & III):
PropID Description
0 Shooting mode dial position
1 Photo effect
5 White balance
6 Drive mode (S3 values: 0 = single, 1 = continuous, 2 = timer)
8 Hi-speed continuous mode (S3: 1 = OFF, 0 = ON
9 Metering mode (S3 values: 0 = eval 1 = spot 2 = centre)
11 Macro (S3 values: 0 = normal, 1 = macro, 2 = super macro)
12 Manual Focus (S3 values: 1 = manual, 0 = auto)
14 Delay of self-timer (appears to be time in milliseconds)
16 Flash mode (S3: 2 = flash closed, otherwise 0 = auto, 1 = ON)
18 Red eye mode (S3: 0 = OFF, 1 = ON)
19 Flash slow sync (S3: 0 = OFF, 1 = ON)
20 Flash Sync Curtain (S3: 0 = first, 1 = second)
21 ISO value (S3: 0 = auto, 1 = ISO-HI, or actual ISO: 80,100,200,400,800)
23 Image quality (S3 values: 0, 1, 2 from best to worst)
24 Image resolution (S3 values: 0, 1, 2, 4, 8 for L, M1, M2, S, W)
25, 26 EV correction (positive or negative, 96 units per stop)
28 Flash correction (same units as 25/26)
32 Exp bracket range (Same units as 25/26: e.g. 96 = ±1 stop range)
34 Focus bracket range 2 = Smallest, 1 = Medium, 0 = largest
36 Bracket mode: 0 = NONE, 1 = exposure, 2 = focus
37 Orientation sensor
39 Chosen Av (by user)
40 Chosen Tv (by user)
65 Focus distance
67 Focus OK: 1 = Yes, 0 = NO
Page 32 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
PropID Description
68 Coming Av
69 Coming Tv
74 AE lock: 1 = ON, 0 = OFF
126 Video FPS (15, 30 or 60. Don’t change here!)
127, 128 Video resolution (S3: 2, 1 for 640480; 1, 0 for 320240)
177 Intervalometer: # of shots (0 if not activated)
205 ? ‘1’ during shooting process
206 “MyColors?” mode (see link below)
218 Custom timer continuous: # of shots to be taken
219 Self-Timer setting: 0 = 2 sec, 1 = 10 sec, 2 = custom/continuous
And value may be any that is appropriate for that particular propid.
2, 3, 4, 207, 208, 209, 210 contain individual parameters for the “Custom” MyColors setting
For Digic II based camera (and some Digic III cameras: G7, SD800IS, SD900):
1 Photo effect
AF
13 *
S3IS: 0=Single, 1=continuous
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 33 of 60
# R/W Description A620 S3IS SD900
ISO value
21 *
S3IS: 0=auto, 1=ISO-HI, or actual ISO: 80, 100, etc
Image quality
23 *
S3IS: 0, 1, 2 from best to worst
Image resolution
24 *
S3IS: 0, 1, 2, 4, 8 for L, M1, M2, S, W
Orientation sensor
37 *
SD900: 0=Normal, 270=Left, 90=Right
Page 34 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
# R/W Description A620 S3IS SD900
Stitch Mode
S3IS: 0=from left to right, 1=from right to left, 2=bottom to
52 *
top, 3=top to bottom,
4=top left→top right→bottom left→bottom right
Digital Zoom
57 S3IS: 0–6, 0 is no digital zoom, 6 is “most digital zoom” * *
(48x); SD900: 3=2.3x, 1=1.4x, 0=no Zoom / Default
AF-light
63 * *
S3IS/SD900: 1=ON, 2=OFF
64 Manual settings
68 Coming Av
69 Coming Tv
71 422
73 371
AE lock activated
74 *
S3IS: 1=yes, 0=no; see 205 (also in movie mode)
75 0
76 0
77 min available Av
78 R Flash fired
79 RW Flash fire
80 1
81 1
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 35 of 60
# R/W Description A620 S3IS SD900
82–
Often the same, sometimes slightly different
85
86 Av
87 0
88 2
89 0
90 0
91 5
92 0
93 0
94 0
95 8
96 10
Zoom step
99 S3IS: 0 = open wide, 128 = maximum zoom, on A620 this *
will be 8 I guess
Video FPS
126 *
SD900: 30=30FPS, 15=15FPS
Video resolution
127 *
SD900: 3=1024768, 2=640480, 1=320240, 0=160120
Page 36 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
# R/W Description A620 S3IS SD900
Language Setting:
2=English, 258=German, 514=French, 770=Dutch,
1026=Danish, 1282=Finnish, 1538=Italian,
1794=Norwegian, 2050=Swedish, 2306=Spanish,
2562=simplified Chinese, 2818=Russian,
3074=Portuguese, 3330=Greek, 3586=Polish, 3842=Czech,
4098=Hungarian, 4354=Turkish, 4610=traditional Chinese,
196 R * * *
4866=Korean, 5122=Thai, 5378=Arabic, 5634=Japanese)
(Confirmed on S3is & A620 and several other cams)
NOTE: I just discovered that this setting directly is related
to NTSC/PAL setting. These numbers are valid for PAL.
If you set to NTSC, all these numbers are decremented by
1, meaning English is 1 and German is 257 in NTSC
(instead of 2 and 258).
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 37 of 60
# R/W Description A620 S3IS SD900
Mic Level
227 *
S3IS: 1–5, low to high
Page 38 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
# R/W Description A620 S3IS SD900
237 47
260 500
274 411
298 201329664
299 –201264128
# R/W Description
3 RO Auto-Exposure Lock
11 RW Auto-Focus Lock
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 39 of 60
# R/W Description
Min Av (widest available aperture for current zoom, updates on half shoot), 96
24 per 1 stop. Note: on SD990, this includes the ND filter, if the ND was selected
by the camera in an auto mode.
Min Av (widest available aperture for current zoom, updates on half shoot), 96
25
per 1 stop. Note: on SD990, this does not include the ND filter.
User Av (user selected “market” value in M/Av modes, writes become effective
26 RW
on LCD on half shoot), 96 per 1 stop.
34 Bv
60 Converter (0, 1, 2, 4)
Subject Dist 2 (“Near limit”, most linear to real distance. You can get-and-set
65 this prop_id, but it doesn’t affect the near limit and/or the focus – tested with
A590).
66 RW Date stamp (0=Off, 1=Date, 2=Date & Time); only postcard mode
Page 40 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
# R/W Description
Digital Zoom depth (0–6: 0=none, 1–6 are for camera dependent zoom steps,
95 RO
higher number = higher magnification)
Changes with light only in M-Mode (–445 is completely dark and 328 is fully
103
bright. Also depends on the current av/tv setting.)
Display mode (record mode only) (0, 1, 2, 3 = show info icons, do not show info
105
icons, LCD off, EVF)
107, RO,
Exposure Shift/Compensation (32 for 1/3 stop)
207 RW
External flash state (0=Absent, 1=Present and turned on, 2=Present but turned
111
off)
122 RO Flash used in last shot (0, 1 = Flash not used, Flash used)
127 RW Flash exposure compensation (units: ±96 per 1 stop range, if #121=0)
143 RW Flash Mode (0, 1, 2 = flash auto, flash on, flash off)
149 RW ISO Mode (0=auto, 1=high auto, 100=manual ISO 100 etc)
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 41 of 60
# R/W Description
(? Scene dependent/ stays 0 when scene is black or when scene has optimal
196
light)
Flash Ready (1 during shutter half press after metering if camera is going to use
208
flash)
Picture Size (0, 1, 2, 3, 4, 5, 6, 8 = L, M1, M2, M3, S, RAW (on g9), Postcard,
218 RW
W)
“Long time exposure” indicator: 1 always in “Night snapshot” scene mode and
227
when the exposure time is set to 1s
Optical Zoom position, same as #192, 0–128 on SX10, same as #195 on A590,
251
0–7 on A590
Page 42 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
# R/W Description
User Tv (user selected value in M/Tv modes, writes become effective on LCD
264 RW
on half shoot), 96 per 1 stop
White Balance Value (Middle around 900. Less corrects red and higher corrects
269
blue. Can’t override.)
ND filter status (0 off, 1 on) for manual mode. NOT updated in auto modes
296 RO
(SD990). Note: you can write to it, but actual ND state doesn’t change.
On the A590, it seems that possible propIDs range from 0 to 287. IDs greater than 287 will
always return “14”.
Additional information (hopefully growing) about what values might work for some of these
properties can be found at the following link: Property case exploration page. This link also
has a more complete description of the MyColors settings (contrast, saturation, sharpness,
individual color intensities, etc).
Example script for setting and viewing Prop_IDs.
@title popcase
@param a propid
@default a 0
@param b value
@default b 0
:loop
wait_click
is_key k “left”
if k=1 then set_prop a b
is_key k “set”
if k=1 then goto “lend”
get_prop a b
print a,b
goto “loop”
:lend
end
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 43 of 60
Allbest’s Builds
The Allbest build is a major rewrite of CHDK, in many ways. It also includes many new
uBASIC commands. Below is a partial list of the complete list of available commands, those
which are unique to the Allbest build. These have not been documented in total yet, and more
uBASIC commands are being added frequently. (Please see the CHDK forum for discussions
of any works in progress.)
NOTE: Syntax usage in most cases is command_name x, where x either sets or returns the
value in that command. Unless stated otherwise, assume this usage syntax. Otherwise they
may be acting as their own variable, and may be used as-is in a command string. Example:
get_vbatt is its own variable. It can either be assigned to another variable with x=get_vbatt,
or used on its own as in print get_vbatt. The different types of uBASIC command syntax
will be clarified as needed or as discovered. (Developers don’t document things very well.
We, as end-users, sometimes have to find these things by trial-and-error, or be perceived as a
major nuisance by hounding them for any clues into what they did. I use both methods. )
:set_shutter
print “Tv set to”,t
wait_click
is_key k “set”
if k=1 then goto “k_set”
is_key k “down”
if k=1 then t=t-32
k=0
is_key k “up”
if k=1 then t=t+32
k=0
set_tv96_direct t
goto “set_shutter”
:k_set
shoot
end
“get_bv96” get brightness value
“get_day_second current within one second of the day
Page 44 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
Syntax: x=get_day_seconds
get_day_seconds acts as its own variable, This allows you to even use it within
calculations without first assigning it to another variable.
“get_dof” get the depth of sharpness in mm
“get_far_limit” get the border zone ranged acceptable sharpness mm
“get_focus”
“get_hyp_dist” get hyperfocal distance
“get_iso_market” get “marketing” ISO (See the Allbest’s Firmware Usage page on ISO
values for what is meant by a “Market Value”.)
“get_iso_mode” obtain ISO mode (the former get_iso) E.g. the A620 ISO list:
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 45 of 60
So for DIGIC II and III cameras you can calculate shutter speed from this equation:
tv 96
Time 2 96
seconds, though unfortunately uBASIC doesn’t facilitate this calculation, since
it lacks the ‘^’ (exponentiation) operator (‘twould be easy in assembler or C ).
“get_user_av_id”, the former get_av. Get custom installation av (in the manual modes) for ID
in CHDK:
E.g. the A620 list:
av_ID av * 96 Aperture
9 288 f2.8
10 320 f3.2
11 352 f3.5
12 384 f4.0 (The step between successive
13 416 f4.5 IDs is a shift of 1/3 EV)
14 448 f5.0
15 480 f5.6
16 512 f6.3
17 544 f7.1
18 576 f8.0
“get_user_av96” returns custom av * 96
“get_user_tv_id” returns CHDK identifier for the established user manual modes tv
E.g. the A620 list:
-4 -128 “2.5”
-3 -96 “2”
-2 -64 “1.6”
-1 -32 “1.3”
0 0 “1”
1 32 “0.8”
2 64 “0.6”
3 96 “0.5”
4 128 “0.4”
Important: earlier scripts just use the “get_tv” and “get_av” commands, these must be
changed to this newer “get_user_tv_id” and “get_user_av_id” commands to make them work
properly if using Allbest builds.
This is part of possible values. Meaning load deflection on the id, the same as in the case of
av. (I hope that makes sense to you – means nothing to me! [DF])
“get_user_tv96” returns value installed in the user manual modes. Important: tv * 96
Page 46 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
“get_vbatt”, the voltage of the battery
Syntax: x=get_vbatt
get_vbatt acts as its own variable. This allows you to use it even within expressions,
e.g. if (get_vbatt <= 4300) then print “DEAD BATTERY!”
“get_zoom”
(What does this do?)
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 47 of 60
“set_user_tv96”, the installation of custom Tv * 96
“wheel_right”
“wheel_left”
“get_autostart” parameter checking autostart for scripts
Syntax: x=get_autostart (or used as it’s own variable-string in calculations; see
get_vbatt example)
“set_autostart” Setting this option to autostart scripts
With this command you should be cautious. Specifying Autorun causes the script to
run when you turn the camera on.
“get_usb_power, checking for USB connectivity. Works for series A and S-as a minimum.
Syntax x=get_usb_power
For G-series is not working. Integration with USB button. (?)
“exit_alt”
Page 48 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
2 = ND filter OUT
Added ability to set ND-filter for next set of cameras: a560, a570, g7, ixus700_sd500,
ixus70_sd1000, ixus800_sd700, a710 (deeply tested for Ixus800_sd700). This ability
replaces aperture override menu entry for Ixus and a560 camera set. For all others
from above-mentioned list it is an experimental feature.
get_raw_nr
Returns the condition of your NR (noise reduction setting).
Syntax: x=get_raw_nr
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 49 of 60
and running as well as a setting to allow activating the last loaded script. See the “Special
Build Usage“ section for a little more info.
Usage:
special remote camera button, used in is_key commands with is_key x “remote”
Running this small script (or the loop embedded as a subroutine in more lengthy scripts) is all
you will need:
@title Remote button
:loop
wait_click 1
is_key k “remote”
if k=1 then shoot
goto “loop”
end
Or, if using Fingalo’s builds you may like his version with the simpler while/wend loop
commands:
@title Remote button
while 1
wait_click 1
if is_key “remote” then shoot
wend
end
There are many ways of using this “remote” key function; these are just two of the simpler
(and faster) ways to implement it.
That’s it! That’s all you need! Well, one of those little scripts, the right CHDK build, and the
cable-switch too.
Between MX3’s Motion-Detection options and this amazing USB cable-release method, there
is no limit to the various ways you may control your camera by remote means. Any simple
electronic circuit that can close a switch and feed a 3v to 5v DC signal to the USB port’s
proper contacts (observe proper polarity!) can now be used. There is also no limit to the
length of wire that you may use, as long as you keep the final contact voltage at the camera-
end between the 3vdc and 5vdc range. Use sound-sensitive circuits to record when sound-
events happen. Use light or motion changing events to trigger shooting sessions. Use any
CHDK intervalometer scripts or electronic intervalometer circuits to trigger shots. (There are
thousands of simple circuits like these all over the Internet.) Have your mouse or cat press a
switch to record their vanity-quotient for a science-fair project! The sky (literally) is the limit
to how many ways you may use these functions.
Have fun!
Page 50 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
See the following sections for IDE and debugging aids ideal for both novice uBASIC
developers as well as the more experienced.
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 51 of 60
Save this as “uBASIC_test.bat” in the same folder where your uBASIC_test.exe is. Now you
can drag a script with your mouse onto this batch file and it will be executed. (This would
also work without making a special batch file, but we need the “pause” command to read the
output).
You may need to modify your BAT file to have the @uBASIC_test.exe %1 line to include
the full path to your uBASIC_text.exe file, as well as enclosing the variable %1 in quotes, in
case your script’s filename includes any spaces. For example:
@H:\Tests\CHDK_Files\SCRIPTS\uBASIC_test.exe “%1”
@pause
If you run into problems and this still doesn’t work (using this drag & drop method):
1) Make sure your uBASIC_test.exe file and scripts are not in any path that contains
spaces. (Example: you can’t have it in a sub-folder path of “D:\CHDK Files\Script
Tests\uBASIC_test.exe”. Change those spaces to _ [underscores] in your actual
folder-names if need be.) [DF:] Actually, you can have spaces in your path – just
enclose the entire path in quotation marks, like so:
@”H:\Tests\CHDK Files\SCRIPTS\uBASIC_test.exe”
2) Your BAT file association may have become corrupted. Here’s a handy page of
Windows® XP File Association Fixes Get the one for Batch Files. (Save them all,
they may come in handy one day!)
(How did I find this out? I had all these problems occurring )
An alternative drag-and-drop method (WinXP):
1) Right-click on uBasic.exe and make a shortcut on desktop,
2) Find/search for your script.
3) drag your script to uBASIC icon, let go and it runs!
You may have to adjust the ‘Icon’ properties to keep the result on-screen
The addition of a few extra print and rem statements will help debugging, also include values
to replace the @defaults.
Page 52 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
Script-Writer’s Handy Command-Reference List
I got tired of trying to remember all the commands, so I put together this handy reference list
to keep open in my text-editor alongside any scripts I might be working on. I thought it might
help other scriptwriters too.
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 53 of 60
print_screen 1 The text is written to “A/CHDK/BOOKS/PS00000.TXT”. The new text
overwrites any existing text in the file if there was any.
print_screen N The text is written to the next file number. The file number cycles between 0
and N-1. If the resulting file number is 5, then the text is written to file
“A/CHDK/BOOKS/PS00005.TXT”.
The file number of the last written file is kept in file “A/CHDK/BOOKS/PS_COUNT.TXT”.
Delete the file to reset the counter.
print_screen 0 turns off writing to the file and print_screen 1 turns it back on.
Example:
@title printscreen test
@param a None
@default a 0
@param c mode: 0-append, 1-replace, other-modulo c
@default c 1
print_screen c
print “START “c
print_screen 0
print “Not written to file”
print_screen 1
print “This should be written to the file.”
print “a=“a
print_screen 0
end
The “cls” Command
cls stands for “Clear Screen”. This is used to clear the mini-console screen from any
“print” statements in an easy way.
Other commands:
set_zoom, set_zoom_rel, get_zoom
syntax: set_zoom x (where x is 0 to 8, 14, or 129)
set_zoom_rel x (x is ±relative change)
get_zoom x (zoom value placed in variable x)
range: A-series: x = 0 to 8 or 14 (9 or 15 steps)
S-series: x = 0 to 128 (129 steps)
Zoom command restrictions:
* Camera does not refocus automatically after the end of zooming.
Use a click or press/release “shoot_half” command to implement a refocusing if
needed.
* The “sleep” command is needed after the “set_zoom” command.
Otherwise, camera will shutdown if other command is executed during zooming
process.
set_tv, set_tv_rel get_tv
syntax: set_tv x (where x is the index value)
set_tv_rel x (x is ±relative change)
Page 54 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
get_tv x (index value placed in variable x)
Exposure
Value Index (w/ black-frame) Value Index
15” –12 (~33”) 1/15 12
13” –11 (~27”) 1/20 13
10” –10 (~21”) 1/25 14
8” –9 (~17”) 1/30 15
6” –8 (~13”) 1/40 16
5” –7 (~11”) 1/50 17
4” –6 (~9”) 1/60 18
3”2 –5 (~7”) 1/80 19
2”5 –4 (~6”) 1/100 20
2” –3 (~5”) 1/125 21
1”6 –2 (~4”) 1/160 22
1”3 –1 (~3”) 1/200 23
1” 0 1/250 24
0”8 1 1/320 25
0”6 2 1/400 26
0”5 3 1/500 27
0”4 4 1/640 28
0”3 5 1/800 29
1/4 6 1/1000 30
1/5 7 1/1250 31
1/6 8 1/1600 32
1/8 9 1/2000 33
1/10 10 1/2500 34
1/13 11 1/3200 35 (S-series)
(note: the w/ black-frame times are approximations for true total-time needed for the longer
shutter speeds)
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 55 of 60
set_av, set_av_rel, get_av
syntax: set_av x (where x is the index value)
set_av_rel x (x is ±relative change)
get_av x (index value placed in variable x)
Aperture
Value Index
ISO
f2.7 9
Value index
f3.2 10
AutoISO 0
f3.5 11
50(80) 1
f4.0 12
100 2
f4.5 13
200 3
f5.0 14
400 4
f5.6 15
800 5 (where applicable)
f6.3 16
HiISO –1 (where applicable)
f7.1 17
f8.0 18
set_focus, get_focus
syntax: set_focus x (where x is the distance in mm)
get_focus x (the distance value placed in variable x)
set_iso, get_iso
syntax: set_iso x (where x is index value)
get_iso x (index value placed in variable x)
wait_click, is_key
syntax: wait_click (waits for keypress)
is_key k “<key>“ (sets k = 1 if the last key pressed was <key>)
Page 56 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
LED Commands: (Fingalo’s Builds)
set_led a,b,c (ONLY for S3 (and S2 I guess))
Parameter a is the LED as follows:
a LED Lamp
4 GREEN (by power switch on S3 and A560)
5 YELLOW (by power switch on S3 and under green LED on A560 )
6 (not used)
7 ORANGE (red LED on back of S3 and same place than green on A560)
8 BLUE
9 Focus Assist/Auto-Focus Lamp/AF Lamp (bright green on S3 & bright orange on A560)
10 Timer/Tally Lamp (bright orange lamp in front on S3)
Parameter b:
0 LED is off
1 LED is on
Parameter c (optional):
Brightness 0–200 (seems to work only on the blue LED)
Examples:
set_led 9,1 Turn on AF_beam
set_led 8,1,35 Turn on blue LED with reduced brightness
SDM Builds:
af_led_on
af_led_off
timer_led_on
timer_led_off
blue_led_on
blue_led_off
yellow_led_on
yellow_led_off
green_led_on
green_led_off
amber_led_on
amber_led_off
Special Build Commands:
set_prop and get_prop – Set and get property-case values directly. Use with caution!
Usage: set_prop a b
where a is the property-case location, b is the value
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 57 of 60
Property Cases (see earlier section for more recent tables):
Prop_ID Description
0 Shooting mode dial position
1 Photo effect
5 White balance
6 Drive mode (S3 values: 0=single, 1=continuous, 2=timer)
8 Hi-speed continuous mode (S3: 1=OFF, 0=ON
9 Metering mode (S3 values: 0=eval 1=spot 2=center)
11 Macro (S3 values: 0=normal, 1=macro, 2=super mac)
12 Manual Focus (S3 values: 1=manual, 0=auto)
14 Delay of selftimer (appears to be time in milliseconds)
16 Flash mode (s3: 2=flash closed, otherwise 0=auto, 1=ON)
18 Red eye mode (S3: 0=OFF, 1=ON)
19 Flash slow sync (S3: 0=OFF, 1=ON)
20 Flash Sync Curtain (S3: 0= first, 1 = second)
21 ISO value (S3: 0=auto, 1=ISO-HI, or actual ISO: 80,100,200,400,800)
23 Image quality (S3 values: 0,1,2 from best to worst)
24 Image resolution (S3 values: 0,1,2,4,8 for L,M1,M2,S,W)
25, 26 EV correction (positive or negative, 96 units per stop)
28 Flash correction (same units as 25,26)
32 Exp bracket range (Same units as 25/26: e.g. 96 = ±1 stop range)
34 Focus bracket range 2=Smallest, 1=Medium, 0=largest
36 Bracket mode: 0=NONE, 1 = exposure, 2 = focus
37 Orientation sensor
39 Chosen Av (by user)
40 Chosen Tv (by user)
65 Focus distance
67 Focus ok: 1=Yes, 0=NO
68 Coming Av
69 Coming Tv
74 AE lock: 1=ON, 0=OFF
126 Video FPS (15, 30 or 60. Don’t change here!)
127, 128 Video resolution (S3: 2,1 for 640x480; 1,0 for 320x240)
177 intervalometer: #of shots (0 if not activated)
205 0 ‘1’ during shooting process
206 “MyColors?” mode (See link below)
218 Custom timer continuous: # of shots to be taken
219 Self Timer setting: 0=2 sec, 1=10 sec, 2=custom continuous
Page 58 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc
Special Build USB Remote Routine:
Put 3v to USB cable to trigger Remote Cable Release
@title Remote button
:loop
wait_click 1
is_key k “remote”
if k=1 then shoot
goto “loop”
end
Fingalo Build Alt. Method:
@title Remote button
while 1
wait_click 1
if is_key “remote” then shoot
wend
end
Note: With this remote function the camera will not enter download mode when connecting
the USB cable. For download just disable CHDK by turning of write protect on the SD card.
Special Fingalo Build uBASIC Syntax
a=get_vbatt (inserts battery mV into variable a)
set_raw a (a=0 RAW recording off, a=1 RAW recording on)
Loop commands (do in lowercase, here in UPPER for clarity):
FOR / TO / STEP /.../ NEXT (step may be + or –)
DO /.../ UNTIL (exit when “until” is true)
WHILE /.../ WEND (loop as long as “while” is true)
IF /.../ THEN /.../ ELSE /.../ ENDIF (multiple relation statements)
D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc Page 59 of 60
Notes:
Page 60 of 60 D:\Downloads\Software\CHDK\UBASIC_User_Guide_D0_3.doc