Top Banner
1 Lesson 24: T-rex Detector What you will need CloudProfessor (CPF) Piezo Vibraon Sensor RGB LED Buzzer Arduino Leonardo Android Shield Learning Expectaons (how learning / progress will be demonstrated) All Explain the reasons for monitoring vibraon. Most Create a system which monitors vibraons and warns of impending danger. Some Use logical reasoning to detect errors in their algorithms. Learning Objecves Understand the need for monitoring vibraon. Combine and process data from mulple sensors to build a real-me system. Program, debug and refine the code for their app. Overview In this lesson, students will create a warning system using a vibraon sensor, RGB LED and buzzer. Curriculum Links (Compung PoS) Designs simple algorithms using loops, and selecon i.e. if statements. (AL) Uses logical reasoning to predict outcomes. (AL) Detects and corrects errors i.e. debugging, in algorithms. (AL) Creates programs that implement algorithms to achieve given goals. (AL) Understands that programming bridges the gap between algorithmic soluons and computers. (AB) Computaonal Thinking Concepts: AB = Abstracon; DE = Decomposion; AL = Algorithmic Thinking; EV = Evaluaon; GE = Generalisaon. Lesson 24 T-Rex Detector 1 USB lead
10

Lesson 24 - Acer for Education Magazine · Arduino Leonardo Android Shield Learning Expectations (how learning / progress will be demonstrated) ... 3. When the loudProfessor detects

Mar 28, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lesson 24 - Acer for Education Magazine · Arduino Leonardo Android Shield Learning Expectations (how learning / progress will be demonstrated) ... 3. When the loudProfessor detects

1 Lesson 24: T-rex Detector

What you will need

CloudProfessor (CPF)

Piezo Vibration Sensor

RGB LED

Buzzer

Arduino Leonardo

Android Shield

Learning Expectations (how learning / progress will be demonstrated)

All Explain the reasons for monitoring vibration.

Most Create a system which monitors vibrations and warns of impending danger.

Some Use logical reasoning to detect errors in their algorithms.

Learning Objectives

Understand the need for monitoring vibration.

Combine and process data from multiple sensors to build a real-time system.

Program, debug and refine the code for their app.

Overview

In this lesson, students will create a warning system using a vibration sensor, RGB LED and buzzer.

Curriculum Links (Computing PoS)

Designs simple algorithms using loops, and selection i.e. if statements. (AL)

Uses logical reasoning to predict outcomes. (AL) Detects and corrects errors i.e. debugging, in algorithms. (AL)

Creates programs that implement algorithms to achieve given goals. (AL)

Understands that programming bridges the gap between algorithmic solutions and computers. (AB)

Computational Thinking Concepts: AB = Abstraction; DE = Decomposition; AL = Algorithmic Thinking; EV = Evaluation;

GE = Generalisation.

Lesson 24 T-Rex Detector

1

USB lead

Page 2: Lesson 24 - Acer for Education Magazine · Arduino Leonardo Android Shield Learning Expectations (how learning / progress will be demonstrated) ... 3. When the loudProfessor detects

2 Lesson 24: T-rex Detector

Step-by-step instructions

1. Connect the power of CloudProfessor then press and hold the power button for two seconds; it will turn on and the

power indicator will light up.

2. Insert the Arduino Shield into Arduino Leonardo and use the USB cable to connect the CloudProfessor with Arduino

Leonardo. Attach the piezo vibration sensor to port D6, the RGB LED to port D7 and the buzzer to port D5.

3. When the CloudProfessor detects the Arduino Leonardo, a notification will appear on your device; click the

notification to launch the Arduino Leonardo APP, and select the CPF Arduino Blockly app. Click on Lesson 9.

4. Press the execute button to enter the control user interface (UI). The UI can be used to adjust the speed of the fan.

5. Press the edit button to enter the program editing page.

2

CPF Arduino Blockly app

Edit button

Execute button

Control user interface (UI) Program editing page

1

2

3

Page 3: Lesson 24 - Acer for Education Magazine · Arduino Leonardo Android Shield Learning Expectations (how learning / progress will be demonstrated) ... 3. When the loudProfessor detects

3 Lesson 24: T-rex Detector

Let’s explore some of the code used in the previous lessons.

1. Light Get Value:

The Light Get Value block returns the value of the light sensor (Returns a numeric value

based on the brightness level of the environment).

2. White LED Set

The White LED Set block controls the attached White LED light. The led can be set to

either ‘ON’ or ‘OFF’.

3. Tone/speaker Pin# D5 (frequency/duration):

The Tone/speaker Pin# D5 (frequency/duration) block plays

a tone via the attached buzzer based on the specified frequency

and duration.

4. If / else:

The if / else block is used to conditionally run code

depending on whether a Boolean condition is true or false.

In this example, if the light sensor value is less than (<) 500,

the RGB LED light will light up; or else (else), if the light sen-

sor value is greater than or equal to 500, the RGB LED light

will automatically turn off.

5. Title Name Panel Set:

The Title Name Panel Set block displays a message on the screen. In this

example it will display a “Downloading…” message on the UI (User Interface).

6. Delay Time(Sec.):

The Delay Time(Sec.) block is used to pause the code (time set in milliseconds). In the example above, the

program is paused for 1000 milliseconds (1 second).

7. Set CPF control request:

Finally, the Set CPF control request block at the end of the program tells the program to run the code from the

beginning again. The program will continuously repeat until the user closes the app or stops the program manually.

Code recap (Blockly) 3

Page 4: Lesson 24 - Acer for Education Magazine · Arduino Leonardo Android Shield Learning Expectations (how learning / progress will be demonstrated) ... 3. When the loudProfessor detects

4 Lesson 24: T-rex Detector

Step-by-step instructions (Blockly) - Part 1

Let’s write the code for our T-rex detector.

1. Clear the existing code so that you have a blank canvas to create your new program. Click on the bin icon (1). Click

on ‘OK’ (2) to create a blank canvas.

2. Note: You can return to the original program at any time by clicking on

the restore icon (3).

3. First, let’s set the ‘Title name’. Click on CPF UI. Select the ‘Title Name Panel

Set’ block and drag it onto your code canvas. Set the title to ‘T-Rex Detector’.

4. Next, we need to write the condition which checks for vibration and turns the RGB LED red when vibration is

detected. For this we will use an IF statement.

a) Click on the ’Logic’ tab. Drag the ‘if do’ block and attach it to the end of

your code (4).

b) Click the cog inside your if block. Drag an ‘else’ block inside the pop-up

window (5).

c). Click on the ‘logic’ tab. Drag the ‘=‘ block and attach it to your ‘if’ block (6).

d). Click on the ‘Sensor Modules’ tab. Select ‘Input Devices’ and ‘Switches’. Drag the ‘Piezo Vibration Sensor’ block

onto the first slot in your logic block (7).

e). Click on the ‘Sensor Modules’ tab.

Select ‘IN/OUT’ and ‘Digital’. Drag the

‘HIGH’ block onto the second slot in your

logic block (8).

3

Add a light sensor and modify the code so that the white LED light can only be activated when it is dark.

3 1 2

4

5

6

7

8

Page 5: Lesson 24 - Acer for Education Magazine · Arduino Leonardo Android Shield Learning Expectations (how learning / progress will be demonstrated) ... 3. When the loudProfessor detects

5 Lesson 24: T-rex Detector

Step-by-step instructions (Blockly) - Part 2

5. Next, let’s write the code to turn the RGB LED red if vibration is detected.

Click on the ‘CPF Devices’ tab. Select the ‘RGB

LED Setting colour with’ block and drag it into

the first empty ‘do’ slot. Change the colour to

red (9).

Next, let’s write the code to turn off the RGB LED when NO vibration is detected.

Click on the ‘CPF Devices’ tab. Select the ‘RGB LED Setting colour with’ block

and drag it into the ‘else’ slot. Change the colour to black (10).

6. Finally, we want the program to run continuously until the user ends the program.

Select the ‘CPF Devices’ tab. Click and drag the ‘Set CPF control repeat block and attach

it to the end of your code.

Instructions

To activate the vibration sensor, bend the tip of the vibration sensor (A) with your finger and release (See figure 1).

3

Add a buzzer to your T-rex detector. Program your buzzer to play an audible alarm when vibration is

detected.

Your finished code should look like this.

Vibration sensor. A

Figure 2.

Figure 1.

9

10

Page 6: Lesson 24 - Acer for Education Magazine · Arduino Leonardo Android Shield Learning Expectations (how learning / progress will be demonstrated) ... 3. When the loudProfessor detects

6 Lesson 24: T-rex Detector

1. Connect the power of CloudProfessor then press and hold the power button for two seconds; it will turn on and the

power indicator will light up.

2. Insert the Arduino Shield into Arduino Leonardo and use the USB cable to connect the CloudProfessor with Arduino

Leonardo. Attach the piezo vibration sensor to port D6, the RGB LED to port D7 and the buzzer to port D5.

3. When the CloudProfessor detects the Arduino Leonardo, a notification will appear on your device; click the

notification to launch the Arduino Leonardo APP, and select the CPF Arduino app. Click on Socket Mode.

4. Press the execute button to enter the control user interface (UI).

5. Press the edit button to enter the program editing page.

Setup instructions (JavaScript) 2

1

2

3

CPF Arduino app

Edit button

Execute button

Control user interface (UI) Program editing page

Page 7: Lesson 24 - Acer for Education Magazine · Arduino Leonardo Android Shield Learning Expectations (how learning / progress will be demonstrated) ... 3. When the loudProfessor detects

7 Lesson 24: T-rex Detector

Code recap (JavaScript)

Let’s explore some of the code used in the previous lessons.

1. d6 = cpf.get(“socket d6”);

The cpf.get(“socket d6”); statement returns the value of the button. (Returns a numeric value of ‘1’ if the button is

pressed and ‘0’ if the button is not pressed) and stores it in a variable called d6.

2. cpf.set(“socket d5”, 262, 250);

The cpf.set(“socket d5”, 262, 250); statement plays a tone via the attached buzzer based on the specified

frequency and duration.

3. If / else:

The if / else statement is used to conditionally run code

depending on whether a Boolean condition is true or false.

In this example from lesson 1, if the light sensor value is less than (<) 500, the

RGB LED light will light up (based on the values taken from the UI); or else

(else), if the light sensor value is greater than (>) 500, the RGB LED light will

automatically turn off: cpf.set(“rgb led”, 0, 0, 0);

4. Math.random()

Through Math.random(), a random number between 0 and 0.999999 is generated. If we want to generate a whole

number in a range such as 1—255 we first need to multiply the result of the random function by 255 and then

round the result to produce a whole number e.g.,

5. cpf.sleep(1000);

The cpf.sleep(1000); statement is used to pause the code (time set in milliseconds). In the example above, the

program is paused for 1000 milliseconds (1 second).

6. Set CPF control request:

Finally, the cpf.repeat(); statement, usually found at the end of the program, tells the program to run the code from

the beginning again. The program will continuously repeat until the user closes the app or stops the program

manually.

3

Page 8: Lesson 24 - Acer for Education Magazine · Arduino Leonardo Android Shield Learning Expectations (how learning / progress will be demonstrated) ... 3. When the loudProfessor detects

8 Lesson 24: T-rex Detector

Step-by-step instructions (JavaScript) - Part 1

Let’s write the code for our T-rex detector.

1. Clear the existing code so that you have a blank canvas to create your new program. Press and hold on a selection

of code and click on ’Select all’ (1). Click on ‘Cut’ (2) to create a blank canvas (3).

Note: You can return to the original

program at any time by clicking on

the restore icon in the Save/load

menu.

2. First, let’s give our new script a name. Click on line 1 and type in the following: //T-Rex detector

3. Next, let’s set the title for our app. Click on line 2 and type the following: ui.set(“title”, ‘T-Rex Detector’);

4. Next, we need to take a reading from the vibration sensor and display the reading on the user interface (UI). To do

this, we are going to use a function. Click on line 4 and type the following:

function GetVibration() {

var Vibration = cpf.get(“socket d6”);

ui.set(“socket d6”, Vibration);

return Vibration;

}

5. Next, we need to write the condition which checks for vibration and turns the RGB LED red when vibration is

detected. For this we will use an IF statement. Let’s start with the condition for when vibration is detected

(vibration = 1).

a) Click on line 10 and type in the following:

if (GetVibration() == 1) {

b) Next, let’s turn the RGB LED red if vibration is detected. Click on line 11 and type the following:

ui.set(“rgb led”, “r”, 255);

ui.set(“rgb led”, “g”, 0);

ui.set(“rgb led”, “b”, 0);

cpf.set(“rgb led”, 255,0,0);

Don’t forget to leave a small indentation before lines 12 to15 (See image below).

4

1

3

2

Remember to leave a small indentation

before lines 5 to7 (See image above).

Page 9: Lesson 24 - Acer for Education Magazine · Arduino Leonardo Android Shield Learning Expectations (how learning / progress will be demonstrated) ... 3. When the loudProfessor detects

9 Lesson 24: T-rex Detector

Step-by-step instructions (JavaScript) - Part 2

6. Next, let’s write the condition for when vibration is NOT detected. Click on line 16 and type the following:

} else {

ui.set(“rgb led”, “r”, 0);

ui.set(“rgb led”, “g”, 0);

ui.set(“rgb led”, “b”, 0);

cpf.set(“rgb led”, 0,0,0);

7. Let’s close our if statement. Click on line 21 and add the following line of code:

}

8. Finally, let’s run the code until the user ends the program. Click on line 22 and type the following:

cpf.repeat();

9. Run your code.

Instructions

To activate the vibration sensor, bend the tip of the vibration sensor (A) with your finger and release (See figure 1).

4

Add a buzzer to your T-rex detector. Program your buzzer to play an audible alarm when vibration is

detected.

Your finished code should look like this.

Vibration sensor. A

Figure 2.

Figure 1.

Page 10: Lesson 24 - Acer for Education Magazine · Arduino Leonardo Android Shield Learning Expectations (how learning / progress will be demonstrated) ... 3. When the loudProfessor detects

10 Lesson 24: T-rex Detector

Extension

Students to modify their code so that it plays an alarm and flashes the RGB LED 4 times when motion is detected. Hint:

Students can use a ‘For’ loop to make their RGB LED flash on and off. Students could also create an RGB LED disco lamp

which changes colour in response to the beat of the music.

Differentiation

To support students, provide step by step guides.

To stretch students ask them to create a flowchart / pseudocode of their code first or code their solution using JavaScript.

Homework

Students to investigate why we need vibration sensors. Students to find and list as many practical uses as possible for a

vibration sensor.

Links

Managing tectonic hazards—Predicting & preparing for volcanoes/earthquakes (BBC Bitesize): http://www.bbc.co.uk/

schools/gcsebitesize/geography/natural_hazards/managing_hazards_rev1.shtml

Condition monitoring (Wikipedia): https://en.wikipedia.org/wiki/Condition_monitoring

Disclaimer: Use these sites at your own risk. Acer is not responsible for the content of external Internet sites. We

recommend that you check the suitability of any recommended websites links before giving them to students.

4

5

6

7