Lesson 5: Board Events

Overview

This lesson transitions students from consider the Circuit Playground as strictly an output device towards using it as a tool for both input and output. Starting with the hardware buttons and switch,sing the hardware buttons and switch, students learn to use onBoardEvent(), analogously to onEvent(), in order to take input from their Circuit Playgrounds.

Purpose

This lesson marks the transition from using the board solely as an output device to using it for both input and output. The onBoardEvent() block works much like onEvent(), with the most significant different being that the first parameter is a board object (a variable) while onEvent() takes a UI element ID (a string).

Agenda

Warm Up (5 min)

Activity (45 min)

Wrap Up (2 min)

View on Code Studio

Objectives

Students will be able to:

  • Attach an event handler to a hardware input
  • Choose the appropriate event for a given scenario

Introduced Code

Teaching Guide

Warm Up (5 min)

Board Inspection: Inputs

Distribute: Pass out a board and USB cable to each pair of students. Let students know that they should not yet plug the boards in.

Prompt: Ask pairs to spend one minute looking over the board, focusing on potential input devices. Based on what you already know about this board, how do you think you might use it to get input?

Share: Have groups share back their thoughts to the whole group, keeping track of ideas on the board. Push students to support their ideas with evidence from reviewing the board, but don't worry about ensuring correctness at this point.

Activity (45 min)

Taking Input from the Board

Transition: Send students to Code Studio.

Wrap Up (2 min)

Journal: Given the types of board events that you saw today, how might you take input from other elements on the board? What other events do you think might exist?

  • Board Events
  • 3
  • 4
  • 5
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Board Events

The onBoardEvent() block allows you to start taking input from your Circuit Playground. Look at the program with a partner and discuss what you think it'll do. Once you've come up with an answer, click "Run" to check your answer.

  • What will happen when you press the left button down?
View on Code Studio

Student Instructions

Using Board Events

Right now the LED turns on when the left button (buttonL) is pressed down. It should turn off when the right button (buttonR) is pressed down.

expandable

Do this

  • Add another onBoardEvent() block to turn the LED off when the right button is pressed down.
View on Code Studio

Student Instructions

Other Board Events with the Buttons

Instead of using the right button, you can make the LED turn off when the left button comes back up.

Do this

  • Create an 'up' event for the left button.
  • Add code to turn the LED off when the button pops back up.
  • Using the Switch
  • 7
  • 8
  • 9
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Using the Toggle Switch

The toggle switch can flip in one of two positions: open and close. This program uses it to make a light switch. Right now it can only turn the light on.

Do this

  • Create a new toggle switch event that is triggered when the switch is flipped to close.
  • Add code so that when the toggle switch is flipped to close the light turns off.
View on Code Studio

Student Instructions

More on the Toggle Switch

You can also check whether the toggle switch is in the open or close position anywhere in your program.

What different things does the button do when the toggle switch is open or close?

View on Code Studio

Student Instructions

State and the Toggle Switch

This program tells you whether the toggle switch is opened or closed, but it needs the toggleSwitch.isOpen block to work. toggleSwitch.isOpen is true if the switch is opened and false if the switch is closed.

Do this

  • Complete the if-statement with the toggleSwitch.isOpen property.
  • Run the program and press the left button, then flip the toggle switch and press the button again.
View on Code Studio

Student Instructions

Using the Buzzer

You can use the buzzer in a lot of different ways, but right now we're just going to use the buzzer.frequency().

Do this

  • Pull out a buzzer.frequency() block from the toolbox and play around with it. You'll notice it has two parameters, what happens when you change either, or both of those?
  • Run the program and enjoy the music of your Circuit Playground.
View on Code Studio

Student Instructions

Buzzer Duration

As you've noticed, buzzer.frequency() has two parameters: frequency and duration. Both of these take a number as input.

  • Frequency changes how the buzzer sounds. The bigger the number you give it, the higher the buzzer's sound will be.

  • Duration determines how long the buzzer is going to play a sound for in milliseconds.

For right now we're going to focus on duration, the second parameter.

Do this

Try out a few things with duration:

  • Change the duration of the buzzer to something longer than 100.
  • Try setting the duration to 0, what happens?

Run the program multiple times to see what happens before you move on.

View on Code Studio

Student Instructions

Buzzer and the Buttons

This program should use the buttons to play long and short buzzes.

Do this

  • Change the duration parameter of buzzer.frequency to make the left button play a long buzz, and the right button play a short buzz.

Standards Alignment

View full course alignment

CSTA K-12 Computer Science Standards (2017)

AP - Algorithms & Programming
  • 2-AP-11 - Create clearly named variables that represent different data types and perform operations on their values.
  • 2-AP-12 - Design and iteratively develop programs that combine control structures, including nested loops and compound conditionals.
  • 2-AP-13 - Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
  • 2-AP-16 - Incorporate existing code, media, and libraries into original programs, and give attribution.
  • 2-AP-17 - Systematically test and refine programs using a range of test cases.
  • 2-AP-19 - Document programs in order to make them easier to follow, test, and debug.
CS - Computing Systems
  • 2-CS-01 - Recommend improvements to the design of computing devices, based on an analysis of how users interact with the devices.
  • 2-CS-02 - Design projects that combine hardware and software components to collect and exchange data.
  • 2-CS-03 - Systematically identify and fix problems with computing devices and their components.