20
Introduction to Programming MVRT 115

Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

Embed Size (px)

Citation preview

Page 1: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

Introduction to Programming

MVRT 115

Page 2: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

• This is an introduction to programming in the C language.• We will be working on Vex and NXT robots, and later on• the large robot.• In this class, we assume that you know little ornothing about programming in general.• If you have programmed in C, C++, or Java, orif you were programming on the team last year,there will be a different class for you.

– In the advanced class, we will be working on the sensors and camera.

• We will talk about style, efficiency, testing and debugging.

Page 3: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

Just the Basics• A computer is an electronic machine that can do a

limited number of things, like adding, subtracting, multiplying, dividing, or comparing two numbers.

• A computer can input numbers from keyboards, disk drives, and sensors. The computer can output numbers to a computer screen, a disk, a printer, or a motor.

• Input & output is collectively referred to as I/O.• Computer instructions are done in sequence, one

after the other until the last instruction is executed; then the computer stops.

• A set of computer instructions is known as a program.• Numbers can be stored (& retrieved) from a

computer’s memory.• We say a program runs or executes. We can perform

or execute an instruction

Page 4: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

Programming Languges

• C

• C++

• Java

Page 5: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

Programming Languages• Programming in machine language is tedious &

error-prone for humans.• Computer memory locations are numbers. E.g.,

the first 256 bytes of memory are locations 0 through 255. (Everything on computers starts counting at zero.)

• For example, to add two numbers in memory locations 150 and 200:– Get the first number from memory location 150– Get the second number from memory location 200– Add the two numbers together– Store the addition result in memory (maybe location

204)

Page 6: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

Programming Languages

• Programming languages, like C & Java, make things easier: adding two numbers can be done in one line of C, and you don’t have to know memory locations!, e.g., sum = a + b;

• * A bit is a single binary digit: either 0 or 1.

• A byte is a set of 8 binary digits and can represent 256 different values.

Page 7: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

The Most Important Thing• The most important thing to know about programming

is that computers do what you tell them to do.• Not what you you’d like them to do.• Not what you think they should do.• You must know how to translate what you want into a

form the computer can use.• The syntax must be correct.• The logic must be correct.• When the computer does something “wrong,” it’s

almost certain that you programmed it to do the “wrong” thing.

Page 8: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

Basic Programming Form

• Every program must contain zero or more statements enclosed in curly brackets “{“ and “}”

• Every statement must end with a semicolon ;• A program is a series of statements• Statements are executed in order until we

run out of statements. But…– You can skip statements with “if” statements– You can repeat statements with loops

• You can block groups of statements with “{“ and “}” This is useful for “if” and “while”

Page 9: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

Syntax

• Syntax just means the way a statement is put together.

• Computer and human languages must have correct syntax, and every language has its own syntax .

• You understand “the dog my homework ate,” although it’s syntax is wrong for English. (However, the syntax would be right for German: “Der Hund meine Aufgaben frass.”)

• Computers never understand incorrect syntax!

Page 10: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

A C Programmain()

{ // a “//” is a comment

int x = 3; // x is a variable

int y, z; // y & z are variables

x = x + 2; // assignment statement

if (x < 6) // if statement

{

y = 2; // more assignment statements

z = 5;

}

}

Page 11: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

Variables• How does a computer get a program?

– You load it into memory

• You store programs and information (data) in memory. You don’t (usually) need to know where the program is in memory; the computer knows where to look.

• For example, your program might start at memory locations (the addresses of bytes of memory) 100 to 200 and your data at memory addresses 201 to 275.

• It’s difficult for humans to deal with numbered memory addresses, so you can name them. Named addresses are called variables.

• You must declare (name) a variable before you can use it.• int x; names a variable x. Where is it in memory? Who cares; you

can just call it x.

Page 12: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

Assignment

• How do you put values into a variable? With an assignment statement.

• x = 3; // put a 3 in variable x• The “=“ sign means put whatever is on

the right side into the left side. The right side value is not changed.

• The right hand side of the “=“ can be an expression.

• x = y *3 + 48;

Page 13: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

IF statement

Sometimes you want to do something only in certain conditions, so you can use an “if” statement. If the expression inside the parentheses is true, then you do the statements between the “{“ and “}” otherwise you skip them.

if (motor_speed > 255)

{

motor_speed = 255;

}

Page 14: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

• In the line-following example, you were introduced to if statements

• The elementary form is if (condition){… statements}• The statements are performed if

“condition” is true, otherwise the statements between { and } are skipped.

Page 15: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

• Defining philosophical truth is difficult; Cprogramming, however, truth is easy to

define.– A condition is true if it evaluates to non-zero.– A condition is false if it evaluates to zero

• This can take a little getting used to, butbasically all C expressions must evaluateto a number

Page 16: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

• Let’s look at some conditionsif (1)if (-1)if (0)if (x > 3)if ((x < 3 * v && r < 1) || 1)Because expressions evaluate to a number, you can

(but shouldn’t) do some weird things: j = h > q; // if h > q then assign 1 to j, else assign 0x = !x; // if x ≠ 0 assign 0 to x, else assign 1 x = !(!x);

Page 17: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

• Looking at the line-follower code, each of the 3 “if” statements can be true or false and may be executed in any combination

Page 18: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

• But if you want to test for exclusive conditions, use “else” clauses if (condition 1) // Order is important for logic and efficiency

{...}else if (condition 2){…}else if (condition 47){…}else{… // come here when all the other conditions are false}

Page 19: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

Switch Statement• If you have many conditions, consider using a

switch (case) construct #define FORWARD 483 switch (condition)

{case 1: // must be number or constant expression…break; // if you leave out break, the next case

executed: BAD!case FORWARD:…break;default:…}

Page 20: Introduction to Programming MVRT 115. This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on

Training DaysMondays: EngineeringWednesday: ElectricalThursdays: Media - We will also be working on the robot to prepare for

CAL games on Thursday – This is an open build meeting - all members are encouraged to come and work with veteran members to prepare for competitions – we will let you know each week if we are meeting on Thursday – This week we are not.

Fridays: Mechanical