March 21, 2019

Time-Lapse Photography with your Inexpensive Canon Camera (CHDK p. 3) - page 2

Aerial Mapping With Your Canon Point-and-Shoot

  • July 20, 2010
  • By Akkana Peck

Writing UBASIC scripts

If you thought chdk-timer looked a bit like BASIC, you weren't mistaken: it's a language called UBASIC.

You can use any text editor to write UBASIC scripts, but try to avoid using TAB characters for indenting.

Each script starts with a header section:

rem Author - Keoeeit
rem do not run for many hours or days
@title Interval Shooting Non-stop
@param a Interval (Minutes)
@default a 0
@param b Interval (Seconds)
@default b 10
@param c Interval (10th Seconds)
@default c 0

rem, short for remark, begins a comment line.

The @title of the script defines an on-screen title.

The @param and @default lines set up parameters the user can customize through the Scripting parameters menu (Figure 2).

<em>figure 2</em>
figure 2

You're only allowed single character variable names -- a through j on earlier builds, a through z in recent versions. Even if you don't define any parameters, CHDK will display at least three in the menu, named a through c, so you might as well use them.

The script sets up a few variables:


if t<100 then let t=5000

then updates the screen to let the user knows what's happening:
print "Interval shooting."
print "Until you interrupt it."
print "Use with caution."

sleep 1000

sleep sleeps for that many milliseconds.

The heart of the script

Now we get to the good part:

  print "Shot number", n
  sleep t
  goto "shot"

If you're used to more modern languages, this UBASIC loop might look a bit strange: define a label, then loop using goto. But it's typical BASIC structure.

If you want to loop a finite number of times -- suppose you know you'll never take more than 999 shots -- you can use FOR/NEXT:

for n=1 to 999
  sleep t
next n

UBASIC also allows simple subroutines:

for n=1 to 10
  gosub "shootprint"
  sleep t
next n

  print n

Camera commands

Of course, the most important part of this loop is the line that takes the picture: shoot. What else can you do?

You can simulate button presses for any button on the camera:

click "button-name"
or wait for the user to click a button:
wait_click "button-name"
The most common button names are up, down, left, right, set, zoom_in, zoom_out menu, display, print, and erase. You can also use shoot_full and shoot_half to specify full or half presses of the shutter button -- so shoot is just shorthand for click "shoot_full". Some cameras have additional buttons.

In theory, you can control your camera's aperture and shutter speed with commands like set_tv value, get_av variablename or set_av_rel value. But in practice I've found they're all very flaky on my camera -- usually the camera won't shoot after setting them, and trying to read the current settings freezes the camera.

CHDK also offers commands to get and set zoom, zoom_rel, focus, and iso. And when your script is finished, call end so it stops running.

And more

This article barely scratches the surface -- refer to the UBASIC Tutorial page for much more detail on these and other commands. You can also get debugging hints there, and information on special CHDK builds that include other features such as motion detection.

If you find yourself frustrated with the limitations of UBASIC, there's another language available for CHDK: lua.

If you write a useful script, be sure to share it on the User-written scripts page. Don't be shy!

Akkana Peck is a freelance programmer and writer, amateur photographer and the author of the book Beginning GIMP: From Novice to Professional. She's also a control freak who loves fiddling with devices and making them do things they aren't supposed to be able to do.

Most Popular LinuxPlanet Stories