as long as the system is powered read the input analog device and display it to the console if the temperaure reading is above a threshold value (indicate it) manipulate the output device to show the alarm state (How?) else make sure alarm is off
This lab will allow you to practice using conditional flow control and introduce looping.
Purpose and Outcomes
You are creating an environmental monitoring system. When run, it will check an important environmental factor and trigger an alarm based on the sensor readings (if it is above a predefined threshold). We will also print to the user some useful information about the weather.
By the end of this lab, you should be able to:
-
Describe the aspects and importance of looping vs conditional flow control in programming; and
-
Implement looping within a Python scrpit for a given problem.
Preparation
In Lab 3, we will program a smart environmental monitoring system using some of the Arduino kit sensors, listed here:
-
Barometric Sensor: We will be monitoring room temperature, pressure, and altitude using the barometric pressure sensor and analyzing the data
-
Rotary Dial: To enable the user to set a temperature threshold of the environment
-
Button: To confirm the user’s threshold selection
-
LED and buzzer: To represent an alarm if the readings exceed a specific threshold
-
OLED display: To display useful information about the weather to the user
Our main goal in this lab is to continuously monitor the environment, trigger the alarm if the conditions represent an alarm state, and display some useful information about the weather to the user. The barometric sensor gives the temperature, pressure, and altitude readings. As part of your preparation:
-
Draw a flowchart that represents a design of the following problem (indicating your design conditions):
-
Draw a flowchart that represents the design of the flow control statements for printing weather information to the user according to the following description (indicating your design conditions):
-
Print "Sunny" if these conditions are met:
-
Temperature: Relatively warm temperatures.
-
Pressure: Rising barometric pressure.
-
Altitude: No significant changes in altitude.
-
-
Print "Rainy" if these conditions are met:
-
Temperature: Moderate to warm temperatures.
-
Pressure: Falling barometric pressure.
-
Altitude: Slightly fluctuating.
-
-
Print "No enough data" otherwise.
-
Procedure
We aim to activate an alarm state using our outputs based on our inputs using conditional flow control and loops. We will build on our Preparation design to implement the Python code.
2.1 Setting the temperature threshold using the rotary dial
Before starting to monitor the environment, we will set up the system. The goal is to set the threshold using the rotary dial. When the code runs, display the current position to the user using the OLED screen; as you rotate the dial, you should see the updated values. Use the button to confirm the selection. You can print on the OLED screen using the following function:
oled_print(value)
To clear the screen, use the following:
oled_clear()
In other words, we are continuously displaying the current dial position to the user, until they confirm the selection using the button. This threshold is saved is in a variable, such that we use this value later in the lab to compare the current temperature readings to it.
Inside the loop, use a delay of one or two seconds. We can add delay in the execution of a program for a specific number of seconds by calling the sleep() function from the time module, as follows:
from time import sleep
sleep(num)
Q1:Indicate if you encountered any errors in your logbook.
2.2 Infinite loop implementation - Reading, printing and waiting
Now, we want to observe and read our sensors' values. How can we get it to monitor the readings continuously? By introducing an infinite loop! We are monitoring the environment using the barometric pressure sensor. We can get three readings: temperature, pressure, and altitude
-
Read
-
Temperature:
pressure_get_temp()
-
Altitude:
pressure_get_altitude()
-
Pressure:
pressure_get_pressure()
-
-
Print
-
Print sensor reading to console in a format that provides context. i.e. Don’t just print the reading. Provide more text such as "Current light level: x" or "Current temperature: x degrees C"
-
-
Wait
-
Implement a delay of one second
-
Q2: In your logbook, record any errors you encounter, and note how did you fix them.
Q3: Come up with a different condition (for the while loop) that will also allow the script to monitor the readings continuously.
2.3 Threshold check for the temperature sensor
Next, we want to include within the body of our loop a conditional statement that compares our sensor reading to the stored threshold value (the reference value, which, based on it, you can judge if the reading encourages an alarm activation or not).
Based on the threshold check, you can activate the alarm using the following:
-
If using the buzzer, we can sound the alarm by using the function call:
buzzer_frequency(pin, frequency)
-
If using the LED, we can turn it on by using the function call:
digital_write(pin, True)
Otherwise, we want to ensure our alarm is off.
-
If using the buzzer, we can ensure it is off with the function call:
buzzer_stop(pin)
-
If using the LED, we can turn it off by using the function call:
digital_write(pin, False)
Once this part has been implemented, run your script to ensure that there are no syntax errors. Then, do some testing/manipulation of your input device, specifically trying to make the reading higher than the threshold, and observe the corresponding changes on the output device.
2.4 Button-dependant alarm loop
Now, our system is looping infinitely, checking and printing readings every second and indicating when the level exceeds our chosen threshold. We want to add one more aspect to the algorithm. If the threshold is exceeded, we want to trigger an alarm, which will only stop when the button is hit (i.e., the alarm will continue to trigger, and no new values will be taken while the button is not pressed). Once the button is pressed, we want to ensure our alarm is off and continue reading values.
Once this part has been implemented, test your button-dependent alarm loop by exceeding the threshold. Try it two times, one where the values go back to normal (after pressing the button), and the other where the values are still exceeding the threshold.
Q4: record your observations in the above two test cases.
2.5 Analyze the data to display on the OLED screen information about the weather
In this step, we will use the temperature, pressure, and altitude to display some information about the weather. Implement your design from the preparation part to indicate the weather.
-
Sunny Weather:
-
Temperature: Relatively warm temperatures.
-
Pressure: Rising barometric pressure.
-
Altitude: No significant changes in altitude.
-
-
Rainy Weather:
-
Temperature: Moderate to warm temperatures.
-
Pressure: Falling barometric pressure.
-
Altitude: Slightly fluctuating.
-
-
Print No enough data otherwise.
The terms rising and falling mean that you will compare with a previous reading of the same sensor. You can simply call the function again and save it in another variable so that you can compare the two of them together. Ensure that there is enough time between these two samples (two seconds or more).
Q5: Did you end up changing the flowchart from the preparation part? What were the changes?
Please keep in mind that these are simplified categories, and real-world weather forecasting is much more complex, involving multiple variables such as humidity, wind patterns, and historical weather data. These basic categories serve as general guidelines and are a starting point for weather interpretation based on temperature, pressure, and altitude.
Q6: The complete code for all the steps should be submitted in a separate python file. Please label the steps in your code.
Testing
Record your testing in a tabular format for step 2.1 (with the testing details, indicate what is the chosen threshold value).
Use this table in your logbook to note your observed results for steps 2.2, 2.3, and 2.4. If observed results don’t match expectations, investigate why or why not.
Scenario | Action | Expected Output | Observed Output | Investigations |
---|---|---|---|---|
Environment setup |
Ensure environmental conditions are low |
Sensor readings should be printed to the console at one-second intervals; no alarm should trigger |
||
Trigger alarm |
Modify environmental conditions so that readings increase (eg. warm-up temperature sensor) |
Once the alarm triggers, sensor readings should no longer be printed. |
||
Trigger alarm |
Once the alarm has been triggered, reduce the environmental reading |
The alarm should continue even when conditions change |
||
Dismiss alarm |
While the alarm is tuned on, press the button |
Once the button is pressed, the alarm should stop. |
Record your testing in a tabular format for step 2.5.
Make sure to submit the required files for the lab through Gradescope. You will need to submit the following:
-
Lab Logbook (.pdf or .png): Includes procedure and testing sections
-
Python Script (.py file): This should include the coding part for the lab