7.5 Program Control

The following example demonstrates a common use of program controls. Connect a button or switch to sensor 1 and a DC motor to the output port A. Then, try the following script.

to sensor_control
    forever [
        if sensor1 > 500 [ a, on wait 10 off]
    ]
end

IF

The IF program-control allows parts of the program to run only when the given condition is met. An IF is always followed by a condition. The example above shows an IF followed by a condition which reads “if sensor 1 is greater than 500”. Then, the commands to be executed are located in the “do” section. In this case, the program will turn on motor A for one second.

Conditions

Defining a suitable condition for an IF is important. When using sensors, the first step is usually to observe how the sensor values change. Then a condition can be defined. In this example, the sensor value for an unpressed switch is usually 0. This value jumps to 1023 when the switch is pressed. Therefore, the condition “if sensor 1 is greater than 500” really means “if switch pressed”. The number 500 was selected as a rough mid-point between the highest and lowest possible values. Changing this threshold to other numbers such as 100 or 1000 will still work for this example.

Condition values are more sensitive when using analog sensors, which values change in proportion to their measurements. Consider a light sensor that is used to control a lamp so that the lamp switches on when the house is dark. Let’s say the sensor value is zero when there is no light during the night and the value increases and reaches 800 during the day. What will be a good threshold for the program? 400? The answer is that it depends on how dark it should be before the lamp turns on. A value too low will make the lamp too slow. A value too high will cause the lamp to switch on prematurely. In practice, the programmer will have to obtain a suitable threshold value by observing the sensor value at the time when the lamp owner feels it is dark enough to switch it on. In this case, the condition determines the lamp’s “light sensitivity”.

Conditions can be combined to form a more complex definition. Logical operators are often used. The example below shows a condition that requires two buttons to be pressed in order to turn on a motor.

if sensor1 > 500 and sensor2 > 500 [ a, on wait 10 off]

IF-ELSE

The IF-ELSE program-control is an extension of an IF. In addition to defining what actions to perform when the condition is true, IF-ELSE can also define what to do when the condition is false. The following example modifies the original example so that it turns on the motor when the switch is pressed and off when the switch is released.

ifelse sensor1 > 500 [a, on] [ a, off]

FOREVER

The FOREVER program-control tells the program to loop indefinitely, executing the blocks it contains. Most programs will need this loop. The sensor_control example is shown again below with and without FOREVER (right and left images respectively). Without a FOREVER, the program will check the condition once and quit, which is not the desired behavior. This is a common mistake of beginners.

Without FOREVER With FOREVER

WAITUNTIL

The WAITUNTIL program-control allows the program to halt until the given condition is true. The example program below shows how WAITUNIL is used so that the program beeps once every time a switch is pressed

forever [ if sensor1 > 500 [ beep waituntil [sensor1 < 500]]]

Note that without the WAITUNTIL, the program will beep repeatedly while the switch remains pressed.

results matching ""

    No results matching ""