04Robotics - Variables and Functions

Embed Size (px)

Citation preview

  • 8/9/2019 04Robotics - Variables and Functions

    1/24

     Variables and Functions

  • 8/9/2019 04Robotics - Variables and Functions

    2/24

    Open your Encoder program

    Let’s begin by opening the “Labyrinth Auto

    Straight” code.

    Save this file as Labyrinth with variables.

  • 8/9/2019 04Robotics - Variables and Functions

    3/24

    Making changes takes too much time

    Suppose your auto straight program doesn’texactly work with the motor powers we

    selected.

    Change these as needed.

    But you need to change these

    everywhere. Is that practical?

  • 8/9/2019 04Robotics - Variables and Functions

    4/24

    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?

  • 8/9/2019 04Robotics - Variables and Functions

    5/24

     Variables

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

  • 8/9/2019 04Robotics - Variables and Functions

    6/24

     Variables

  • 8/9/2019 04Robotics - Variables and Functions

    7/24

     Variable Types

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

  • 8/9/2019 04Robotics - Variables and Functions

    8/24

  • 8/9/2019 04Robotics - Variables and Functions

    9/24

     Variable Types

    Referenced by “string” , these can be a combination of letters,

    numbers, and spaces.

  • 8/9/2019 04Robotics - Variables and Functions

    10/24

  • 8/9/2019 04Robotics - Variables and Functions

    11/24

  • 8/9/2019 04Robotics - Variables and Functions

    12/24

    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)

  • 8/9/2019 04Robotics - Variables and Functions

    13/24

  • 8/9/2019 04Robotics - Variables and Functions

    14/24

    Replace all motor speeds with integer “names”

    Now change all motor powers tothe appropriate variable name.

    Do this for the entire program.

    Then save, compile, download,

    and test.

  • 8/9/2019 04Robotics - Variables and Functions

    15/24

     Test

    The 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.

  • 8/9/2019 04Robotics - Variables and Functions

    16/24

    Behaviors and FUNCTIONS

  • 8/9/2019 04Robotics - Variables and Functions

    17/24

    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.

  • 8/9/2019 04Robotics - Variables and Functions

    18/24

     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.

  • 8/9/2019 04Robotics - Variables and Functions

    19/24

    Behaviors and FUNCTIONSOpen the file Labyrinth Auto Straight.

    Save this file as Labyrinth with functions.

    Declare the function and name it BEFORE task main.

  • 8/9/2019 04Robotics - Variables and Functions

    20/24

    Behaviors and FUNCTIONS

    Highlight the code starting with

    the FIRST IF statement and

    END with the last IF statementRight click and select copy.

    Make sure you DO NOT copy

    the while loop’s curly braces.

  • 8/9/2019 04Robotics - Variables and Functions

    21/24

    Behaviors and FUNCTIONS

    Then paste the code inside the

    functions curly braces.

  • 8/9/2019 04Robotics - Variables and Functions

    22/24

  • 8/9/2019 04Robotics - Variables and Functions

    23/24

    Behaviors and FUNCTIONSDelete all instances of this “highlighted” code in your program and replace

    each instance with the call.

  • 8/9/2019 04Robotics - Variables and Functions

    24/24

    Let’s test your understanding 

    1. Create new FUNCTIONS for your right and leftturns.

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

    it into the labyrinth.

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

    4. Be sure to place your NAME in the comments

    for grading.