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). If personnel are not present (indicated by whether or not the button is pressed), the alarm will continue to sound even if the tempreture or light readings return to normal. 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 Bonanza: 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:

  1. Draw a flowchart that represents a design of the following problem (indicating your design conditions):

      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 alarm output device to show "high alarm criteria" met
        else
          make sure alarm is off
  2. 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):

    1. Print "Sunny" if these conditions are met:

      1. Temperature: Relatively warm temperatures.

      2. Pressure: Rising barometric pressure.

      3. Altitude: No significant changes in altitude.

    2. Print "Rainy" if these conditions are met:

      1. Temperature: Moderate to warm temperatures.

      2. Pressure: Falling barometric pressure.

      3. Altitude: Slightly fluctuating.

    3. 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.

Note
Logbook submission: Make sure to submit two files; one .py file that includes the coding part of the lab, and another document (pdf file) that includes the rest of the details (preparation, answers to procedure questions, and testing)

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. To 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 with it.

Inside the loop, use a delay of one second. We can wait or cause a delay in the 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

Once all these steps are completed, run your script to ensure that there are no syntax errors.

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 in output printed to the console (using the print function to indicate when the threshold has been exceeded).

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.

  1. Sunny Weather:

    1. Temperature: Relatively warm temperatures.

    2. Pressure: Rising barometric pressure.

    3. Altitude: No significant changes in altitude.

    Weather Conditions: Clear skies, minimal cloud cover, and little chance of precipitation. This is typical of high-pressure systems associated with fair weather.

  2. Rainy Weather:

    1. Temperature: Moderate to warm temperatures.

    2. Pressure: Falling barometric pressure.

    3. Altitude: Slightly fluctuating.

    Weather Conditions: Falling pressure and moderate to high humidity levels can indicate the approach of a low-pressure system, potentially leading to rain, thunderstorms, or stormy weather.

  3. 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.