Hands-on Activity: Using Waits, Loops and Switches

Contributed by: GK-12 Program, Computational Neurobiology Center, College of Engineering, University of Missouri

A photograph of an assembled LEGO MINDSTORMS NXT robot in a classroom, showing the brick/computer, sound sensor, cables, and ultrasonic sensor attached at top, looking like two eyes.
Programs involving loops, switches and wait blocks enable more efficient control of LEGO robots.
copyright
Copyright © 2009 Patrick Fleischman, Wikimedia Commons http://commons.wikimedia.org/wiki/File:USMC-090507-M-3035F-562.jpg

Summary

Students are given a difficult challenge that requires they integrate what they have learned so far in the unit about wait blocks, loops and switches. They incorporate these tools into their programming of the LEGO® MINDSTORMS® robots to perform different tasks depending on input from a sound sensor and two touch sensors. This activity helps students understand how similar logic is implemented for other every day device operations via computer programs. A PowerPoint® presentation, pre/post quizzes and worksheet are provided.
This engineering curriculum meets Next Generation Science Standards (NGSS).

Engineering Connection

Programming tools similar to those that students build into their skill sets from this activity are incorporated into the programming that runs all sorts of devices we use every day. Many of these programs are designed to "wait" until a user does something, such as credit card readers waiting until a card is swiped. In addition, depending on what happens (new sensor input) the program behaves a certain way. For example, if the device was unable to read the card, it might display a message to try again. And once the task is completed with one card, the program "goes back" and gets ready to accept another card swipe. The card reader program is continuously in this loop and designed to respond to various sensory input.

Pre-Req Knowledge

Completion of the How Do You Make a Program Wait? lesson and its associated activity.

Learning Objectives

After this activity, students should be able to:

  • Explain what loops and switches are.
  • Identify programming situations in which loops and switches should be utilized.
  • Write programs that utilize loops and switches.

More Curriculum Like This

How Do You Make Loops and Switches?

Students learn how to program using loops and switches. Using the LEGO® MINDSTORMS® robots, sensors and software, student pairs perform three mini programming activities using loops and switches individually, and then combined.

How Do You Make a Program Wait?

Building on the programming basics learned so far in the unit, students next learn how to program using sensors rather than by specifying exact durations. Working with the LEGO® MINDSTORMS® EV3 robots and software, they learn about wait blocks and how to use them in conjunction with move blocks set ...

Wait Program!

After completing the associated lesson, students test their understanding in two programming tasks that utilize LEGO® MINDSTORMS® NXT robots and sound/touch sensors. They create programs that make the robots change speed several times when a touch sensor is pressed.

Elementary Activity
What Is a Computer Program?

Through four lesson and four activities, students are introduced to the logic behind programming. Starting with very basic commands, they develop programming skills while they create and test programs using LEGO® MINDSTORMS® robots. The unit is designed to be motivational for student learning, so th...

Educational Standards

Each TeachEngineering lesson or activity is correlated to one or more K-12 science, technology, engineering or math (STEM) educational standards.

All 100,000+ K-12 STEM standards covered in TeachEngineering are collected, maintained and packaged by the Achievement Standards Network (ASN), a project of D2L (www.achievementstandards.org).

In the ASN, standards are hierarchically structured: first by source; e.g., by state; within source by type; e.g., science or mathematics; within type by subtype, then by grade, etc.

  • Generate and compare multiple possible solutions to a problem based on how well each is likely to meet the criteria and constraints of the problem. (Grades 3 - 5) Details... View more aligned curriculum... Do you agree with this alignment?
  • Analyze data from tests to determine similarities and differences among several design solutions to identify the best characteristics of each that can be combined into a new solution to better meet the criteria for success. (Grades 6 - 8) Details... View more aligned curriculum... Do you agree with this alignment?
  • The design process is a purposeful method of planning practical solutions to problems. (Grades 3 - 5) Details... View more aligned curriculum... Do you agree with this alignment?
  • Test and evaluate the solutions for the design problem. (Grades 3 - 5) Details... View more aligned curriculum... Do you agree with this alignment?
  • The processing of information through the use of technology can be used to help humans make decisions and solve problems. (Grades 3 - 5) Details... View more aligned curriculum... Do you agree with this alignment?
  • New products and systems can be developed to solve problems or to help do things that could not be done without the help of technology. (Grades 6 - 8) Details... View more aligned curriculum... Do you agree with this alignment?
  • Specify criteria and constraints for the design. (Grades 6 - 8) Details... View more aligned curriculum... Do you agree with this alignment?
  • Describe how new technologies have helped scientists make better observations and measurements for investigations (e.g., telescopes, electronic balances, electronic microscopes, x-ray technology, computers, ultrasounds, computer probes such as thermometers) (Grade 5) Details... View more aligned curriculum... Do you agree with this alignment?
Suggest an alignment not listed above

Materials List

Each group needs:

To share with the entire class:

Introduction/Motivation

Suppose you need to program a robot to carry a load back and forth between two points 100 times. It would be very tedious to write the same set of blocks 100 times in your program. A loop allows you to easily make your program repeat a set of steps until a condition is satisfied.

People react differently to different stimuli. For example, in order to be comfortable outside, we dress in a particular way depending on the weather. If it is hot, we wear t-shirts and shorts, if it is a little colder maybe jeans and a sweater. And when it is snowing, we put on heavy coats, boots and gloves. Similarly, we can design programs to respond differently, depending on whether different conditions are satisfied.

Switches enable programs to respond differently based on whether certain conditions are satisfied. For instance, to program a calculator, one could use a switch to direct the calculator to perform the correct operations depending on whether plus (+), minus (-), multiply (x) or divide (/) keys are pressed. Today we will study about how you can incorporate loops and switches into programs.

Vocabulary/Definitions

brainstorming: Thinking of ideas as a group.

iteration: Doing something again, especially with the intent to make improvements.

loop: A command or "operator" that repeats a set of commands.

switch: In programming, a switch is an object that gives different commands depending on which state it is in

Procedure

Before the Activity

  • Gather materials and make copies of the Waits, Loops and Switches Pre/Post Quiz, two each per student, and the Waits, Loops and Switches Worksheet, one per student. The quizzes and worksheet are provided as separate attachments, and also embedded in the presentation to make it easier to go through them as a class, if desired.
  • The content previously presented in the Loops and Switches Presentation in the associated lesson, How Do You Make Loops and Switches?, is essential for students to understand and remember in order to complete this activity. Thus, it is recommended that a copy of this presentation be provided for each group as a reference.
  • Assemble the LEGO MINDSTORMS NXT taskbots for each group, following the instructions in the base set manual
  • In advance, perform the entire activity so as to be familiar with all details, especially the programming solution on slides 5-8.
  • Present the activity challenge to students using the 11-slide Using Waits, Loops and Switches Presentation, a PowerPoint file. Set up a computer/projector to show the presentation to the class.
  • Arrange for enough computers so you have one for each student group. Make sure each computer has the LEGO software loaded.

With the Students

  1. Administer the pre-quiz by handing out paper copies (also on slide 2). The answers are provided for the teacher on slide 3.
  2. (slide 4) Introduce students to the activity challenge in which they combine wait blocks, loops and switches to perform a task. After attaching two touch sensors (A and B) and a sound sensor to the robot, program the LEGO robots to perform the following:
  • Stay at rest and display "Ready" on the screen until detecting a loud noise. Once a load noise is detected:
  • Move left and display "left" whenever touch sensor A is pressed
  • Move right and display "right" whenever touch sensor B is pressed.
  • Move forward and display "forward" whenever both touch sensors A and B are pressed.
  • Stay at rest and display "stop" when no buttons are pressed.
  1. Divide the class into student pairs and distribute the worksheets.
  2. Expect each student pair to come up with a LEGO MINDSTORMS NXT program design to meet the activity challenge by completing the worksheet, including providing the settings for each block in the program.
  3. Once students have designed their programs, guided by the worksheet, direct them to create the program using the LEGO MINDSTORMS NXT software.
  4. Oversee students as they download and test their programs using LEGO taskbots with touch and sound sensors installed. If a program does not work correctly, have students troubleshoot to isolate the problem to determine which block needs adjustment or which block setting might be incorrect. Encourage students to talk with their group partners to figure out and fix any problems. Each time the group alters its program, have them download the revised program and test it; by doing this, they are experiencing first-hand the iteration process that is typical in the design process.
  5. Once all groups have made their programs work as intended (or the time allotted for developing programs is over), review the programming solution (on slides 5-8) with the class. Have students share and discuss their observations, experiences and difficulties in creating successful programs.
  6. Administer the post-quiz by handing out paper copies (also on slide 9). Answers are on slide 10. Vocabulary are provided on slide 11.

Attachments

Investigating Questions

  • In your own words, explain what you did at every step of the engineering design process as you created programming solutions to meet the challenge. (The steps are: 1-state the problem, 2-generate ideas [brainstorming], 3-select the best solution, 4-create a prototype solution or item, 5-test and evaluate to see if it works, 6-present results as a successful solution.)
  • In your own words, tell me what is meant by "brainstorming" and "iteration"?
  • What are some examples of everyday devices that likely use programming similar to the wait blocks, loops and switches you have used with the LEGO robots?

Assessment

Pre-Activity Assessment

Pre-Quiz: Administer the three-question Waits, Loops and Switches Pre/Post Quiz (also on slide 2) to assess what students retained about loops and switches from the associated lesson. Answers are provided on the Waits, Loops and Switches Pre/Post-Quiz Answer Key (and slide 3). Administer the same quiz at lesson end.

Activity-Embedded Assessment

Worksheet & Iterations: Make sure that students first complete the Waits, Loops and Switches Worksheet and have a good idea of what they want to program before using the LEGO software to create and test their programs. Like engineers, it is rare for students to create programs perfectly on the first attempt, so expect them to take some time and repeated efforts to make them work as intended. The teacher should, however, know the solution well (provided in the slides) and ask pertinent questions to guide students to successful solutions. Review students' worksheet answers to gauge their comprehension of the concepts covered.

Post-Activity Assessment

Post-Quiz: At activity end, administer the Waits, Loops and Switches Pre/Post Quiz again by handing out paper copies (also on slide 9). Expect students to be able to provide more in-depth responses to the questions on the post-quiz after completing the activity. Compare students' answers to their pre-quiz answers to assess their comprehension of the activity content. Answers are provided on the Waits, Loops and Switches Pre/Post-Quiz Answer Key (and slide 10).

References

NXT User's Guide. Accessed Jul 17, 2013 http://goo.gl/wuhSUA

Contributors

Riaz Helfer, Pranit Samarth, Satish S. Nair

Copyright

© 2014 by Regents of the University of Colorado; original © 2013 Curators of the University of Missouri

Supporting Program

GK-12 Program, Computational Neurobiology Center, College of Engineering, University of Missouri

Acknowledgements

This curriculum was developed under National Science Foundation GK-12 grant no. DGE 0440524. However, these contents do not necessarily represent the policies of the National Science Foundation, and you should not assume endorsement by the federal government.

Last modified: February 12, 2018

Comments