lesson 26 - eu-acerforeducation.acer.com · notification to launch the arduino leonardo app, and...

12
1 Lesson 26: CPF Smart Plant What you will need CloudProfessor (CPF) Moisture Sensor Light sensor RGB LED Arduino Leonardo Android Shield Learning Expectaons (how learning / progress will be demonstrated) All Explain how weather predicons are made and the importance of data collecon. Most Create a plant monitoring system which detects the amount of water/light a plant receives. Some Create a watering device for the plant which acvates when the moisture level is too low. Learning Objecves Understand how weather predicons are made, and the importance of data collecon. Know how to collect data received from sensors. Interpret data retrieved from sensors and present the results in a meaningful way. Overview In this lesson, students will create a plant monitoring system using a moisture sensor, light sensor and RGB LCD Backlight. 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 26 CPF Smart Plant 1 RGB LCD Backlight USB lead

Upload: others

Post on 30-Aug-2019

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson 26 - eu-acerforeducation.acer.com · notification to launch the Arduino Leonardo APP, and select the PF Arduino lockly app. lick on Lesson 9. 4. Press the execute button to

1 Lesson 26: CPF Smart Plant

What you will need

CloudProfessor (CPF)

Moisture Sensor

Light sensor

RGB LED

Arduino Leonardo

Android Shield

Learning Expectations (how learning / progress will be demonstrated)

All Explain how weather predictions are made and the importance of data collection.

Most Create a plant monitoring system which detects the amount of water/light a plant receives.

Some Create a watering device for the plant which activates when the moisture level is too low.

Learning Objectives

Understand how weather predictions are made, and the importance of data collection.

Know how to collect data received from sensors.

Interpret data retrieved from sensors and present the results in a meaningful way.

Overview

In this lesson, students will create a plant monitoring system using a moisture sensor, light sensor and RGB LCD Backlight.

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 26 CPF Smart Plant

1

RGB LCD Backlight

USB lead

Page 2: Lesson 26 - eu-acerforeducation.acer.com · notification to launch the Arduino Leonardo APP, and select the PF Arduino lockly app. lick on Lesson 9. 4. Press the execute button to

2 Lesson 26: CPF Smart Plant

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 moisture sensor to port A2, the light sensor to port A0 and the RGB LCD backlight to port I2C.

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 26 - eu-acerforeducation.acer.com · notification to launch the Arduino Leonardo APP, and select the PF Arduino lockly app. lick on Lesson 9. 4. Press the execute button to

3 Lesson 26: CPF Smart Plant

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. Servo Pin# D4:

The Servo block allows the user to control the attached

servo by specifying the angle (degrees) and delay (milliseconds).

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 26 - eu-acerforeducation.acer.com · notification to launch the Arduino Leonardo APP, and select the PF Arduino lockly app. lick on Lesson 9. 4. Press the execute button to

4 Lesson 26: CPF Smart Plant

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

Let’s write the code for our smart plant.

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 ‘Smart Plant’.

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’ and ‘else’ if block inside the

pop-up window (5).

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

Change the ’=’ value to less than ’<’

d) Click on the ‘Sensor Modules’ tab.

Select ‘Input Devices’ and ‘Environment

Sensors’. Drag the ‘Moisture Sensor’

block onto the first slot in your logic

block (7).

e) Click on the ‘Math’ tab. Drag the ‘0’

block onto the second slot in your logic

block. Change this value to a ’100’ (8).

3

3 1 2

4

5

6

7 8

Page 5: Lesson 26 - eu-acerforeducation.acer.com · notification to launch the Arduino Leonardo APP, and select the PF Arduino lockly app. lick on Lesson 9. 4. Press the execute button to

5 Lesson 26: CPF Smart Plant

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

5. Let’s now display the message ‘I’m thirsty! :(’ on the RGB LCD backlight when the moisture level drops below 100.

Click on ‘Output Devices’. Select ‘Display’. Drag the ‘RGB LCD(IC2) setCursor’ block and place it inside the first

empty ‘do’ slot. Change the Print value to ’I’m thirsty! :( ’ (9).

6. Next, let’s write the condition for when the light level falls below 200 (light < 200).

a). Click on the ‘logic’ tab. Drag the ‘=‘ block and attach it to your ‘if’ block. Change the ’=’ value to less than ’<’ (10).

b) Click on the ‘CPF’ tab and select ‘CPF Devices’. Drag the ‘Light Get Value’ block onto the first slot in your logic

block (11).

c) Click on the ‘Math’ tab. Drag the ‘0’ block onto the second slot in your logic block. Change this value to a

’200’ (12).

7. Let’s now display the message ‘Its ‘too dark! :(’ on the RGB LCD backlight when the light level drops below 200. Click

on ‘Output Devices’. Select ‘Display’. Drag the ‘RGB LCD(IC2) setCursor’ block and place it inside the ‘else if do’ slot.

Change the Print value to ’It’s too dark! :( ’ (13).

3

9

10

11

12

13

Page 6: Lesson 26 - eu-acerforeducation.acer.com · notification to launch the Arduino Leonardo APP, and select the PF Arduino lockly app. lick on Lesson 9. 4. Press the execute button to

6 Lesson 26: CPF Smart Plant

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

8. Next, let’s write the condition which displays a message if both the moisture and light levels are acceptable.

Click on ‘Output Devices’. Select ‘Display’. Drag the ‘RGB LCD(IC2) setCursor’ block and place it inside the ‘else do’

slot. Change the Print value to a smiley face ’ :) ’ (14).

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

Place the tip of the moisture sensor (A) into soil and attach the light sensor (B) to the plant pot (See figure 1).

3

Your finished code should look like this.

A B

Connect a white LED light to your plant monitoring system and program it to automatically turn on when the

there is not enough light in the room.

14

Figure 1.

Page 7: Lesson 26 - eu-acerforeducation.acer.com · notification to launch the Arduino Leonardo APP, and select the PF Arduino lockly app. lick on Lesson 9. 4. Press the execute button to

7 Lesson 26: CPF Smart Plant

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 moisture sensor to port A2, the light sensor to port A0 and the RGB LCD backlight to port I2C.

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 8: Lesson 26 - eu-acerforeducation.acer.com · notification to launch the Arduino Leonardo APP, and select the PF Arduino lockly app. lick on Lesson 9. 4. Press the execute button to

8 Lesson 26: CPF Smart Plant

Code recap (JavaScript)

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

1. var value = cpf.get(“light sensor”);

The cpf.get(“light sensor”); statement returns the value taken from the light sensor and stores it in a variable called

value.

2. cpf.set(“socket d4”, 180, 20);

The cpf.set(“socket d4”, 180, 20); statement controls the attached servo. The syntax for moving the servo is

cpf.set(socket, angle, delay).

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 9: Lesson 26 - eu-acerforeducation.acer.com · notification to launch the Arduino Leonardo APP, and select the PF Arduino lockly app. lick on Lesson 9. 4. Press the execute button to

9 Lesson 26: CPF Smart Plant

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

Let’s write the code for our smart plant.

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: // Smart Plant

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

4. Next, we need to take a reading from the moisture 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 GetMoisture() {

var moisture = cpf.get(“socket a2”);

ui.set(“socket a2”, moisture);

return moisture;

}

5. We also need to take a reading from the light sensor. Click on line 10 and type the following (remembering to leave

a small indentation between lines 11 to 13):

function GetLight() {

var light = cpf.get(“socket a0”);

ui.set(“socket a0”, light);

return light;

}

6. Next, we need to write the condition which checks the moisture level and displays a message on the RGB LCD

Backlight. For this we will use an IF statement. Let’s start with the condition for when the moisture level falls below

100 (moisture < 100).

a) Click on line 16 and type the following:

if (GetMoisture() < 100) {

b) Next, let’s write the code which displays the message ‘I am thirsty! :(’ on the RGB LCD backlight when the

moisture level drops below 100. Click on line 17 and add the following code:

cpf.set(“grove_rgblcd_print”, 0, 0, ‘I am thirsty! :( ’);

ui.set(“grove_rgblcd_print”, 0, 0, ‘I am thirsty! :( ’);

4

1

3

2

Remember to leave a small indentation

before lines 5 to7 (See image above).

Page 10: Lesson 26 - eu-acerforeducation.acer.com · notification to launch the Arduino Leonardo APP, and select the PF Arduino lockly app. lick on Lesson 9. 4. Press the execute button to

10 Lesson 26: CPF Smart Plant

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

6. Next, let’s write the condition for when the light level falls below 200 (light < 200).

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

} else if (GetLight() < 200) {

b) Let’s also display the message ‘It is too dark! :(’ if the light level falls below 200. Click on line 20 and type the

following (Remembering to leave a small indentation before lines 20 and 21):

cpf.set(“grove_rgblcd_print”,0,0,’It is too dark! :( ’);

ui.set(“grove_rgblcd_print”,0,0,’it is too dark! :( ’);

7. Next, let’s write the condition which displays a message if both the moisture and light levels are acceptable. Click on

22 and type in the following (Remembering to leave a small indentation before lines 23 and 24):

} else {

cpf.set(“grove_rgblcd_print”,0,0,’ :) ’);

ui.set(“grove_rgblcd_print”,0,0,’ :) ’);

}

8. Finally, let’s run the code until the user ends the program. Click on line 26 and type the following: cpf.repeat();

9. Run your code.

Instructions

Place the tip of the moisture sensor (A) into soil and attach the light sensor (B) to the plant pot (See figure 1).

4

Connect a white LED light to your plant monitoring system and program it to automatically turn on when the

there is not enough light in the room.

Your finished code should look like this.

A B

Figure 1.

Page 11: Lesson 26 - eu-acerforeducation.acer.com · notification to launch the Arduino Leonardo APP, and select the PF Arduino lockly app. lick on Lesson 9. 4. Press the execute button to

11 Lesson 26: CPF Smart Plant

Extension—Plant watering system.

Step-by-step instructions

1. Securely attach the servo to a lollipop stick using an

elastic band or sticky tape (A).

2. Attach the straw to the arm of the servo using an

elastic band or stick tape (B).

3. Using an elastic band, secure the lollipop stick to the

glass of water (C). You may use sticky tape to secure

the elastic to the glass.

4. Once all the pieces are securely attached, connect the

servo to port D4 and turn on the Cloud Professor.

5. Program your Cloud Professor to activate the servo and

pour water onto the plant when the moisture level drops

below 100 (moisture < 100). Use the code snippets, below,

to help you with this challenge.

Code recap

5

What you will need

lollipop stick / thick card

Bendy drinking straw

Drinking glass

Servo

Elastic bands / sticky tape

A B C

D

Blockly

Servo Pin# D4:

The Servo block allows the user to control the attached servo

by specifying the angle (degrees) and delay (milliseconds).

Javascript

In Javascript, the syntax for moving the servo is

cpf.set(socket, angle, delay).

For example:

Page 12: Lesson 26 - eu-acerforeducation.acer.com · notification to launch the Arduino Leonardo APP, and select the PF Arduino lockly app. lick on Lesson 9. 4. Press the execute button to

12 Lesson 26: CPF Smart Plant

Extension

Students to create an automated plant watering system which activates a servo and pours water onto the plant when the

moisture level drops below 100 (moisture < 100). Students can also add a white LED light which turns on automatically

when the there is not enough light in the room.

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 write up a summary of what they’ve learned; students to include screenshots and snippets of their code in

their summary.

Students to comment their code to explain how it works.

Links

Helping plants grow well (BBC Bitesize): http://www.bbc.co.uk/bitesize/ks2/science/living_things/help_plants_grow/

read/1/

What do plants need to grow? (BBC Bitesize video): https://www.bbc.co.uk/education/clips/z9f87hv

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.

5

6

7

8