# Line Following using Arrays

Now that you are sure your line follower is working well, try experimenting with arrays as another approach to building your BlocklyProp program.  Think of an array as a numbered list of data elements. Your code can refer to a particular data element by its list number.    Now, imagine your list numbering uses binary numbers instead of decimal numbers.

Remember the table of wheel speeds from the previous page?  The binary numbers created by the QTI sensors can be used as array list numbers, and the left and right wheel speed values can be used as array elements.

binary left = right = behavior
1000 16 32 pivot left
1100 32 48 sharp left
1110 48 64 left
0100 56 64 slight left
0110 64 64 forward
0010 64 56 slight right
0111 64 48 right
0011 48 32 sharp right
0001 32 16 pivot right

The binary numbers in the column above represent the output of the four QTI sensors - but binary is also a way to represent an integer. The next table shows each of the binary numbers and their decimal equivalents:

binary decimal left = right = behavior
1000 8 16 32 pivot left
1100 12 32 48 sharp left
1110 14 48 64 left
0100 4 56 64 slight left
0110 6 64 64 forward
0010 2 64 56 slight right
0111 7 64 48 right
0011 3 48 32 sharp right
0001 1 32 16 pivot right

Next, we will sort the table by the decimal representation of the QTI sensors’ output, and leave blanks for the in-between numbers:

binary decimal left = right = behavior
0 0 0
0001 1 32 16 pivot right
0010 2 64 56 slight right
0011 3 48 32 sharp right
0100 4 56 64 slight left
5 0 0
0110 6 64 64 forward
0111 7 64 48 right
1000 8 16 32 pivot left
9 0 0
10 0 0
11 0 0
1100 12 32 48 sharp left
13 0 0
1110 14 48 64 left
15 0 0

Then, let’s turn these into lists, filling in the blanks with zeros:

• Left = 0,32,64,48,56,0,64,64,16,0,0,0,32,0,48,0
• Right = 0,16,56,32,64,0,64,48,32,0,0,0,48,0,64,0

These values can then be used to fill an array for each side, left and right:

Now, instead of the large switch...case block - you can use the array values. You will add an if...do block to handle the cases that have been filled with zeros by using the leftPrev and rightPrev variables:

## Try this

With shorter code, it’s easier to customize the behavior of the robot. For example, you can add an else if that looks for all four of the QTI sensors to read 1 (binary 1111, or decimal 15), and when they do, have the robot stop or turncompletely around:

• Create a shape like this at the end of your line:

• Have your robot stop, beep, and then turn completely around when it encounters the “end of the line” by adding an else if to the if...do block in your code: