Variables and Functions. ROBOTC Software

Similar documents
Movement using Shaft Encoders

Activity Variables and Functions VEX

G. Tardiani RoboCup Rescue. EV3 Workshop Part 1 Introduction to RobotC

Computer Programming : C++

Troubleshooting ROBOTC with Cortex

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

BTE2313. Chapter 2: Introduction to C++ Programming

Visual C# Instructor s Manual Table of Contents

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CpSc 1111 Lab 4 Formatting and Flow Control

COMP 202 Java in one week

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Activity Basic Inputs Programming - VEX Clawbot

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Ex: If you use a program to record sales, you will want to remember data:

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

6.096 Introduction to C++

BASIC ELEMENTS OF A COMPUTER PROGRAM

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Lab # 02. Basic Elements of C++ _ Part1

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Activity Basic Inputs Programming VEX

Activity Basic Inputs Programming VEX

The C++ Language. Arizona State University 1

2. Within your four student group, form a two student team known as Team A and a two student team known as Team B.

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Basics of Java Programming

Lecture 2 Tao Wang 1

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

COMP 202 Java in one week

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

CSc Introduction to Computing

Programming in ROBOTC ROBOTC Rules

CS102: Variables and Expressions

Variables. Data Types.

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

AP Computer Science A

COMP 202. Java in one week

LECTURE 02 INTRODUCTION TO C++

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CpSc 1111 Lab 5 Formatting and Flow Control

Lec 3. Compilers, Debugging, Hello World, and Variables

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Text Input and Conditionals

Exercise: Inventing Language

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Java Module Lesson 2A Practice Exercise

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CS2141 Software Development using C/C++ C++ Basics

Programming in C++ PART 2

CS50 Supersection (for those less comfortable)

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Technical Questions. Q 1) What are the key features in C programming language?

You ll be reading more about tools for branching in Sections 3.4, and We are skipping Sections 3.5, 3.11 and 3.13-end of chapter.

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Objectives. In this chapter, you will:

ARG! Language Reference Manual

Working with JavaScript

CSI33 Data Structures

Chapter 2: Using Data

Course Coding Standards

IT 374 C# and Applications/ IT695 C# Data Structures

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

If you note any errors, typos, etc. with this manual or our software libraries, let us know at

UNIT 1 مدخالت معالجة مخرجات

A First Program - Greeting.cpp

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

C++ Support Classes (Data and Variables)

Presented By : Gaurav Juneja

Activity Basic Inputs Programming Answer Key (VEX)

Program Fundamentals

CS313D: ADVANCED PROGRAMMING LANGUAGE

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

CODAPPS. Coding Cheatsheet. Clément Levallois. Version 1.0, last modified

Mr. Monroe s Guide to Mastering Java Syntax

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Overview. Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays. Initialization Searching

Chapter 2. C++ Basics

Boolean Algebra Boolean Algebra

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Fundamental of Programming (C)

Introduction to Programming. Writing an Arduino Program

Variables and Constants

Transcription:

Variables and Functions ROBOTC Software

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

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

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

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 Initialization Assignment Assignment

Assigning a Value to a Variable The left-hand side of the assignment operator must be a variable. Correct: Incorrect:

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 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 This loop will run five times, with a=0,1,2,3,4 Increment

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

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.

Variable Application #4: Remember a maximum Remember the new max! Similar to the flag, but the variable remembers an int instead of a bool.

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 Remembers the most recent code Counts the # of times loop is executed

Variable Application #5: Debug a program Activate global variables tab in the debug window. 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 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 1. Function header (name of function) 2. Function definition (code in the function) 3. 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