3 - Programming with Variables

Unit 3 - Programming with Variables

How do programs "remember?"

What Am I Learning and Why Learn This Now?

Think of a number. Add two. What's your new number? Simple right? People have "memory" - a way to hold information so that we can do something with that information. (OK, sometimes we just write it down...) Programs have "variables" and "constants" for this. "Variables" are designed for information that will change. "Constants" are for information that will not change. Sometimes variables and constants have a very small ("local") scope - they are only relevant for a specific task. Sometimes variables and constants have a larger ("global") scope - they are used in multiple parts of the program or multiple sub programs. Sometimes values have to be "passed" from one program, or part of a program, to another.

Being able to use variables in your programs will let you create more complex programs to handle more complex tasks, like the robot navigation task that comes later in this course.

When you have completed this unit, you will have "met" competency 3 - programming with variables. You will get additional practice and feedback on your journal for competency 1 - engineering process, but it will not be a summative grade.

Am I Ready to Learn This?

Before attempting this unit, you should have demonstrated mastery of Competency 1 - Intro Programming. This unit assumes you already know how to work in your programming environment and how to use basic control structures in your programming. You can do this unit before completing unit 2 - Gears, but you may need to modify the learning project.

Example Robotics Learning Project: Programming the Hill Climb Bot

Create the program for your Hill Climb Bot from the last unit. Before your bot begins, it needs to ask the user how to "celebrate" when it finishes. Your bot now needs a specific starting signal, not just "starting the program." It needs to stop "automatically" when it crosses the "finish line" at the top of the hill. Once it's stopped, it needs to display it's average velocity for the run in meters/second, and it's average rpm. (Note that average velocity is found by dividing distance by time.) Then it needs to do the "celebration" that the user chose before the run began.

Minimum Expectations for this project to be assessed:

  • Journal includes programming objectives

  • Journal includes at least three partial programs and their results, showing how you intentionally "worked up to" the final program

  • Program uses at least one variable and at least one constant, which are properly defined / set to initial values

  • Program meets all the requirements of the task

How to Navigate this Unit:

1. KNOW YOUR GOAL: READ the competency 4 rubric and minimum expectations. Ask clarifying questions as needed. Some of the terms may be unfamiliar to you at the start of the unit.

Work on the "Learn" parts in any way that meets your needs -- any order, or cycle among them.

2. LEARN: Investigate programming "variables" and "constants." How are they defined in your programming environment? Take notes as you explore. If you are working at an appropriate rigor, you should understand the differences and appropriate uses for the following types of variables:

  • Boolean

  • Numeric (Integer, Float)

  • String (Text, Varchar)

Some additional vocabulary / concepts you should be using as you learn:

  • variable vs constant

  • "name" of a variable vs "value" of a variable

  • "declaring" a variable

  • "scope" of a variable

  • Concatenation ("adding" string variables together)

  • Timers (concept is more specific to LEGO robotics than to many other programming environments)

  • "Wait" block/command

3. LEARN: If you are in the face-to-face class sessions, there will be on-pace learning opportunities:

  • Paper-based manipulative activity on constants vs variables names and values

  • Teacher-delivered content on variable types and scope

3. LEARN: Try these tasks, or similar ones. Don't forget to document in your journal - include your task, programming objective, shots of programs you tried, their results, and your thoughts about why you saw those results and how to change the program. Remember that designing your own appropriate tasks to scaffold your learning is a sign of good engineering process skills!

  • Create a program that defines a numeric constant, sets it to some initial value of your choosing, adds three, then displays the result on the robot's screen.

  • Create a program that displays "Hi " and your name on the robot screen. Try using two different string variables, one for the word, and one for your name. Can you concatenate them into one variable? How do you display longer text, like your first middle and last name?

  • Create a program that starts timing when the light level reaches a certain value, then stops timing when the light level drops below that value. Store the time in a variable, and have the program display the total amount of time on the RCX screen.

  • Create a program that "counts" how many times the user clicked a button/touch sensor within three seconds, then beeps that number of times.

  • Modify that last program. Let the user input an initial number. Multiply the number by 5. If the result is bigger than 50, have one output, and if it's less than 50, have a different output. ( * If you are aiming for a 4, try defining a function / subroutine to complete the calculation. See if you can pass the user's initial number to the function, then return the result to the main program. )

4. FORMAL PROGRESS CHECK: When you are ready, schedule a formal check in with your teacher. Show your understanding of types of variables, how they are defined in your programming environment, and how you complete mathematical and logical operations with them. Your teacher will help you check your understanding against the competency 3 rubric. Your teacher may suggest/assign other learning tasks and additional formal check-ins to help you. If you are aiming for a '4,' be sure your teacher knows.

5. ROBOTICS LEARNING PROJECT: When your teacher indicates that you are ready to begin, start your robotics learning project for this unit to deepen your understanding and develop your engineering skills. Strongly consider working with your teacher to tailor the project to your specific interests and programming environment. Check-in with your teacher (and classmates) for feedback as you work.

6. SUMMATIVE EVALUATION: Your completed robotics learning project is your evidence that you are ready for the summative. AFTER your teacher has accepted your demonstration of the project, schedule a time to take the summative. Your summative is how you show that meet the requirements of the competency 3, as given in the rubric. Your summative will require that you can create a program to accomplish a teacher-provided task that requires manipulation of variables, and that you can correctly predict/explain the behavior of a bot with a given program.