Variables and Functions

Slides:



Advertisements
Similar presentations
Variables and Functions ROBOTC Software. Variables A variable is a space in your robots memory where data can be stored, including whole numbers, decimal.
Advertisements

While Loops and If-Else Structures
08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Team 5220 Roboknights. Outline  Getting Started  Hardware Setup/Wiring  Software Setup/Pragmas  Programming with RobotC  Grammar/Syntax  Basic Statements.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Introduction to C Programming
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Testbed: Exercises.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CPS120: Introduction to Computer Science
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Variables and Functions ROBOTC Software © 2012 Project Lead The Way, Inc.Principles of Engineering.
While and If-Else Loops ROBOTC Software. While Loops While loop is a structure within ROBOTC Allows a section of code to be repeated as long as a certain.
CPS120: Introduction to Computer Science Variables and Constants.
Variables and Functions ROBOTC Software. Variables A variable is a space in your robots memory where data can be stored, including whole numbers, decimal.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Vex Robotics program three: using motors and sensors together.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Variables. A variable is a space in your robot’s memory where you can store data, such as whole numbers, decimal numbers, and words. Variable names follow.
Bill Tucker Austin Community College COSC 1315
Introduction to Programming in RobotC
Variables and Functions
A variable is a name for a value stored in memory.
Variables and Functions
Variables and Functions
Chapter 4 C Program Control Part I
EGR 2261 Unit 4 Control Structures I: Selection
Multiple variables can be created in one declaration
Variables, Expressions, and IO
Programming Design ROBOTC Software Computer Integrated Manufacturing
Variables and Functions
Movement using Shaft Encoders
Using Encoders to go Straight
Programming – Touch Sensors
Variables and Functions
Variables and Arithmetic Operations
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Variables and Functions
Variables and Functions
Automation and Robotics
While Loops and If-Else Structures
Introduction to C++ Programming
Chapter 8 JavaScript: Control Statements, Part 2
Variables and Functions
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
CMSC 202 Java Primer 2.
While Loops and If-Else Structures
While Loops and If-Else Structures
While Loops and If-Else Structures
While Loops and If-Else Structures
if-else Structures Principles of Engineering
Automation with RobotC
Robotics Programming Using Shaft Encoders
While Loops and If-Else Structures
Primitive Types and Expressions
Automation with RobotC
While Loops and If-Else Structures
Programming Design ROBOTC Software Principles of Engineering
While Loops And If-Else Structures
Variables and Constants
Boolean Expressions September 1, 2019 ICS102: The course.
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Variables and Functions ROBOTC Software

Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variables A variable is a space in your robots memory where data can be stored, including whole numbers, decimal numbers, and words Variable names follow the same rules as custom motor and sensor names: capitalization, spelling, availability Variables can improve the readability and expandability of your programs

Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Creating a Variable Declare the variable (stating its type and its name) once at the beginning of task main: Type of data: int float Name of variable: Starts with letter Letters, numbers, and underscores are ok Not a reserved word The name of a variable should be descriptive. Use words! When using multiple words, most programmers start with lowercase and then capitalize the first letter of each additional word. This is affectionately known as “drinking camel” notation: first letter lowercase id the head down drinking, then the later uppercase letters are the humps. Example: numberOfWords = 3;

Variable Types Data Type Description Example Code Integer Positive and negative whole numbers, as well as zero -35, -1, 0, 33, 100 int Floating Point Number Numeric values with decimal points (even if the decimal part is zero) -.123, 0.56, 3.0, 1000.07 float Boolean True or false – Useful for expressing the outcomes of comparisons true, false bool Character Individual characters, placed in single quotes. Not useful with POE kits. ‘L’, ‘f’, ‘8’ char String Strings of characters, such as words and sentences placed in double quotes. Not useful with POE kits. “Hello World!”, “asdf” string

Creating a Variable Initialize the variable by giving it its initial value: Declaration and initialization are often contracted into a single statement:

Using the Value of a Variable Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Using the Value of a Variable The value stored in the variable can be referenced by using the variable’s name anywhere the value of the variable could be used. This does not change the value of the variable. Only referenced when this line executed; in this example, if “a” changes later, it won’t automatically update the motor speed. When this command executes, the leftMotor’s speed will be set to the opposite of the value of the variable speed. Note the asterix is used for multiplication.

Assigning a Value to a Variable Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Assigning a Value to a Variable The assignment operator is the single equal sign The right-hand side of the equal sign is evaluated, and then the value is assigned to variable on the left-hand side This is not the equality from algebra! Declaration speed=speed+1 has “no solution” in algebra. The algebra equal sign is most similar to the conditional ==, but in algebra it is taken as a claim that it is true instead of a question asking whether it is true. Initialization Assignment Assignment

Assigning a Value to a Variable Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Assigning a Value to a Variable The left-hand side of the assignment operator must be a variable. Correct: Incorrect:

Variable Applications Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Applications Variables are needed for most programs. Here are some examples: Example #1: Repeat code 5 times Example #2: Count user’s button presses Example #3: Remember if the user EVER pushed a button Example #4: Remember a maximum value Example #5: Debug a program by remembering which branch of code has been executed.

Variable Application #1: Loop n times Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Application #1: Loop n times Task description: Start and stop a motor 5 times. Instead of writing the same code multiple times, use a variable to remember how many times the code has executed so far.

Variable Application #1: Loop n times Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Application #1: Loop n times Increment This loop will run five times, with count=0,1,2,3,4 The “Do something here” comment can be replaced by code that you want to run 5 times. An assignment that increments is so commonly used that there is shorthand way to write it: count=count+increment; is the same as count+=increment;

Variable Application #2: Count the user’s actions Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Application #2: Count the user’s actions Task description: Count the number of times a user does something. E.g., how many times did the user press the “increase volume” button on a remote? Use a variable to remember how many times the user performed that action so far.

Variable Application #2: Count the user’s actions Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Application #2: Count the user’s actions If the untilRelease is left out, nPresses will increment by 1 every 0.050 seconds. A typical quick press of the bumpSwitch might last 0.400 seconds! If the wait(0.05) is left out, nPresses might increment several times when the button is released. This is because “releasing” the bump switch actually makes it bounce back and forth between 1 and 0 several times as microscopic sparks jump across the small air gap as the button is first released. The variable nPresses remembers how many times the bump switch was pressed before the limit switch was pressed.

Variable Application #3: Remember whether an event ever happened. Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Application #3: Remember whether an event ever happened. Task description: Observe the user for 5 seconds and remember whether they EVER pressed a switch, even briefly. Use a variable to remember whether the event has happened yet. This is called a flag. Once the flag is thrown, it stays thrown until cleared.

Variable Application #3: Set a “flag” Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Application #3: Set a “flag” The variable touched remembers if the bump switch was EVER pushed. After this code, touched will be true, even if bump was pressed and released. A flag is “raised” (i.e., set to true) if a condition is *ever* met during a certain time period, even if it was *usually* not met. This snippet of code might be useful in a material sorter if the machine is checking for conductivity as a marble rolls by. Two 24- or 26-gauge wires, connected to where the black and white wires normally go in a digital input, can make a “conductivity sensor”.

Variable Application #4: Remember the maximum value Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Application #4: Remember the maximum value Task description: Observe a sensor for 5 seconds and remember its highest value. Use a variable to remember the biggest value that has occurred so far. This might be used with a scale as a truck drives over the scale. The heaviest value was when the entire truck was on the scale. It might also be used with a light sensor as an opaque object passes in front of a light. The dimmest value was when the object was blocking the light.

Variable Application #4: Remember a maximum Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Application #4: Remember a maximum Remember the new max! This snippet of code might be useful to remember the darkest value of a light sensor as an object passes in front of it. Similar to the flag, but the variable remembers an “int” instead of a “bool”.

Variable Application #4: Remember what has executed Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Application #4: Remember what has executed Run-time errors can be hard to figure out without knowing which parts of your program are being executed! Sometimes slowing down a program with the step debugger is impractical. Use a variable to remember (and report) what parts of your program executed.

Variable Application #5: Debug a program Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Remembers the most recent code Counts the # of times loop is executed By opening the “variables” tab of the debugger windows, your program can run and the value of “a” will indicate where the program most recently set the value of “a”.

Variable Application #5: Debug a program Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Activate “global variables” tab in the debug window. By opening the “variables” tab of the debugger windows, your program can run and the value of “a” will indicate where the program most recently set the value of “a”. Variable values reported here as program runs

Global vs. Local Variables Variables can have either a “global” or a “local” scope. Global variable Can be read or changed from any task or function in your code. Its value can be seen/read globally. Local variable Belongs only to the task or function in which it was created Value can only be read or changed from within that task or function Value can only be seen/read locally Generally the type of variable you’ll want to use, local to “main”

Creating Local Variables (preferred) To create a local variable, declare it within the curly braces of task main or one of your functions. You will only be able to change the value of this variable within its task or function.

Creating Global Variables To create a global variable, declare it after your pragma statements, but before task main or any function declarations. This will allow your variable to be changed by any task or function in your program.

Functions Functions Creating Functions Group together several lines of code Referenced many times in task main or in other functions Creating Functions Example: LED on if bumper is pressed, off if released Function header (name of function) Function definition (code in the function) Function call (where function code will run)

Sample Function “LEDControl()”

Function Declaration Function declarations (or prototypes) declare that a function exists and indicates its name Function declarations between #pragma statements and task main Function declaration optional if function definition is above task main

Function Definition Function definitions define the code that belongs to the function

Function Call Function calls Call and run code from function Placed in task main or other functions

References Carnegie Mellon Robotics Academy. (2011). ROBOTC. Retrieved from http://www.robotc.net