This lab helps understand iteration by creating, appending, slicing and indexing lists.
Purpose and Outcomes
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
-
Design
-
Create PSEUDOCODE that describes an algorithm that takes 10 samples of a sensor (one a second), and saves them to a Python list in the correct order.
-
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, givendataList = [1, 1, 2, 2, 3, 3, 4, 3, 0, 1]
the result will be 20.
-
-
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
1. Description and Components
In this lab, we will the following:
-
Input: light and sound sensors, and temperature.
-
Output: OLED screen and LED.
We will create a smart meeting room assistant system that will:
-
Collect information from different environmental sensors (light, sound, and temperature) by taking 10 samples, each separated by 1 second, and store them in lists.
-
Calculate statistics for the entire list as well as a subset of the list
-
Monitor environmental conditions in a meeting room and provides suggestions to enhance comfort and productivity as follows
-
Lighting Conditions
-
Too Bright → Display on OLED: Dim the lights
-
Too Dark → Display on OLED: Increase brightness
-
-
Noise Levels
-
Too Loud → Display on OLED: Manage noise
-
-
Temperature Control
-
Too Hot → Display on OLED: Lower the thermostat
-
Too Cold → Display on OLED: Increase the thermostat
-
-
If light, sound, and temperature are all in the optimal range → Display on OLED: Perfect Meeting Environment!
-
If any condition is outside the ideal range
-
Turn on the LED
-
Display the corresponding message according to the above.
-
-
You can start by downloading lab4-w25.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 existing lists called light, sound, and temp.
-
In Procedure 2.1, we will implement the algorithm that stores samples in the defined lists (as designed in pseudocode in Preparation).
-
In Procedure 2.2, we will implement the algorithm that calculates some statistics of the elements in lists (as designed in flowchart in Preparation).
-
In Procedure 2.3, we will manipulate output devices based on the statistics and stored list values.
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]
wherei
is the value of the index. To get the length of the list, I can use,len(Digits)
, which evaluates to 6. We know thatDigits[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 defined lists. 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 the corresponding with the reading value
-
Wait one second
Print your list to see what has been saved in the list 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. You can implement the algorithm to get the sum of light list.
-
Print the result to the console and compare it to the result of the statement sum(light). 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.
-
Get the sum and average of sound and temp lists as well. You can use the sum() function directly.
Now, suppose I knew that the first and last data samples collected in light 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.
2.3 Putting Together the Smart Meeting Room Assistant System!
Now that we have lists of data and their averages, we want to implement the following:
-
Average Lighting Conditions
-
Too Bright → Display on OLED: Dim the lights
-
Too Dark → Display on OLED: Increase brightness
-
-
Noise Levels (Average)
-
Too Loud → Display on OLED: Manage noise
-
-
Temperature Control (Average)
-
Too Hot → Display on OLED: Lower the thermostat
-
Too Cold → Display on OLED: Increase the thermostat
-
-
If light, sound, and temperature are all in the optimal range → Display on OLED: Perfect Meeting Environment!
-
If any condition is outside the ideal range
-
Turn on the LED
-
Display the corresponding message according to the above.
-
Given the problem and algorithm outlined above, implement Python statements that do this.
Q4: 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 (OPTIONAL)
To make it easier to test for different conditions, the entire process can be included in a loop that runs continuously until the button is pressed. This means that as long as the button remains unpressed, the system will repeatedly:
-
Collect 10 sensor readings (light, sound, and temperature) at 1-second intervals.
-
Calculate statistics such as average values for each sensor.
-
Analyze the data and determine if the room conditions are too bright, too dark, too loud, too hot, or too cold.
-
Manipulate the output devices accordingly by displaying recommendations on the screen, activating the LED.
-
Repeat the entire process, continuously monitoring the environment.
-
Once the button is pressed, the loop will terminate, stopping data collection and system responses.
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).