Ski Slope – Piper

SKI SLOPE

PIPER MAKE EDUCATOR RESOURCES SERIES

To do this project, you will need a Piper Make Starter Kit. Get yours here:

Build a game controller using phototransistors!

To get started, head to Piper Make and hit this icon:

Time: 45 minutes

Age Range: 8+

Difficulty: Intermediate

Students will enable 2 phototransistors to act as keys on a keyboard. There is wiring required, as well as programming using loops, variables, and conditionals. The project culminates with the game Ski Slope found in Pip's Arcade.

Note: There are step by step instructions for the students to follow in the tutorials included in each project on Piper Make. These provide directions both for writing code and for building the electronic circuits. The tutorials are well-defined and most students will be able to follow them with little assistance required.

LEARNING OBJECTIVES

Students will:

 

  • Practice breadboarding and wiring
  • Review and understand computational concepts of:
    • loops: running the same sequence multiple times.
    • sequence: identifying a series of steps for a task
  • Create programs that include events, loops, and conditionals.
  • Decompose problems into smaller, manageable tasks which may themselves be decomposed.
  • Test and debug a program or algorithm to ensure it accomplishes the intended task.
  • Perform different roles when collaborating with peers during the design, implementation, and review stages of program development.

STANDARDS ALIGNMENT

CSTA's K-12 Standards 

1B-CS-03 Determine potential solutions to solve simple hardware and software problems using common troubleshooting strategies. Subconcept: Troubleshooting; Practice 6.2

1B-DA-07 Use data to highlight or propose cause-and-effect relationships, predict outcomes, or communicate an idea. Subconcept: Inference & Models; Practice 7.1

1B-AP-09 Create programs that use variables to store and modify data. Subconcept: Variables; Practice 5.2

1B-AP-10 Create programs that include sequences, events, loops, and conditionals. Subconcept: Control; Practice 5.2

1B-AP-11 Decompose (break down) problems into smaller, manageable subproblems to facilitate the program development process. Subconcept: Modularity; Practice 3.2

1B-AP-12 Modify, remix, or incorporate portions of an existing program into one’s own work, to develop something new or add more advanced features. Subconcept: Modularity; Practice 5.3

1B-AP-15 Test and debug (identify and fix errors) a program or algorithm to ensure it runs as intended. Subconcept: Program Development; Practice 6.1, 6.2

CCSS ELA

CCSS.ELA.L.W.3.8: Recall information from experiences or gather information from print and digital sources; take brief notes on sources and sort evidence into provided categories.

CCSS.ELA.L.W.3.10: Write routinely over extended time frames (time for research, reflection, and revision) and shorter time frames (a single sitting or a day or two) for a range of discipline-specific tasks, purposes, and audiences.

World-Class Instructional Design and Assessment (WIDA) English Language Proficiency Standards

ELD-SI.K-3.Argue:

  • Ask questions about others’ opinions
  • Support own opinions with reasons
  • Clarify and elaborate ideas based on feedback
  • Defend change in one’s own thinking
  • Revise one’s own opinions based on new information

 

ELD-SC.2-3.Argue.Interpretive:

  • Interpret scientific arguments by
  • Identifying potential evidence from data, models, and/or information from investigations of phenomena or design solutions
  • Analyzing whether evidence is relevant or not
  • Distinguishing between evidence and opinions

California's K-12 Computer Science Standards

3-5.CS.3 Determine potential solutions to solve simple hardware and software problems using common troubleshooting strategies.

3-5.DA.9 Use data to highlight and/or propose relationships, predict outcomes, or communicate ideas.

3-5.AP.11 Create programs that use variables to store and modify data.

3-5.AP.12 Create programs that include events, loops, and conditionals.

3-5.AP.13 Decompose problems into smaller, manageable tasks which may themselves be decomposed.

3-5.AP.14 Create programs by incorporating smaller portions of existing programs, to develop something new or add more advanced features.

3-5.AP.17 Test and debug a program or algorithm to ensure it accomplishes the intended task.

Common Core English Language Arts

CCSS.ELA.L.W.3.8: Recall information from experiences or gather information from print and digital sources; take brief notes on sources and sort evidence into provided categories.

CCSS.ELA.L.W.3.10: Write routinely over extended time frames (time for research, reflection, and revision) and shorter time frames (a single sitting or a day or two) for a range of discipline-specific tasks, purposes, and audiences.

California English Language Development Standards

CA ELD.3.C.11: Supporting own opinions and evaluating others’ opinions in speaking and writing

CA ELD.3.C.12: Selecting and applying varied and precise vocabulary and language structures to effectively convey ideas

Michigan Integrated Technology Competencies for Students (MITECS)

1B-CS-03 Determine potential solutions to solve simple hardware and software problems using common troubleshooting strategies. Subconcept: Troubleshooting; Practice 6.2

1B-DA-07 Use data to highlight or propose cause-and-effect relationships, predict outcomes, or communicate an idea. Subconcept: Inference & Models; Practice 7.1

1B-AP-09 Create programs that use variables to store and modify data. Subconcept: Variables; Practice 5.2

1B-AP-10 Create programs that include sequences, events, loops, and conditionals. Subconcept: Control; Practice 5.2

1B-AP-11 Decompose (break down) problems into smaller, manageable subproblems to facilitate the program development process. Subconcept: Modularity; Practice 3.2

1B-AP-12 Modify, remix, or incorporate portions of an existing program into one’s own work, to develop something new or add more advanced features. Subconcept: Modularity; Practice 5.3

1B-AP-15 Test and debug (identify and fix errors) a program or algorithm to ensure it runs as intended. Subconcept: Program Development; Practice 6.1, 6.2

Michigan English Language Arts

Michigan ELA, Grade 3-8, Research, 8: Recall information from experiences or gather information from print and digital sources; take brief notes on sources and sort evidence into provided categories.

Michigan ELA, Grade 3-8, Range of Writing, 10: Write routinely over extended time frames (time for research, reflection, and revision) and shorter time frames (a single sitting or a day or two) for a range of discipline-specific tasks, purposes, and audiences.

WIDA English Language Development

ELD-SI.K-3.Argue:

  • Ask questions about others’ opinions
  • Support own opinions with reasons
  • Clarify and elaborate ideas based on feedback
  • Defend change in one’s own thinking
  • Revise one’s own opinions based on new information

 

ELD-SC.2-3.Argue.Interpretive:

  • Interpret scientific arguments by
  • Identifying potential evidence from data, models, and/or information from investigations of phenomena or design solutions
  • Analyzing whether evidence is relevant or not
  • Distinguishing between evidence and opinions

Science Texas Essential Knowledge & Skills

Grade 3

(b)(2) Scientific investigation and reasoning. The student uses scientific practices during laboratory and outdoor investigations. The student is expected to:
(A) plan and implement descriptive investigations, including asking and answering questions, making inferences, and selecting and using equipment or technology needed, to solve a specific problem in the natural world;

(b)(3) Scientific investigation and reasoning. The student knows that information, critical thinking, scientific problem solving, and the contributions of scientists are used in making decisions.

Grade 4

(a)(1)(A) Within the physical environment, students know about the physical properties of matter including mass, volume, states of matter, temperature, magnetism, and the ability to sink or float. Students will differentiate among forms of energy including mechanical, light, sound, and thermal energy. Students will explore electrical circuits and design descriptive investigations to explore the effect of force on objects.

(b)(3) Scientific investigation and reasoning. The student uses critical thinking and scientific problem solving to make informed decisions. The student is expected to:

    (A) analyze, evaluate, and critique scientific explanations by using evidence, logical reasoning, and experimental and observational testing;

    (B) represent the natural world using models such as the water cycle and stream tables and identify their limitations, including accuracy and size; and

    (C) connect grade-level appropriate science concepts with the history of science, science careers, and contributions of scientists.

Grade 5

(a)(1) In Grade 5, scientific investigations are used to learn about the natural world. Students should understand that certain types of questions can be answered by investigations and that methods, models, and conclusions built from these investigations change as new observations are made. Models of objects and events are tools for understanding the natural world and can show how systems work. They have limitations and based on new discoveries are constantly being modified to more closely reflect the natural world.

(a)(3) Recurring themes are pervasive in sciences, mathematics, and technology. These ideas transcend disciplinary boundaries and include patterns, cycles, systems, models, and change and constancy.

ELA Texas Essential Knowledge & Skills Grade 3

(b) (1)Developing and sustaining foundational language skills: listening, speaking, discussion, and thinking--oral language. The student develops oral language through listening, speaking, and discussion. The student is expected to:

    (A) listen actively, ask relevant questions to clarify information, and make pertinent comments;

    (B) follow, restate, and give oral instructions that involve a series of related sequences of action;

    (C) speak coherently about the topic under discussion, employing eye contact, speaking rate, volume, enunciation, and the conventions of language to communicate ideas effectively;

    (D) work collaboratively with others by following agreed-upon rules, norms, and protocols; and

    (E) develop social communication such as conversing politely in all situations.

ELA Texas Essential Knowledge & Skills Grades 4 & 5

(b) (1)Developing and sustaining foundational language skills: listening, speaking, discussion, and thinking--oral language. The student develops oral language through listening, speaking, and discussion. The student is expected to:

    (A) listen actively, ask relevant questions to clarify information, and make pertinent comments;

    (B) follow, restate, and give oral instructions that involve a series of related sequences of action;

    (C) express an opinion supported by accurate information, employing eye contact, speaking rate, volume, enunciation, and the conventions of language to communicate ideas effectively; and

    (D) work collaboratively with others to develop a plan of shared responsibilities.

(b)(13) Inquiry and research: listening, speaking, reading, writing, and thinking using multiple texts. The student engages in both short-term and sustained recursive inquiry processes for a variety of purposes. The student is expected to:

    (A) generate and clarify questions on a topic for formal and informal inquiry;

    (B) develop and follow a research plan with adult assistance;

    (C) identify and gather relevant information from a variety of sources;

    (D) understand credibility of primary and secondary sources;

    (E) demonstrate understanding of information gathered;

    (F) differentiate between paraphrasing and plagiarism when using source materials;

    (G) develop a bibliography; and

    (H) use an appropriate mode of delivery, whether written, oral, or multimodal, to present results.

§74.4. English Language Proficiency Standards

(c) Cross-curricular second language acquisition essential knowledge and skills.

(3) Cross-curricular second language acquisition/speaking.

(D) speak using grade-level content area vocabulary in context to internalize new English words and build academic language proficiency;

(E) share information in cooperative learning interactions;

(F) ask and give information ranging from using a very limited bank of high-frequency, high-need, concrete vocabulary, including key words and expressions needed for basic communication in academic and social contexts, to using abstract and content-based vocabulary during extended speaking assignments;

(G) express opinions, ideas, and feelings ranging from communicating single words and short phrases to participating in extended discussions on a variety of social and grade-appropriate academic topics;

(H) narrate, describe, and explain with increasing specificity and detail as more English is acquired;

CONCEPTS

Students will review loops, variables and conditionals to enable 2 phototransistors to act as left and right keys for a game found in Pip's Arcade.

PARTS

  • Pico and Breadboard
  • 5 M2M jumper wires
  • 2 10kΩ resistors
  • 1 330Ω resistor
  • 1 LED (any color will work!)
  • Flashlight

GPIO SETUP

OVERVIEW OF STEPS

Step 1: How bright is it?

In this tutorial, we are going to experiment with phototransistors. You might have heard of a transistor before. A transistor is kind of like an electronic switch. When you apply a little bit of electrical current to one of the leads of the transistor called a base, then the transistor lets a whole bunch of current begin flowing through it - from it's collector to its emitter.

A phototransistor is mostly the same as a regular transistor - except that instead of applying a bit of electrical current to it's base, we apply light. When we shine light on a phototransistor, it turns on and allows current to flow through it more easily! This means that it can be used as a light sensor!

Ready to try it out? Click NEXT.

Step 2: Grab your stuff

You will need:

  • Pico and Breadboard
  • 5 M2M jumper wires
  • 2 10kΩ resistors
  • 1 330Ω resistor
  • 1 LED (any color will work!)

 

You will also need a flashlight. The flashlight on a cell phone will work just fine if you have one available.

Once you have all of your materials, click NEXT.

Step 3: Build the first circuit

In this tutorial, we are actually going to build two different circuits. The first circuit we will build and write some code for is a night-light circuit. This circuit will help you understand how to use a phototransistor with your Raspberry Pi Pico.

First, use one of your jumper wires to connect the top ground rail on your breadboard to one of the ground pins on your Pico. Then, use another jumper wire to connect the 3V pin to the bottom positive rail on your breadboard.

Next, place the LED into your breadboard just like it shows in the image below. Make sure you place the shorter lead of the LED toward the top ground rail that you connected earlier. Then, connect the 330Ω resistor(it's stripes are orange, orange, brown) from the shorter lead of the LED to the ground rail. Finally, connect a jumper wire between the longer lead of the LED to pin GP15:

Now that the LED is connected, let's connect the Phototransistor. Place the Phototransistor in the breadboard a little further to the right than the LED like it shows in the image above. Make sure the shorter lead of the Phototransistor is toward the top ground rail. Then, connect a 10kΩ resistor (it's stripes are brown, black, orange) between the shorter lead of the Phototransistor and the top ground rail.

Next, connect a jumper wire from the longer lead of the Phototransistor to the bottom positive rail of your breadboard. Finally, connect a jumper wire from the connection between the resistor and Phototransistor to pin A1 on the Pico.

Click NEXT.

Step 4: Test your circuit

Grab a start block from the Chip menu and place it on the workspace. Then, grab a repeat forever block from the Loops menu and connect it to the start block.

Grab a print block from the Chip menu and place it inside the repeat forever block. Then, grab the "_" from inside the print block and drag it to the trash can to delete it.

Grab a read voltage from block from Chip and place it into the print block. Change A0 to A1:

Grab your flashlight and turn it on. Then, click CONNECT, select your Pico, and then click START to run your code.

Click the CONSOLE tab at the bottom of the workspace to open the console. You should see numbers scrolling by. These are the readings from the Phototransistor.

What happens to the value in the console when you shine your flashlight on the Phototransistor?

Did you notice that when the light is shining on the Phototransistor the reading is greater than 1.0, and when it is not shining on the Phototransistor, the reading is less than 1.0?

Remember how potentiometers worked in Party Lights and voltage dividers worked in Resistor Piano? This circuit works very similarly!

When light hits the Phototransistor's base, it allows more electrical current to flow through it. This means that the Phototransistor's resistance is lower. If the Phototransistor's resistance is lower than the resistance of the resistor that it is connected to, then it will swing the output to a higher voltage.

Click STOP, and then click NEXT.

Step 5: Light detector

Now that we can detect when a flashlight is shining the on the Phototransistor, let's use that to make a light detector!

Grab an if _ = _ do block from the Logic menu and connect it below the print block. Change the = (equals) to a > (greater than). Then, right-click the read voltage from block and select Duplicate. Place the new block into the left side of the _ > _ block. Then, grab a 0 block from the Values menu and place it into the right side of the _ > _ block. Change the 0 to 1:

Click the blue gear icon on the if do block to open it's mini-workspace. Grab the else block and connect it below the if block in the mini-workspace. Then, click the blue gear icon again to close it.

Grab a turn pin block from the Chip menu and place it into the do part of the if do block. Change the pin to 15.

Right-click the turn pin block and select Duplicate. Place the new block into the else part of the if do else block. Change the ON in the new block to OFF:

Try it out! Click START.

Shine your flashlight on the Phototransistor. Does the LED turn on? Awesome! Click STOP.

How could you change your code to turn your circuit into a night-light? See if you can do it!

When you're ready to try another circuit, click NEXT.

Step 6: Build the next circuit

It's time to build a new circuit! Unplug your Pico from your computer, and then take all of the resistors, jumper wires, the LED, and the Phototransistor off of your breadboard.

Once your breadboard is cleared, place your two Phototransistors in the breadboard like to shows in the image below. The longer leads should be on the left side:

Connect a jumper wire between the two longer leads of the two Phototransistors. Then, connect another jumper wire from that jumper wire to the 3V pin on the Pico.

Then, place two 10kΩ resistors (their stripes are brown, black, orange) between the shorter leads of the Phototransistors to a column to the right on the breadboard. Then, connect the other end of the resistors to a ground pin on the Pico.

Next, use another jumper wire to connect the column where the shorter leg of the top Phototransistor connects to a resistor to pin A1 on the Pico. Do this again from the other Phototransistor to pin A0.

That's it! Click NEXT.

Step 7: Code your circuit

Now that we have two Phototransistors, we can use them to detect which direction a light is coming from! All we have to do is see which Phototransistor has less resistance (a higher voltage) compared to the other one.

Since we need to read both Phototransistor values and then do a little bit of math with them, it will work best to store those values in variables.

Click the Variables menu and then click Create variable.... Name your first new variable "right light". Grab the set right light to block and place it above the print block inside of the repeat forever block. Then, grab the read voltage from block from inside the print block and place it inside the set right light block.

Click Create variable.... Name your next variable "left light". Grab the set left light to block and place it above the set right light block inside of the repeat forever block. Then, grab the read voltage from block from inside the if _ > _ blocks and place it inside the set left light block. Change the A1 to A0.
Delete the if do else block and all of the blocks inside of it by dragging it to the trash can:


To figure out where the light is coming from, we first need to know the total amount of light. We can get this easily by adding the left and right values together. Then, we can figure out what portion of the total belongs to the left light by dividing the left light by the total.

Grab a _ + _ block and place it into the set light position to block. Change the Change the + (plus) to a ÷ (divide by). Then, grab another _ + _ block and place it into the right side of the _ ÷ _ block:

Step 8: Where is the light?

Make sure your Pico is plugged into your computer. Then, click CONNECT. Select your Pico, then click START to try out your new circuit.

Click the DATA tab to at the bottom of the workspace to open the graph.

Use your flashlight to light up each Phototransistor:

What happens to the graph when you move your flashlight back and forth? How does the graph change?

Pay close attention to the numbers - they should vary between 0.0 and 1.0.

Cool, right?

Click STOP, then click NEXT.

Step 9: Use the light!

Let's turn your circuit into a game controller! You can use your flashlight to steer left or right and play a game!

Grab an if _ = _ do block from the Logic menu and connect it right above the graph block. Grab the light position block in the graph number block and place it into the left side of the _ = _ block. Change the = (equals) to a < (less than). Then, grab a 0 block from Values and place it into the right side of the _ < _ block. Change the 0 to 0.4. Then, delete the graph number block:
Click the blue gear icon on the if do block to open its mini-workspace. Add an else if, and then add an else. Click the blue gear icon on the if do block again to close the mini workspace.

Right click the _ < _ block and select Duplicate. Place the new block into the else if input. Change the < (less than) to a > (greater than), and change the 0.4 to 0.6.

Because this is going to be a controller, we want the repeat forever to loop quickly. Change the wait time in the repeat forever block to 0.1:
Now we need to add keyboard presses to the do, else if, and else statements.

Grab a keyboard press block from Actions and place it into the do part if the if do block. Change the aA to right arrow. Grab another keyboard press block and place it into the else if part of the if do block. Change it to left arrow. Finally, grab a keyboard release keys from Actions and place it into the else part of the if do block:

Step 10: Letting go...

If you ran your code right now, it would mostly work. The problem is that it's possible for one of the keys to get "stuck".

If you press a key on your keyboard, and while you hold it down, press another key - it just sort of locks up. That's what can happen in your current program.

If our flashlight changes from left to right, or right to left, we need to make sure the key gets released first. That means we need to keep track of when the left or right key gets pressed.

That means we need another variable.

Click the Variables menu and then click Create variable.... Name your new variable "key pressed". Grab the set key pressed block and connect it right below the first keyboard press block in your code. Then, grab a "_" block from Values and place it into the set key pressed block. Type "right" into the "_" block:

Right-click the set key pressed block and select Duplicate. Connect the new blocks below the second keyboard press block. Change the new block from "right" to "left".

Next, grab an if _ = _ do block from Logic and connect it right above the first key pressed block. Grab a key pressed block from Variables and place it into the left side of the _ = _ block. Then, grab a "_" block from Values and place it into the right side of the _ = _ block. Type "left" into the "_" block. Grab a keyboard release keys block from Actions and place it inside of the new if do block:

Right-click the if do block you just built and select Duplicate. Connect the new blocks above the second keyboard press block. Change the new block from "left" to "right":

Step 11: Go Skiing!

Try out your code by playing Ski Slope at Pip's Arcade.

Click the link below to open Pip's Arcade in a new tab:

When you are done and ready to add the mouse mode to your controller, close the tab with Pip's Arcade.
Ready for a challenge? Can you figure out how to use the sum of the right and left Phototransistor values to determine how close the flashlight is to both of them? If so, they you can not only control the left and right arrow keys, but also the up arrow! And if you can do that, then you can try this controller with the Retro Racer game in Pip's Arcade!

When you are finished, click NEXT.

Step 12: You Finished!

Click EXIT to return to the menu and start your next coding challenge.