This lab helps understand iteration by creating, appending, slicing and indexing lists.

Purpose and Outcomes

In Lab 4 we will create a monitoring system that will:

  • Collect information from an analog environmental sensor (10 samples each separated by 1 second), and store it in a list,

  • Calculate statistics for the entire list as well as a subset of the list

  • Manipulating output devices based on the values stored in the list

By the end of this lab, you should be able to:

  • Create and iterate through lists and strings;

  • Manipulate lists containing numeric data;

  • Slice a list or string

Preparation

  1. Design

    1. Create PSEUDOCODE that describes an algorithm that takes 10 samples (one a second), and saves them to a Python list in the correct order.

    2. Create a FLOWCHART that includes a FOR LOOP that, given a list of numeric data called dataList with a length of 10, calculates the sum of that list. For example, given

      dataList = [1, 1, 2, 2, 3, 3, 4, 3, 0, 1]

      the result will be 20.

  2. Test plan - Describe how you could test each of the above algorithms, given you will be using your Arduino kit in implementation to collect data from a sensor.

Procedure

Warning
Please, do NOT start the Procedure until you have completed the Preparation (Design and Test Plan) outlined above.

1. Components

In this lab, we will use one input and one output.

  • Input: Either the sound sensor or the light sensor.

  • Output: Either the OLED screen or LED. In addition to the console.

We will use our design from Preparation to do the implementation. You can start by downloading lab4-w2024.py and opening it in Thonny.

2. Implementation

Some useful information:

  • To help with delaying the timing of taking samples, we can call the sleep(seconds) functions from the time module.

  • We have an existing list called dataList.

  • In Procedure 2.1, we will implement the algorithm that stores samples in dataList (as designed in pseudocode in Preparation).

  • In Procedure 2.2, we will implement the algorithm that calculates some statistics of the elements in dataList (as designed in flowchart in Preparation).

  • In Procedure 2.3, we will manipulate output devices based on the statistics and stored list values.

  • In Procedure 2.4, we will implement a while loop instead of a for loop.

2.1 Storing numeric data in a list

  • Background:

    In Python, lists are a type of data collection that are non-homogeneous (data does not have to be the same type) and ordered (we can use indices to access a particular element of a list).

    Example: If I want to create a list called Digits to hold some values. I could do this with the statement Digits = [3, 1, 4, 1, 5, 9]

    If I want to access the value of any element, I can do so using: Digits[i] where i is the value of the index. To get the length of the list, I can use, len(Digits), which evaluates to 6. We know that Digits[0] evaluates to 3, Digits[1] evaluates to 1, and so on.

    If I want to iterate through the list, I can do that with a definite (ie. for) loop.

    We can also iterate through the list via its range of indices. Q1: How? Record the answer in your logbook (just descriptions).

  • Design Implementation:

    With this knowledge about lists and iteration, implement the algorithm from Preparation Design. Here are some hints:

    • We want to iterate through the list dataList. Which way makes more sense - via elements or indices?

    • Each time the loop executes, what do we want to happen over and over again?

      • Read from our chosen input sensor

      • Replace the value of the appropriate element in dataList with the reading value

      • Wait one second

      Print your list to see what has been saved in dataList and if it makes sense, given your data source. Before moving on, test to see if this part of the script is working correctly.

    Q2: Record your observations and if you encountered any errors and how you solved them.

2.2 Calculating statistics for our numeric list

Given that we now have a list of numeric information that represents samples of data, let us analyze it.

  • Implement the algorithm from the Preparation Design, in which we use a FOR LOOP to calculate the sum of values in the list.

  • Print the result to the console and compare it to the result of the statement sum(dataList). If they do not match, investigate why not.

  • Then, use the calculated sum to find the average data value in the list. Confirm that this part of the script is working correctly.

Now, suppose I knew that the first and last data samples collected in each script run were corrupt. How could I use slicing and/or the range function to calculate the sum and average of the uncorrupted samples?

Implement this and test it. Q3: In your logbook, make notes about how you did this and if you encountered errors.

Similarly, if I knew that the first five samples were corrupt, how could I use slicing to calculate the sum and average of the uncorrupted samples?

Implement this and test it. Q4: In your logbook, make notes about how you did this and if you encountered errors.

2.3 Manipulate output device

Now that we have a list of data and the average value of the samples calculated, we want to

  • Iterate through our list of samples (again)

  • Compare the element value to our average

  • Indicate via an output device how that sample compares to the average (either write to the OLED screen or turn on/off the LED)

  • Wait one second

Given the problem and algorithm outlined above, implement Python statements that do this.

Q5: Include the coding part for this step and all the previous steps in the submitted Python file (please label the steps).

2.4 While loop implementation

Use a WHILE loop in place of a FOR loop in your implementation of Lab 4 (choose any of them).

Q6: In your logbook, record the changes you made and the output you get. Include the coding part for this step as a comment in your script (after testing it)

Testing

Come up with a test plan for the Procedure and document the results. Is the output behaving the way you expect it to? If not, have you confirmed that you are collecting the data correctly and correctly calculating the statistics?

In your logbook, create your test plan table and complete it with results (Include all scenarios).