This lab will allow you to practice conditional flow control using the Arduino.

Purpose and Outcomes

We will use conditional flow control to manipulate our chosen output device based on the value of the inputs.

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

  • Read and write to digital devices connected to the Arduino;

  • Know how to write conditionals that make decisions using provided boundaries; and

  • Understand the syntax and semantics of conditional flow control in Python.

Preparation

The main input sensors are the light and sound sensors. The main output devices are the LED and the buzzer. Lab 2 Preparation needs to be done before the lab session and will be submitted as part of the Lab 2 Logbook Entry. No coding is required to finish the preparation; it is related to the design and test plan.

Our goal in this lab is to implement two different designs that relate our input to the output using conditional flow control (if statements). As part of your design:

  • Write the decision sentenses to the following designs (Write them in a pseudo code format. A general example is available here):

    • Design 1: Here, we are planning to program an automated light activation system. The light sensor is used as an input. The system will check the current light level and will turn the LED on if the light level of the room is low. Otherwise, it will ensure that the LED is off.

      • The light sensor is an analog sensor; the output values are in the range of 0 to 750 approximately (keep in mind the actual range of values you got in lab 1). Based on these values, think of a value that represents the threshold or the "low" light level to use in your if statement design.

    • Design 2: Here, we are planning to create a simple security system with intrusion detection. The sound sensor is used as an input. The system will check the sound level. If it is high (this will be regarded as an intrusion), the system will ask the user to enter the admin password. If it is entered successfully, the buzzer will not sound. Otherwise, the buzzer will sound to trigger the alarm.

      • The sound sensor is an analog sensor; the output values are in the range of 0 to 500 approximately. Based on these values, think of a value that represents the "high" sound level to use in your if statement design to detect intrusion.

  • Testing: Describe how you plan to test your implementation of each of the above designs, i.e. what the state of the input device is and what the corresponding output observation should be. You will have TWO test plans (i.e., each with at least two test cases) - one for your light input and one for the sound input.

Procedure

In lab 2, we will use the Python script to write our code. We will implement a program that interacts with the Arduino sensors using conditional flow control, i.e. if statements. Recall that our project contains:

  • One digital input - the button

  • Two analog inputs - the light sensor and the sound sensor

  • One digital output - the LED

Before moving to the implementation stage, download the Python script file lab2.py and save it in your desired directory. Then, open the downloaded file within Thonny.

2.1 Examine the code in the starting script:

The downloaded script contains an implementation of branching that decides what to print to the console based on user input. Run the given script "as-is" and note the script output based on different input values.

2.2 Read analog inputs (examine the range of values)

In Lab 1, we learned that we can read any analog sensor readings using the function call (where pin represents the pin number that the sensor is connected to):

analog_read(pin)

We are planning to get the readings of the light and sound sensors.

In the downloaded script, implement the following:

  • Implement Python statements that read your analog inputs (light and sound)

  • Store the output in variables and print the evaluations to the console.

  • Test the sensor range by trying to get both the minimum and maximum possible readings (you will need to rerun the script to observe new values).

Q1: In your logbook entry, note what you did to get different readings from the sensor and record the output values.

2.3 Implement lab 2 preparation design # 1

In this design, we are planning to program an automated light activation system. If the current light level in the room is low, the Arduino will turn on the LED. Otherwise, it will turn off the LED.

  • Here, the decision is based on the light sensor (if it is above or below a threshold value). In the script, what will be the Python condition or proposition? What Python variable do you have that stores the value of our digital input? What value should we compare them to?

    Q2: Record your chosen threshold value and the reason you chose it.

  • Based on the output, we will either turn the LED on or off. You can use the following function to turn the LED on:

    digital_write(4, True)

    Also, you can turn it off using:

    digital_write(4, False)

2.3.1 Adding an option to turn on the LED based on the button

We would like to build on design 1 and include one more feature, which is turning on the LED based on the button condition. If the button is pressed, the LED should be turned on (i.e., treating the button as a light switch). Add this feature to your script.

  • Here, the decision is based on the digital input (the button, whether it is pressed or not). In the script, what will be the Python condition or proposition? What Python variable do you have that stores the value of our digital input? What value should we compare them to? We will need to get the input value from our digital sensor - the button - by calling the function that will provide that information. To read the button, we can use the following function call that reads the digital sensors (the pin number that the button is connected to is 6):

    digital_read(pin)
  • Since we are reading from a digital port, the readings will have two possible outputs (True or False). True indicates that the button is pressed, and False indicates that it is not. Test that to ensure that your sensor is working as expected.

Q3: Record your complete code for Design 1.

Q4: Record if you encountered any errors, what they are, and how you solved them.

2.4 Implement lab 2 preparation design # 2

In this design, we are planning to build a simple security system with intrusion detection. The sound sensor is used as an input to indicate the intrusion. The system will check the sound level. If it is high, the system will ask the user to enter the admin password. If it is entered successfully, the buzzer will not sound. Otherwise, the buzzer will sound to trigger the alarm.

  • Here, the decision is based on the sound sensor (if it is above a threshold value). In the script, what will be the Python condition or proposition? What Python variable do you have that stores the value of our digital input? What value should we compare them to?

  • If the sound level is high, we will need to get the admin credentials from the user and implement an if statement to check if the admin credentials match what you have in the system. If it doesn’t, trigger the alarm (i.e., the buzzer) using the following function:

    buzzer_frequency(5,400)

    If the sound level is low, ensure that the buzzer is off, using the following:

    buzzer_stop(5)

    Once implemented, confirm that your script runs.

Q5: Record your chosen threshold value and the reason you chose it.

Q6: Record your complete code for Design 2 in your logbook.

Q7: Record if you encountered any errors, what they are, and how you solved them.

2.5 Additional Challenge (OPTIONAL)

If you implemented all the steps successfully and you still have time, try the following!

Instead of asking the user input for the design testing choice, let’s take the input from the Arduino using the rotary dial. The program can start by reading the rotary dial value and storing it in a variable. Then, the if-elif statements will change to be dependent on the rotary dial instead of the user input. You can define the range as you wish, but simply, it can be divided halfway since we only have two designs to test. You can discard/delete the else statement.

Testing

In Lab 2 Preparation, you created a test plan. If this test plan no longer makes sense, revise it before testing. In your logbook, record if you changed your testing plan.

Record your test results in tabular format for designs 1 and 2. Note any syntax errors and how you fixed them. Also, note logical errors and what assumptions may have led to them.

You will need to run the script a few times with different input values to test all scenarios. In your logbook entry, note the input you provided and the output you observed, and explain if it makes sense (i.e., Record what happens for each test case when your script runs).