Lists and Graphing with the Hummingbird (Scratch)

In the introductory lessons, you learned to use variables. Remember that a variable can store a value. A variable can be used to store the value of a Hummingbird sensor, as shown below. 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. Remember that the repeat block is a loop that repeats the blocks inside it a certain number of times. For example, the loop below adds five distance sensor measurements to practiceList.

Exercise: Write a program that records 100 measurements with the distance sensor. The program should wait 0.1 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: Return to your program from the last exercise. After you collect the distance sensor measurements, use a loop to move through the list. Use each item in the list to determine the y-position of the sprite on the screen. Use math blocks to scale the distance value so that a distance measurement of 0 cm means the sprite is at the bottom of the screen and a distance measurement of 100 cm means the sprite is at the top of the screen.

Exercise: Next, modify your program so that the second part of the program graphs the distance measurements to the screen. You can do this by using the variable count  as the x-coordinate of the sprite. Use the blocks in the Pen menu to draw the path of the sprite. How could you spread your graph out over the entire x-axis? Note: You can hide the sprite if you want to see only the graph.

Exercise: 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 distance.