variables and functions. open your encoder program let’s begin by opening the “labyrinth auto...

24
Variables and Functions

Upload: jesse-gray

Post on 03-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Variables and Functions

Page 2: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Open your Encoder program

Let’s begin by opening the “Labyrinth Auto Straight” code.

Save this file as Labyrinth with variables.

Page 3: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Making changes takes too much timeSuppose your auto straight program doesn’t

exactly work with the motor powers we selected.

Change these as needed.

But you need to change these everywhere. Is that practical?

Page 4: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Behaviors and Variables

What if we could change the power levels in ONE place and that would change the power levels in ALL the places at one time?

Page 5: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Variables

You can take a motor power value and STORE it inside of a variable for later use.

Page 6: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Variables

Page 7: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Variable Types

These can be whole numbers only and are referenced by “int”.

Page 8: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Variable Types

Unlike integers, these numbers are allowed to have decimal values and are referenced by “float”.

Page 9: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Variable Types

Referenced by “string” , these can be a combination of letters, numbers, and spaces.

Page 10: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Variable Types

Single items are called “characters” and are referenced by “char”.

Page 11: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Variable Types

Variables can even be simply true or false which use Boolean logic and referenced by “bool”.

Page 12: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Let’s use the “int” variable

When you name and “integer” type variable you cannot use:•Spaces•Characters(!)•Numbers(1)•Already reserved/recognized terms(int)

Page 13: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

The “int” variable

Here we place the integer just after the task main command. The name refers to how we will remember its function.After we name it, we then STORE the integer we want.

Page 14: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Replace all motor speeds with integer “names”

Now change all motor powers to the appropriate variable name. Do this for the entire program. Then save, compile, download, and test.

Page 15: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

TestThe idea behind this is that you can now

“change” the motor powers in ONE place instead of 12 places as you test. This is just one way to program efficiently.

Page 16: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Behaviors and FUNCTIONS

Page 17: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

FunctionsFunctions are pieces of code that come BEFORE task main. We use “void” to begin our functions. The function itself is then given a name, in this case moveStraight. There is a set of empty parenthesis after the name. Then the commands are placed inside the functions curly braces.

Page 18: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

The “call”Then, when you are ready to use the code, you simply “call” it from within task main.

So in summary, you first have to DECLARE the function and give it a name. Then you must CALL the function to use it.

Page 19: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Behaviors and FUNCTIONSOpen the file Labyrinth Auto Straight. Save this file as Labyrinth with functions.

Declare the function and name it BEFORE task main.

Page 20: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Behaviors and FUNCTIONS

Highlight the code starting with the FIRST IF statement and END with the last IF statement Right click and select copy.

Make sure you DO NOT copy the while loop’s curly braces.

Page 21: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Behaviors and FUNCTIONS

Then paste the code inside the functions curly braces.

Page 22: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Behaviors and FUNCTIONS

Then go back and delete the code you highlighted under task main. Replace the code with the CALL. Make sure the call is spelled and capitalized exactly as the function name. Also make sure there is a semicolon after the call as this is now a command or simple statement with the main program.

Page 23: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Behaviors and FUNCTIONSDelete all instances of this “highlighted” code in your program and replace each instance with the call.

Page 24: Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables

Let’s test your understanding1. Create new FUNCTIONS for your right and left

turns.

2. Compile and test the program to see if it makes it into the labyrinth.

3. If successful, use COMMENTS to EXPLAIN each line of code in YOUR own words.

4. Be sure to place your NAME in the comments for grading.