3
NAME PERIOD DATE PASCO 1 BLOCKLY EXTENSION: ACOUSTIC STOPWATCH How can you use Blockly coding to create a stopwatch that responds to sound? Objectives Create code that responds to a sound signal. Create code with outputs that can be used to time something. Modify working code to tailor it to improve its execution. Materials and Equipment Data collection system Wireless Sound Sensor Safety Follow regular laboratory safety precautions. Procedure 1. Start your data collection system and turn on the wireless sound sensor. 2. Connect the wireless sound sensor to your data collection system and set the sample rate to at least 200 Hz. 3. Open the Blockly coding window on your software. Create code that will output the time at which the Sound Wave value measured by the sound sensor exceeds a specified trigger value. 4. Create a display that shows a graph of the Sound wave value vs time and a digit display that shows the time output by the code. 5. Start recording, then clap your hands near the sound sensor. The time that you clap should be displayed in milliseconds in the digit display and correspond to the sound wave measured on the graph display. If not, check your code. If needed, use Figure 1 at the end of the lab to help debug. 6. Figure 1 shows some features of the code that help it work with the sound sensor. The repeat while true loop causes the program to run until it is stopped. The If statement checks the sound wave value and, if it is greater than 30, outputs the time to the Code Output variable Start Time. The absolute value helps the code recognize the trigger because the sound wave value oscillates above and below zero as the pressure from the sound wave changes. The sleep statement causes the program to run slower making it is easier to communicate with the sound sensor. If your code wasn't working well, modify it to add these features. 7. Add to the code the capability to output to another digit display the time when a second sound exceeds the trigger value. Create a second digit display and test it out the code by clapping until a start time and a stop time are output to the digit displays. If it doesn't work, check your code. If needed, use Figure 2 at the end of the lab to help debug. 8. Figure 2 shows the code that outputs a stop time too. However, it has some problems. Sometimes the stop time is earlier than the start time. One clap can cause a start and a stop timer to be output. If your code worked without these problems, then go to step 11. Otherwise modify your code so that the first clap outputs a start time and a second clap outputs a stop time. Step 9 has some hints for accomplishing this.

BLOCKLY EXTENSION: ACOUSTIC STOPWATCH · 2020. 7. 10. · BLOCKLY EXTENSION: ACOUSTIC STOPWATCH . 2. PASCO . 9. One method to solve the problem of the stop time coming before the

  • Upload
    others

  • View
    9

  • Download
    2

Embed Size (px)

Citation preview

Page 1: BLOCKLY EXTENSION: ACOUSTIC STOPWATCH · 2020. 7. 10. · BLOCKLY EXTENSION: ACOUSTIC STOPWATCH . 2. PASCO . 9. One method to solve the problem of the stop time coming before the

NAME PERIOD DATE

PASCO 1

BLOCKLY EXTENSION: ACOUSTIC STOPWATCH How can you use Blockly coding to create a stopwatch that responds to sound?

Objectives • Create code that responds to a sound signal.

• Create code with outputs that can be used to time something.

• Modify working code to tailor it to improve its execution.

Materials and Equipment • Data collection system • Wireless Sound Sensor

Safety • Follow regular laboratory safety precautions.

Procedure

1. Start your data collection system and turn on the wireless sound sensor.

2. Connect the wireless sound sensor to your data collection system and set the sample rate to at least 200 Hz.

3. Open the Blockly coding window on your software. Create code that will output the time at which the Sound Wave value measured by the sound sensor exceeds a specified trigger value.

4. Create a display that shows a graph of the Sound wave value vs time and a digit display that shows the time output by the code.

5. Start recording, then clap your hands near the sound sensor. The time that you clap should be displayed in milliseconds in the digit display and correspond to the sound wave measured on the graph display. If not, check your code. If needed, use Figure 1 at the end of the lab to help debug.

6. Figure 1 shows some features of the code that help it work with the sound sensor. The repeat while true loop causes the program to run until it is stopped. The If statement checks the sound wave value and, if it is greater than 30, outputs the time to the Code Output variable Start Time. The absolute value helps the code recognize the trigger because the sound wave value oscillates above and below zero as the pressure from the sound wave changes. The sleep statement causes the program to run slower making it is easier to communicate with the sound sensor. If your code wasn't working well, modify it to add these features.

7. Add to the code the capability to output to another digit display the time when a second sound exceeds the trigger value. Create a second digit display and test it out the code by clapping until a start time and a stop time are output to the digit displays. If it doesn't work, check your code. If needed, use Figure 2 at the end of the lab to help debug.

8. Figure 2 shows the code that outputs a stop time too. However, it has some problems. Sometimes the stop time is earlier than the start time. One clap can cause a start and a stop timer to be output. If your code worked without these problems, then go to step 11. Otherwise modify your code so that the first clap outputs a start time and a second clap outputs a stop time. Step 9 has some hints for accomplishing this.

Page 2: BLOCKLY EXTENSION: ACOUSTIC STOPWATCH · 2020. 7. 10. · BLOCKLY EXTENSION: ACOUSTIC STOPWATCH . 2. PASCO . 9. One method to solve the problem of the stop time coming before the

BLOCKLY EXTENSION: ACOUSTIC STOPWATCH

2 PASCO

9. One method to solve the problem of the stop time coming before the start time is to create another condition to output the time. The If statement for the start time can change the value of a variable to let the other If statement know if a start time was output already. The problem of one clap triggering both start and stop times can be solved by increasing the sleep time in the start time If statement. Modify your code to incorporate these ideas. Test you code until the first clap outputs a start time and a second clap outputs a stop time. If needed, use Figure 3 at the end of the lab to help debug.

10. Figure 3 adds a couple of features to the code to solve the problems encountered previously and to make it work better. A variable called trigger number was created and initialized at zero. The start time If statement adds an "and" statement that requires the trigger level to be zero to output a start time. It then changes the trigger value to 1. The stop time If statement adds an "and" statement that requires the trigger level to be 1 to output a stop time. It then changes the trigger value to 2. The repeat while loop was changed to exit the program after the trigger value is changed to 2. This stops the execution after the stop time is output and prevents it from being changed by an additional sound. The sleep times were increased to 1000 ms to solve the problem of 1 clap outputting both start and stop times. A variable called Trigger Level was added to make it easier to change the sound wave value that triggers the times to be output.

11. Now that you have a code that works (at least it should most of the time), incorporate some improvements. Change the output time to seconds instead of milliseconds, add an output that gives the change in time, and prevent the code from stopping if the change in time is greater than a specified amount.

Questions and Analysis

1. What is the purpose of the repeat while loop in the code?

2. Why were the sleep statements necessary?

3. What was the purpose of the trigger number?

4. Other sensors that could be used as a stopwatch include light sensors, force sensors, acceleration sensors, and altimeters. Describe one potential use of a stopwatch program using one of these sensors to do something useful or interesting.

Page 3: BLOCKLY EXTENSION: ACOUSTIC STOPWATCH · 2020. 7. 10. · BLOCKLY EXTENSION: ACOUSTIC STOPWATCH . 2. PASCO . 9. One method to solve the problem of the stop time coming before the

BLOCKLY EXTENSION: ACOUSTIC STOPWATCH

PASCO 3

Figure 1

Figure 2

Figure 3