Grade 5 · Coding & Electronics · Lesson 59

Scratch Pet: Is the Key Down?

Use a loop and an if–else condition to react to a changing input.

Time2 × 45 minutesTeamsPairs; switch maker and tester rolesEquipmentScratch on a keyboard-equipped computer

Project inspiration · 3 images

See, build, and test

Use these images to understand the kind of making, details, and testing involved. They are inspiration—not designs students must copy.

Generated project illustrations are shared by the two grade versions of this project. Follow your grade’s directions and starter recipe—not the pictured screen or hidden wiring.

Student directions · Grade 5

What will you make and do?

You will make a digital pet that stays happy for as long as space is held and rests as soon as it is released. This is a continuous keyboard check, not a timed greeting.

Teacher overview: Students will plan the specific object described below, make and test a first version, then use their results to improve it. Read one step together, demonstrate the unfamiliar action, and let students complete that step before moving on.

  1. 1Plan your project

    Write the two expected states: held means Happy; released means Rest. Sketch the two faces and save a new copy of your Scratch project.

  2. 2Make the first parts

    Create or check Rest and Happy costumes on one sprite. Remove the earlier space-key event script so it cannot compete with the new program.

  3. 3Put it together

    Follow the coding guide to put a key-pressed condition inside an if–else inside forever. Place one costume in each branch and a short wait inside the loop.

  4. 4Test it fairly

    Try three two-second holds and releases. Record the costume while held, after release and while holding a different key.

  5. 5Improve one part

    Fix one condition, branch or loop placement if the pet gets stuck. Repeat all three input cases and save the revised project.

  6. 6Show and turn in

    Show the pet and submit the saved project and test sheet. Explain why the condition needs to be checked repeatedly.

How will you know it works?

  • The pet stays Happy while space is held and Rest while released.
  • The sensing condition is checked inside a repeating loop.
  • Student explains both branches and records each test case.

Before you move on

Explain which measurement, input or design choice you are testing. Predict the result and record what stays the same so your comparison is fair.

Machine cutting, printer setup and electrical power-on require adult approval. A basic-materials model is a valid way to demonstrate the design when equipment is unavailable.

Keep your plan, test notes and before-and-after evidence with the printable design brief below. Use the detailed teacher plan and any coding guide for tool instructions. These steps continue across the lesson’s meetings, not all in one sitting.

Jump to step-by-step coding guide ↓

Beginner sequence · Step 2 of 3

Start here

Readiness check: Students can build the event-based pet and explain its order. Rehearse true/false with a held and released key before combining blocks.

Exact starter recipe

Use one sprite with costumes named Rest and Happy. Remove the earlier space-key event script so it cannot conflict.
when green flag clicked
forever
  if <key [space] pressed?> then
    switch costume to [Happy]
  else
    switch costume to [Rest]
  end
  wait (0.05) seconds
end
The sensing block reports true or false; forever checks it again.

Make the simplest version work first. Then use the test and extension in this lesson.

Teacher learning resources

Preview external resources before class. The circuit reference explains the science; use this page’s specified AA kit and connection list for the build.

Scratch · Grade 5 · Guided practice

How to code this project

What you will make: Check continuously whether space is held, instead of running a timed greeting.

Core coding lesson. Save a new copy and remove the old space-key event script so two scripts do not compete.

Before you start

  • Use one pet sprite with Rest and Happy costumes.
  • Find Control (orange), Sensing (light blue), Looks (purple) and Events (yellow).

Opening and saving: Open the teacher-approved Scratch editor and select your sprite. The recipe is a block-by-block guide, not text to paste into Scratch. Build blocks with matching names and keep indented lines inside their enclosing blocks. Save through File → Save to your computer as an .sb3 file; no public sharing is required.

Build it one step at a time

  1. Add when green flag clicked from Events, then snap a forever block underneath.
  2. Place if–else INSIDE forever. Drag the hexagonal key space pressed? Sensing block into the hexagonal condition hole. This is not the yellow key-pressed event hat.
  3. Put switch costume to Happy in the if branch and switch costume to Rest in else.
  4. Inside forever, below the entire if–else, add wait 0.05 seconds.
  5. Click the green flag. Hold space for two seconds, release it for two seconds, and repeat three times.
  6. Point to the branch that runs on release; save your project and write why the loop is needed.

Your block recipe

when green flag clicked
forever
  if <key [space] pressed?> then
    switch costume to [Happy]
  else
    switch costume to [Rest]
  wait (0.05) seconds

Predict, test and record

Write your name and date on the printed page. Run every test, note the actual result, and retest after one change.

Try this inputExpected resultActual result / retest
Hold spaceHappy remains visible________________
Release spaceRest returns________________
Hold a different keyRest________________

If it does not work

  • Checks only once? Put if–else inside forever.
  • Flickers between faces? Remove the old space-event script or other scripts changing the costume.
  • Cannot fit the block? Use Sensing’s hexagonal condition, not Events’ hat.

Make one change

Try another key in the sensing dropdown; update the three test cases before testing.

We changed __________. We predicted __________. Our test showed __________.

Teacher pacing: Use a 45-minute coding session: 5 minutes to predict, 10 to model a first working response, 20 for paired building, 7 for tests and 3 to save and explain. Continue in another meeting if setup or debugging takes longer; do not rush hardware checks.

Grade 5 support: Start with the working reference version, then explain each condition or state. Keep an input/expected/actual test log and change only one variable at a time.

Teacher learning resources

Preview resources before class. Follow this page’s specified reference build; another tutorial may use different hardware.

Print support: The lesson’s Print button includes this guide, recipe and test table.

For teachers

Engineering standards

This shows how the lesson connects to the national NGSS engineering standards and what student work you can collect.

Practiced

3-5-ETS1-1

Define the user's need for scratch pet: is the key down? and agree on the listed success criteria and limits.

What students show: A stated user need and checked criteria; this is engineering-design practice, not a separate coding standard.
Practiced

3-5-ETS1-2

Compare two possible routes, pet behaviors, or switch arrangements before selecting one for the user.

What students show: Two small plans and a reason for the selected design.
Assessed

3-5-ETS1-3

Repeat the stated test under the same conditions, identify a failure, change one feature, and retest.

What students show: Before-and-after test entries and an explanation connecting the change to the result.

Materials

  • Teacher-managed computer with physical keyboard and Scratch editor
  • Paper for two pet-costume sketches
  • Approved folder for saving .sb3 project files
  • Printable design brief

Teacher preparation

  1. Check readiness: Students can build the event-based pet and explain its order. Rehearse true/false with a held and released key before combining blocks.
  2. Prepare the event-based pet as an optional starting file.
  3. Show the difference between the event hat when-key-pressed and the Boolean sensing block key-pressed?.
  4. Prepare the printable design brief at the end of this page. Use the project picture as inspiration, not a technical diagram or a required artistic finish.

Step by step

How to teach it

Class 1 · 0–5

Meet the beginner challenge

  1. Make a digital pet that stays happy while the space key is held and rests when it is released. Use keyboard sensing rather than an external sensor.
  2. Read the criteria together. Ask students to predict what the system should do.
Class 1 · 5–12

Model one working action

  1. Use a pet with Rest and Happy costumes. Save a new copy before changing its code.
  2. Remove the earlier space-key event script and build the green-flag forever loop in the recipe.
  3. Use the exact starter recipe below. Ask a student to explain the action before demonstrating a second step.
Class 1 · 12–20

Compare two small plans

  1. Sketch or arrange two possible solutions using the same materials.
  2. Compare each plan against the criteria; choose one and state a reason.
Class 1 · 20–38

Build the smallest working version

  1. Place the key-space-pressed sensing block inside the if–else opening.
  2. Put Happy in the then branch and Rest in the else branch; include a short wait inside the loop.
  3. Start the program. Hold space for two seconds, release it, and observe both states.
Class 1 · 38–45

Check and save

  1. Show one working action. Record where you stopped and what you need next.
  2. Save the project to the teacher-approved location using File → Save to your computer.
  3. Return materials and reset the workspace.
Class 2 · 0–8

Rebuild understanding

  1. Read the saved plan and predict the first test.
  2. Reset the starting state and test the simplest action.
Class 2 · 8–22

Test fairly

  1. List expected states first: space held → Happy; space released → Rest; different key held → Rest.
  2. Run three hold-and-release cycles, using about two seconds per state. Record the actual costume.
  3. Stop and restart the project; confirm it checks the current input rather than staying stuck in the previous costume.
Class 2 · 22–35

Improve one thing

  1. Identify the first point where the result differed from the prediction.
  2. Change one instruction, code block, or mechanical feature. Repeat exactly the same test.
  3. If the core skill is secure, offer the extension; otherwise use the support option.
Class 2 · 35–45

Share evidence and clean

  1. Let another pair try the project with your directions.
  2. Use the worksheet to show an original result, a change, and the new result.
  3. Save or label the work, then return materials.

Assessment

  • The pet stays Happy while space is held and Rest while released.
  • The sensing condition is checked inside a repeating loop.
  • Student explains both branches and records each test case.
  • Student predicts an outcome before testing.
  • Test record distinguishes expected and actual results.
  • Student explains why the revision helps; neat decoration and speed are not assessment criteria.

Supports & extensions

  • Start with only if-space-pressed then Happy; add the else-Rest branch after discussing what release should do.
  • Offer larger cards or tools, a stabilized work surface, picture cues, partner assistance, and spoken or dictated evidence.
  • Extension after success: Add an on-screen button using mouse sensing as a separate version; write new test cases and avoid mixing two competing scripts.

Safety & responsible use

  • Use a teacher-managed Scratch session or approved offline editor; no account or public upload is required.
  • Use invented names and original pet artwork. Do not enter personal information, record classmates, or browse community content independently.
  • Keep sound quiet or omit it. Use short screen breaks and share keyboard control.

No-machine alternative

Use Happy and Rest cards. One partner holds or releases a space card while another repeatedly checks it and shows the correct costume.

Printable student design brief

The challenge

Make a digital pet that stays happy while the space key is held and rests when it is released. Use keyboard sensing rather than an external sensor.

Success criteria

  • The pet stays Happy while space is held and Rest while released.
  • The sensing condition is checked inside a repeating loop.
  • Student explains both branches and records each test case.

Constraints

  • One sprite, two costumes, and the listed beginner blocks first.
  • Use two 45-minute meetings; start with the working system before decoration.
  • Keep test conditions consistent and change only one feature at a time.

Engineer’s notes

Our pet should respond to… Our two states are…
Sketch two possible visual responses and choose one.
Write the condition. Mark the true branch and false branch.
Test table: space held / released / other key… expected costume… actual costume…
Why must the check repeat?…
Bug location… one change… retest evidence…

Printable beginner test record

Predict. Try. Improve.

Name ____________________ Partner ____________________

Our success rule: __________________________________________________

TrialInput / actionExpected resultFirst resultResult after one change
1
2
3
4
5
6
7
8
9
10

Use three rows for the introductory tests or all ten for switch reliability. Write the held, released, or other-key state for each sensing test.

One thing we changed: ______________________________________________

Things we kept the same: ____________________________________________

Our evidence shows: _________________________________________________