Scripting

Advanced

Arrays

So we can make a loop, which makes our code a lot smaller. But in the previous lesson we were really restricted in the values used in that loop.. just mathematical permutations of i. Arrays are how we get more control of our loops.

Before you start

You need to know the syntax of a javascript array.

Start your script

Nothing special here, same as always.

/**
 * Advanced Scripting 2 - Arrays
 *
 * Demonstrates how arrays can be used to seperate the content of days from the
 *   logic of their timing, making it possible to make day templates.
 *
 * made: 2017-03-23
 * by:   William A. Norman
 */

// Takes a single image with red lights.
var protocol = new CameraProtocol("example_protocol")
    protocol.setAnalysisID       ("example")
    protocol.setNumberLoops      ( 1 )
    protocol.setFramesPerLoop    ([ 1 ])
    protocol.setMeasuringLight   ([ 1 ])
    protocol.setSaturationFlash  ([ 0 ])
    protocol.setExposure         ([ "50us" ])
    protocol.setFrameInterval    ([ "70ms" ])

// Set starting conditions
set_intensity(0)

// Take a sample imageset
run_protocol(protocol)

Define setting arrays

Next we're going to create two arrays, one representing daytime light intensity and one representing night time light intensity.

You'll want to put these arrays after the starting comment but before you define protocols. It makes it easier to read the script. So put this code there:

var daylight = 
        [50,  100, 150, 100,
         100, 150, 100, 50]  

var nightlight =
        [20,  0,   0,   20,
         20,  20,  0,   0]

Add in a loop

Now lets take a loop like the one from the previous lesson and modify it, so we can make use of the array we just created.

for (int i = 0; i < daylight.length(); i++) {
    wait("10s")
    set_intensity(daylight[i])
    wait("10s")
    run_protocol(protocol)
    wait("10s")
    set_intensity(nightlight[i])
}

Notice the syntax daylight[i]. We already know this runs once where i=0 through i=9. We're pulling the first value from the array, then the second, etc.

Complete script

So now we have a template. We can alter the day without actually changing any code outside of that array!

/**
 * Advanced Scripting 2 - Arrays
 *
 * Demonstrates how arrays can be used to seperate the content of days from the
 *   logic of their timing, making it possible to make day templates.
 *
 * made: 2017-03-23
 * by:   William A. Norman
 */

var daylight = 
        [50,  100, 150, 100,
         100, 150, 100, 50]  

var nightlight =
        [20,  0,   0,   20,
         20,  20,  0,   0]

// Takes a single image with red lights.
var protocol = new CameraProtocol("example_protocol")
    protocol.setAnalysisID       ("example")
    protocol.setNumberLoops      ( 1 )
    protocol.setFramesPerLoop    ([ 1 ])
    protocol.setMeasuringLight   ([ 1 ])
    protocol.setSaturationFlash  ([ 0 ])
    protocol.setExposure         ([ "50us" ])
    protocol.setFrameInterval    ([ "70ms" ])

// Set starting conditions
set_intensity(0)

// Take a sample imageset
run_protocol(protocol)

for (int i = 0; i < daylight.length(); i++) {
    wait("10s")
    set_intensity(daylight[i])
    wait("10s")
    run_protocol(protocol)
    wait("10s")
    set_intensity(nightlight[i])
}