Programming Basics: Selection

The second major construct in programming is selection. In a programming language this is commonly done through an if statement. In Flowol this is done with the diamond or decision flowchart shape.

1. Pelican Crossing

In this example we introduce the idea of selection - the diamond flowchart symbol. This allows us to answer a simple question with two possible outcomes: Yes or No.

Describe the scenario at a Pelican crossing: which lights are on most of the time? How are the lights changed? What happens when they change?

At one point the traffic lights flash a number of times. Flashing is an example of an operation that consists of the same actions repeated a number of times. There are various ways to achieve this in a program such as Flowol. One way os to create a subroutine that produces the flashing and then call this subroutine from the main program:

Main program: set up lights; test for pedestrian; if button pressed then change lights; flash lights (subroutine); set up lights...

Subroutine: turn light off; wait; turn light on; wait; turn light off; wait; tiurn light on... (as many times as you like)

2. Level Crossing

This mimic has three sensors, each of which needs a decision. It also has a motor for the barrier.

Once again you must write your own account of how the scenario works: not so much the mimic but a real-life level crossing. Your account should include every aspect of the mimic: the train lights, the train sensors, the warning lgihts for cars, the barrier sensor and its effects and the motor on the barrier.

Mobile

This mimic has three motors to turn the mobile and the rotors of the helicopter and aeroplane. The mobile will turn both clockwise and anticlockwise.

The mimic also has three inputs that can be read from the number box above the mobile. When the mimic is active the user can left-click or right-click on the number box to move the input value up or down. This mimic introduces the idea of increasing input values until a certain value is reached when a motor is turned on or off.

Outputs 1-4 on the mobile are lights on the car, balloon and aeroplane.

You will find it best to write a separate block of instructions for each button combination. Once you have written one block the rest are near-copies of the first.

Selection or Decision Making

The baby can learn to press buttons on the side of the cot that can be programmed to do different things. When a button is pressed the flowchart needs to detect this and act on it.

The symbol for input is a diamond and its action is a question with two possible answers, Yes or No. A typical way to use the input symbol is to wait until the user presses the button on the cot and only then to make the associated action happen. This creates what is known as a loop - the process of waiting for the button to be pressed is in a loop that continues until the desired action takes place.

When you place a diamond/input symbol on the screen this dialogue box opens:

inputs

The first option is 'Is Input', which is what you choose if the diamond is waiting for an input such as a button press. (We will look at the other options later.)

There are three input buttons which can have the following combinations:

Green
Yellow
Blue
On Off Off
Off On Off
Off Off On
     
     
     
     
     

Complete this table. You can now make 8 different routines with corresponding actions. This means testing all three input buttons in every block of code. This is probably too many so make it 3 or 4 and baby should be happy!

The mobile also includes a light sensor that works on numeric inputs. When the number above the mobile is clicked it increases or decreases. You can make the light come on as it reaches a certain value. You have to decide what values represent dark and light: let's say that 20 is the critical value and that values below this are 'dark' and values above are 'light'. Set the light on the balloon to come on when the number on the mobile is <= 20.

Experiment with the mobile and then write a program that will keep a baby occupied as it gets dark. Change the light levels with the mouse as the program runs to confirm that the outputs change in response to the inputs.