Download presentation
Presentation is loading. Please wait.
Published byChristian Antony Edwards Modified over 9 years ago
1
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Program Functions to Accept Values
2
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Program Functions to Accept Values Performance Objective: Using an ultrasonic range sensor and optical quad encoder, program a function that will accept a value of the ultrasonic range sensor, compare the value to the optical quad encoder value and adjust the encoder value to stop at a distance from the wall. Enabling Objectives: describe how functions pass values describe the benefit of passing values in functions create code to pass values to a function
3
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Program Functions to Accept Values You have already seen how functions can be used to run the same group of statements from multiple places within your program There is another technique that allows you to have greater code reusability
4
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Arguments and Parameters We can solve this problem by defining special variables in our function called arguments that will stand in for the real variables when we define the function We then tell the function which real variables to use in place of those arguments by passing the required variables as parameters
5
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Function Call We will begin by looking at how we need to modify our function definition to include arguments Here's an example of a function header and a function call: a. is the header b. is the function call a. Void encoder(int); b. encoder(a);
6
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Function Call The following example is why we may want to use this option Let us say we made four functions Drive forward, Drive backward, turn left, and turn right I can accomplish the same task by passing values to just one function
7
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Function Call So in our example the logic would work: Void drive(a) { If a = 1 then drive forward If a = 2 then drive backward If a = 3 then turn left If a = 4 then turn right }
8
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Function Call Of course the values we could pass would control the direction of motors In this lesson, you will be programming a robot to pass a value to a function that will compare and adjust a quad encoder The quad encoder will be compared and adjusted to the ultrasonic sensor
9
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Function We will start by creating a function that handles the ultrasonic sensor We will create the function However a difference will occur in our dialog box
10
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Receive Value Notice in our argument list we offered the variable “a” By setting up our function in this manner, we are allowing the function ultrasonic to receive a value stored in “a”
11
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Receive Value You will also notice that are mode/function has created a new tab titled “ultrasonic”
12
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Receive Value If we can now enter the code in the body of the ultrasonic sensor. The code will compare the value of “a”, which is passed from the autonomous function, to the value of the ultrasonic sensor. If the values do not mathematically align then we have to give the encoder a new value to synchronize the ultrasonic sensor to the encoder sensor. See flow chart on next slide. See lesson 3.2
13
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Vex Programmer II Function Logic
14
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Receive Value So in our code, we should see the function call passing a variable “a”
15
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Receive Value And we can add the majority of our code in the ultrasonic function/mode
16
Available at: www.etcurr.comLesson 5.2 – Program Functions to Accept Values Try it Using an ultrasonic range sensor and optical quad encoder, program a function that will accept a value of the ultrasonic range sensor, compare the value to the optical quad encoder value and adjust the encoder value to stop at a distance from the wall
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.