ScratchLists with the Finch

Lesson Key

Lesson key available for teachers.   Get Access

In the previous lessons, you have used the Finch sensors to make measurements, and you have stored these measurements in variables. In this lesson, you will learn to use a new type of variable called a list. A list can hold many values, instead of just one.

Creating a List

To create a list in Scratch, go to the Data menu and click Make a List. For now, name your variable practiceList. The default option For all sprites is fine.

At the beginning of a program, you need to make sure that the list is empty. To do this, use the delete of block on the Data menu. Click on the black triangle to select “all,” and make sure that practiceList is selected.

To add items to a list, you can use the add to block in the Data menu. For example, the block below adds the number 3 to practiceList. Try adding a few other values to practiceList. Notice that each value is added to the bottom of the list. By default, the list is shown in the stage area, although you can deselect the list to hide it.

You can also add items to the beginning of the list using the insert at of block. For example, the block below inserts the number 2 at the top of practiceList.

You can use the repeat block (Control menu) to add a large number of items to a list. The repeat block is a loop that repeats the blocks inside it a certain number of times. For example, the loop below adds five accelerometer measurements to practiceList.

Exercise 1:

Write a program that records 60 measurements with the light sensor. The program should wait 0.5 seconds between measurements. Be sure to save this code, because you will need it later.

Using Items in a List

Each item in the list is assigned a number from 1 to the number of items in the list. You can access an item in the list using the item of block. For example, the block below will display the first item of practiceList.

Often, you will want to use a loop to move through all of the items in a list. You can do this using a counter variable and a repeat block. An example is shown below. At the beginning of the program, practiceList is set to contain three items. Before the repeat block, the variable count is set to 1, corresponding to the first item in the list. In order to repeat the contents of the loop for each item in the list, the number of repetitions in the repeat block is set to the length of the list using the length of block (Data menu). Each time through the loop, the item in the list that corresponds to count is displayed, and then the value of count is increased by 1. Try out this script to make sure you understand how it works.

Exercise 2:

Return to your program from the last exercise. After you collect the accelerometer measurements, use a loop to move through the list. Use each item in the list to determine the position of the sprite on the screen. Remember, you may need to scale the accelerometer value so that you can see the sprite move.

Exercise 3:

Search through your list to find the largest value.

Exercise 4:

Finally, modify your program so that it calculates the sum of the items in your list in a variable called sum. Then calculate the mean acceleration.