PROGRAM WORKSHEET 22: Counting events

Projected outcome: The pupils are introduced to variables, and learn to use them in a concrete meaningful way.

What it does: This programme counts the number of times the touch sensor is pressed and displays the number on the RCX display.

Equipment: RCX, touch-sensor with short lead, and computer running Lego Mindstorms V.2.

Method: Attach touch-sensor to RCX port No. 1 using a short lead.



A programme starts with this icon. Note that the default name for a programme is Untitled. To give it a name of our choice we go to File, Save As, type a name and save it. We can load the programme by going back to File, and typing Open to show the folder in which it was saved.

To do: Save this programme as counter1, and check if its name changed from Untitled.


Part 1: Setting up the program for the touch-sensor

1. Click Sensors on the left panel

2. Click Touch Sensor and drag it to position it a little to the right of  the programme icon you renamed counter1. Click and its menu appears -- see it on the left.

3. Click Next, Next and Done,  to make the programme respond each time the touch sensor attached to Input Port 1 is pressed.


Note that when the menu collapses, the sensor icon docks with the programme start icon by means of a Lego plate.



4. Click Small Blocks on the left panel, click Variable, select Add, and drag it into the place-holder under the touch-sensor. Click to make it dock.

Note that the default variable here is counter1, but by clicking the little tag on the right-hand side of its icon we have an opportunity to rename it.

5. Close the Variable menu on the panel on the left by clicking the word Variable on top of the drop-down list.



Part 2: Setting up the program for the counter

1. On the left panel, click Repeat, select Repeat Forever and drag it to dock under the programme start block.

Note that this block has a place-holder at its centre. This block is used to loop an instruction or list of instructions when placed between the words Repeat and Forever.


2. In the Small Blocks menu on the panel on the left, select Comm, select Display Value, and place it inside the repeat loop, as shown.

3. Click the little tag on the right of this block to get it to display the value of counter1.


4. Use the arrow marked A in the screenshot on the left to open the menu in order to choose what to display, and in this case we select counter1. Click OK.

5. Click File, Save to save the program.

6. Click Download from the menu on top of screen to download the program to the RCX.

To do: Save the variable as times, (go back to step 4 in Part 1) and check if this name appears under counter1 when you select the menu on the left.

7. Click Run on the RCX and note that its window displays the number of times you press the yellow button on the end of the touch-sensor. If it does not diplay any numbers, check that the lead is connected to the top of the touch-sensor where you can see metal parts on the studs on the end near the yellow button.

See a Lego plotter in action