Class Meeting 05: Responding to Sensor Input

Learning Goals

Conditional Statements (if, else if, else)

Here is a link to the presentation slides for today.

Programming Exercise #1: Programming ClicBot to Respond to Touch Input

In this first exercise, you will gain experience using conditional statements (if, else if, else). To begin, build a ClicBot that has 3 joints with the base and brain at opposite ends, as pictured below.

Three Joint ClicBot

Create a new program called "Touch Input" and build the following program. When you add the Speaker starts playing sound [] block, you'll need to record a sound. Record a sound that you think represents happiness. For example, you could record yourself saying "yay!"

exercise 1 starter code

Once you've built this program and connected to your ClicBot, run the program. After the program starts running, pet your ClicBot on the head and see what it does!

Discuss as a group: What happens if you touch the robot's head before it has finished playing the happy emotion on its screen? Why does the robot respond this way?

Now, build on your program by adding an else block by clicking on the blue box next to the if, as shown below, and dragging the else underneath the if.

add else block

Now, exit out of this view and add onto your program by putting the Screen starts playing emotion [Curious] inside the else.

add curious emotion

Before you run the program, discuss the prompt below.

Discuss as a group: Before you run this program, how do you expect your robot to behave with this program running?

Now run your program. Try to touch the robot on the head. What happens? If you don't notice anything happening, try keeping your hand on the robot's head for 5-10 seconds. Once you've seen the robot display both happy and curious emotions, discuss the prompt below.

Discuss as a group: Did the program run as you expected? Why or why not?

Next, you will continue adding onto your program. You will add an else if block similarly to how you added the else block, see the image below.

add else if

Exit out of this view and add onto your program, copying what you see in the following code snippet. We'll need to use a new block, the or block that you can find in the "Operators" section of the menu.

add responiveness to left/right touch

Run your program. Try both touching the robot on the head and also squeezing it on both sides of the head with one hand. Once you've been able to see all three emotions (happy, angry, curious) discuss the question below.

Discuss as a group: What would happen with your program and your robot if you were to both touch the top of the robot's head and the side of the robot's head at the same time?

You've now finished this first exercise. Please wait for all groups to finish. Then, we'll go over the exercise as a class together.

Programming Exercise #2: Opposite Gesture Robot

For this programming exercise, your goal is to have your ClicBot detect your directional gestures (left, right, up, down) and respond by moving it's head in the opposite direction:

You will also want the robots head to return to a central position while it waits for the next gesture.

Let's begin with some starter code. You can use the same ClicBot shape you used for Programming Exercise #1. Begin a new program called "Opposite Gesture" and construct the following program.

Exercise 2 Starter Code

When you drag on the Joint [] starts rotating to [], you'll need to select the 2nearby joint, which will be the one that will rotate to allow the robot's head to move up and down. You can see what the joint interface selection looks like below.

Joint Selection

What you want to pay attention to is the joint angle listed at the bottom of the screen (circled in yellow in the image above). You can move your robot and this number should change. This will tell you what angle you want to program the robot so that it looks forward, looks up, or looks down.

IMPORTANT: If your robot has a value closer to 180 (as opposed to around 0), then you'll want to change the numbers in your starter code from 30 and 0 to 210 and 180.

Now, try running your program and giving your robot a "up" gesture. Does it work as you expect?

Discuss as a group: What is happening in the else block?

Now, as a group build the rest of the program so that your ClicBot can respond to the remaining gestures (down, right, left).

Here are some tips:

Programming Exercise #3: Emotional Robot Dog

The goal of this programming exercise is to design a robot dog that responds with emotion to your touch and/or gestures. This exercise is intentionally open ended so that you can express your creativity in the types of interactions and gestures you produce.

Examples of emotional reactions you could program include:

Some recommendations we have for you as you approach this exercise include:

Robot Dog

Programming Exercise #4: Using a Joint as a Sensor

The goal of this exercise is to create a ClicBot robot that is controlled by the rotation of one of it's joints. If you rotate the joint quickly, the robot will move fast. If you rotate the joint slowly, the robot will move slowly. Let's get started.

First, build a ClicBot that looks like the following picture:

Robot Exercise 4

As mentioned before, you'll rotate one of the ClicBot's joints to guide it's forward speed. We've attached the ClicBot holder to make this joint easier to rotate. See below for an example of how to rotate this joint for this exercise.

Rotating Joint

Now that we have the robot set up, let's get more specific about it's behavior. You'll design three "modes" for the robot:

The following piece of code gives you a snippet of what you'll need for this program.

joint as sensor starter code

You'll want to use the if block to detect how fast your joint is moving. You'll use the sensor block Detect rotation speed of Joint [NO.X] to get the rotation speed of the joint. You'll need to compare it to a numeric value. Then, you'll need to specify the wheel rotations that will follow that joint rotation speed using the Wheel [NO.X + Direction] rotation, speed block.

Some tips for this exercise:

Requirements to Meet Expectations for Today's Class


"Programming Exercise #2: Opposite Gesture Robot" was designed after the programming exercise from the ClicBot Academy Conditional Clause & Gesture Sensing Robot instructional YouTube video. "Programming Exercise #4: Using a Joint as a Sensor" was designed after the programming exercise from ClicBot Academy ClicBot Academy. Joint as A Sensor & Magical Cars instructional YouTube video.