← Back to Library
Lesson PlanFreeEN

CyberPi: Remix Control

In this lesson, students will be introduced to the use of functions in Python. Using functions can reduce duplication of code, improve the clarity of code and decompose complex problems into simpler pieces while creating complex algorithms. In this lesson, students will explore the use of functions by creating functions for musical sounds. Students will use the computer keyboard to combine sound effects and play sounds.

Gr. 11–12
CyberPi: Remix Control

Lesson

Overview

Description

In this lesson, students will be introduced to the use of functions in Python. Using functions can reduce duplication of code, improve the clarity of code and decompose complex problems into simpler pieces while creating complex algorithms. In this lesson, students will explore the use of functions by creating functions for musical sounds. Students will use the computer keyboard to combine sound effects and play sounds.

Objectives

  • Why and how to define functions.

  • How to control and monitor the input device by using the ‘pynput’ module.

Before the Lesson

Preparation

For the teacher:

  • Students should have a laptop or device with mBlock 5 installed from here: https://www.mblock.cc/en/download/ (desktop version or cloud-based version) and mBlock Python editor (mLink) installed. Click here to download the Python editor (mLink)

  • One CyberPi with USB-C cable 

  • Pocket shield (optional)

  • Worksheet: Lesson Remix Control

  • Example Program: Lesson Remix Control

  • Refer to the text-based beginner lessons in this bundle for more ways to teach text-based programming without the use of the CyberPi.

Introduction:

  • This lesson will introduce the use of functions in Python.

  • A function is a set of well-defined, organised, and reusable code that can perform a specific task when it is called.

  • Using functions can reduce duplication of code, improve the clarity of code, and decompose complex problems into simpler pieces while creating complex algorithms.

  • In this lesson, students will explore the use of functions by creating functions for musical sounds. Students will use the computer keyboard to combine sound effects and play sounds.

Review

Warm Up

1. Introduce the remix culture.

  • Explain the concept of remix culture: Remix culture refers to a cultural practice of artists that create and produce creative products by combining or editing existing materials. Sometimes, remix culture is also called read-write culture, which indicates that the cultural artefacts may not be considered as the original work of someone and hence are indicated as ‘cultural collective work’.

  • Say: Digital technologies are suited for adaptation and remixing and facilitate the remix culture creation. In music, for example, we can use music applications to edit and modify a piece and combine different parts of existing songs to create a new piece of music.

2. Have students fill out the ‘What I Know’ column of the K-W-L chart before the class.

Direct Instruction

Tour of mBlock 5

1. Open the mBlock software or mBlock 5 Web version.

2. Introduce students to the following key areas of the software interface:

Connect the CyberPi

3. Plug the CyberPi into the computer using the included cable. The CyberPi should boot up and the screen will display either the last program uploaded or the Home menu.

4. On the "Devices" tab in mBlock, click the "Add" button. Select CyberPi and click "Ok".

5. Click the "Connect" button. Then, select the USB port and click "Connect".

6. If connected successfully, the button will change to "Disconnect".

7. When you open the editor for the first time you will see these explanations mapping the interface. Encourage students to thoroughly read through it. Below are the screen captures if you ever need to reference them again.

Figure 1: To connect your CyperPi

Figure 2: Creating Projects and Project files (see How to Create, Open, Import, or Export Project Files)

Figure 3: Editing Area

Figure 4: Console Commands to run code and more

Figure 5: Terminal (Console) to display output from Editing Area

Figure 6: Libraries containing example program. Feel free to explore these at your leisure

Figure 7: Click "Tutorials" to view these helpful instructions anytime

Guided Practice

Hands On

Activity 1:

1. Have students read "Activity 1" in the example program

2. Ask students to think about the questions below:

  • Identify the new module that can monitor the input from your keyboard.

  • What is meant by the Python keyword ‘def’?

  • Why does it separate the set of expressions within each ‘def’ code block?

  • How to produce interactive sound and light effects?

3. Ask students to explain the program and draw a flowchart.

4. Introduce the ‘pynput’ module.

  • Instruct students to identify the line of code below and figure out what is different about this statement.

from pynput.keyboard import Key, Listener

  • Say: In the example program, the ‘pynput’ module is added into the Python code editor. The ‘pynput’ module is a third-party library that controls and monitors input devices. 

  • Explain the ‘pynput’ module: In the example, we call relevant ‘pynput’ functions to monitor keyboard input by obtaining the current status of the keyboard. The program can monitor which key is pressed or released. ‘pynput’ can also monitor mouse input and even control the keyboard and mouse. For example, some functions can make the computer type a word.

  • Explain the syntax: In the example, the ‘from’ indicates the source of the library that we want to import. The ‘import’ indicates the set of functions we need in the library – the ‘Key, Listener’ means that we want functions that can monitor the keyboard input.

  • Instruct students to utilise ‘pynput’ to control the mouse. Demonstrate the following example program:

  • Then ask students to add new functions that make the mouse cursor move up and down by tilting CyberPi forward and backward.

Tips: The syntax for reference:

cyberpi.is_tiltforward; cyberpi.is_tiltback

5. Ask students to add a new function to program the right mouse button to be remotely controlled by CyberPi.

6. Summarize: The keyword ‘Key’ refers to the keyboard. The keyword ‘Button’ refers to the mouse. The keyword ’Listener’ indicates the action of monitoring an input device while the keyword ‘Controller’ indicates the action of controlling an input device.

7. Explain the use of functions:

  • Instruct students to identify the lines of code below in the example program:

Line 5: def bar1():

Line 10: def bar2():

Line 15: def bar3():

Line 20: def bar4():

Line 25: def on_press(key):

Line 39: def on_release(key):

  • Say: The keyword ‘def’ indicates that the following lines of code are a function.

  • Explain: A function is a group of related statements that performs a specific task. A function contains a set of well-defined, organized, and reusable code. To create a function, we need to use the keyword ‘def’ as the function header.

  • To define the function, we need to use the indentation to indicate that a group of code belongs to the function. In the example program, for instance, we create the functions ‘bar1’‘bar2’‘bar3’, and ‘bar4’ to store and represent the bars.

  • Remind students that the name of a function, like the name of a variable, should be readable and explanatory.

8. Instruct students to define a function. Have them pay attention to the following points:

  • Start with the keyword ‘def’ and do not forget the colon;

  • Indent the statement, otherwise an error will occur.

9. Explain the statement below: 

  • The word ‘key’ in the round brackets is a parameter that indicates the input device.

def on_press(key)

def on_release(key)

10. Explain how to play sounds.

  • Instruct students to identify the lines of code below:

key.char == “1”

  • Explain: This expression is to check whether Key ‘1’ is pressed or not; if Key ‘1’ is pressed (i.e., the statement is ‘True’) execute the ‘bar1’ function.

  • We can modify the parameters to use other characters such as ‘a’, ‘b’, and ‘c’.

Independent Practice

Try It

Activity 2:

  • Instruct students to work individually on modifying the example program by completing the task below:

Task 1: Modify the parameters in the example program. Use letters to replace the numeric parameters.

key.char == “a”

key.char == “b”

key.char == “c”

key.char == “d”

Note: Remind students that they should use lowercase letters.

Task 2: Modify the ‘bar’ functions. Find some pieces of songs from music textbooks and combine different parts of them together to make a new song. Consider how to combine the sound effects and LED lights.

BAR 1 Function:

BAR 2 Function:

Reflection

  • Have students fill out the K-W-L chart

  • Remind students to always comment their code, regardless of the simplicity

Extension

  • Good coding conventions ask to keep programs as modular ("full of functions") as possible

  • Go back to previous lessons and create functions to complete specific tasks

Wrap Up

Wrap-Up & Quick Check

Summary

1. Summarize why and how to define functions.

2. Summarize how to control and monitor the input device by using the ‘pynput’ module.

Downloadable Material

Example Program

Worksheet

Educational Standards

Ontario - Grade 11-12 - Computer Science

Grade 11 University Computer Science (ICS3U)

A. Programming Concepts and Skills

A1. Data types and Expressions

  • A1.1 use constants and variables, including integers, floating points, strings, and Boolean values, correctly in computer programs;

  • A1.3 use assignment statements correctly with both arithmetic and string expressions in computer programs;

  • A1.4 demonstrate the ability to use Boolean operators (e.g., AND, OR, NOT), comparison operators (i.e., equal to, not equal to, greater than, less than, greater than or equal to, less than or equal to), arithmetic operators (e.g., addition, subtraction, multiplication, division, exponentiation, parentheses), and order of operations correctly in computer programs;

A2. Control Structures and Simple Algorithms

  • A2.1 write programs that incorporate user input, processing, and screen output;

  • A2.2 use sequence, selection, and repetition control structures to create programming solutions;

A3. Subprograms

  • A3.1 demonstrate the ability to use existing sub- programs (e.g., random number generator, substring, absolute value) within computer programs;

  • A3.2 write subprograms (e.g., functions, procedures) that use parameter passing and appropriate variable scope (e.g., local, global), to perform tasks within programs.

A4. Code Maintenance

  • A4.1 demonstrate the ability to identify and correct syntax, logic, and run-time errors in computer programs;

  • A4.2 use workplace and professional conventions (e.g., naming, indenting, commenting) correctly to write programs and internal documentation;

  • A4.3 demonstrate the ability to interpret error messages displayed by programming tools (e.g., compiler, debugging tool), at different times during the software development process (e.g., writing, compilation, testing);

  • A4.4 use a tracing technique to understand program flow and to identify and correct logic and run-time errors in computer programs;

  • A4.5 demonstrates the ability to validate a program using a full range of test cases.

B. Software Development

B1. Problem-solving Strategies

  • B1.2 demonstrate the ability to solve problems independently and as part of a team;

B2. Designing Software Solutions

  • B2.1 design programs from a program template or skeleton (e.g., teacher-supplied skeleton, Help facility code snippet);

  • B2.2 use appropriate vocabulary and mode of expression (i.e., written, oral, diagrammatic) to describe alternative program designs, and to explain the structure of a program;

  • B2.4 represent the structure and components of a program using industry-standard program- ming tools (e.g., structure chart, flow chart, UML [Unified Modeling Language], data flow diagram, pseudocode);

  • B2.5 design user-friendly software interfaces (e.g., prompts, messages, screens, forms).

Grade 11 University Computer Science (ICS3C)

A. Programming Concepts and Skills

A1. Data Types and Expressions

  • A1.1 use constants and variables, including integers, floating points, strings, and Boolean values, correctly in computer programs;

  • A1.3 use assignment statements correctly with both arithmetic and string expressions in computer programs (e.g., numStudents = 4 + 2, name = “Devi”);

  • A1.4 use Boolean operators (e.g., AND, OR, NOT), comparison operators (i.e., equal to, not equal to, greater than, less than, greater than or equal to, less than or equal to), arithmetic operators (e.g., addition, subtraction, multiplication, divi- sion, exponentiation, parentheses), and order of operations correctly.

A2. Control Structures and Simple Algorithms

  • A2.1 write programs that incorporate user input, processing, and screen output;

  • A2.2 use sequence, selection, and repetition con- trol structures to create programming solutions;

  • A2.3 Demonstrate the ability to write algorithms with nested structures.

A3. Code Maintenance

  • A3.2 demonstrate the ability to correct syntax, logic, and run-time errors in computer programs;

  • A3.3 use workplace and professional conventions (e.g., naming, indenting, commenting) correctly to write programs and internal documentation;

  • A3.4 demonstrate the ability to interpret error messages displayed by programming tools (e.g., compiler, debugging tool), at different times during the software development process (e.g., writing, compilation, testing);

  • A3.5 demonstrate the ability to validate a program using test cases.

B. Software Development

B1. Problem-solving Strategies

  • B1.1 use various problem-solving strategies (e.g., divide and conquer, working backwards, process analysis, examples, extreme cases, tables and charts, trial and error) to solve program- ming problems;

B2. Designing Software Solutions

  • B2.1 design a simple program from a program template or skeleton (e.g., teacher-supplied skeleton, Help facility code snippet);

  • B2.2 use appropriate vocabulary and mode of expression (i.e., written, oral, diagrammatic) to describe alternative program designs and to explain the structure of a program;

  • B2.3 write subprograms (e.g., functions, procedures) that perform one well-defined task and use parameter passing and appropriate variable scope (e.g., local, global);

  • B2.4 use industry-standard programming tools (e.g., structure chart, flow chart, UML [Unified Modelling Language], data flow diagram, pseudo- code) to represent the structure and components of a computer program;

  • B2.5 design user-friendly software interfaces (e.g., prompts, messages, screens, forms).

Grade 12 Computer Science - Coming Soon!