Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Vex Robotics Introduction to Sensors. introduction to sensors Sensors assist robots in seeing and feeling the physical world through which they travel.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Programming Concepts (Part A) Ping Hsu. What is a program? WHITE CAKE RECIPE 1.Preheat oven to 350 degrees F (175 degrees C). 2.Grease and flour a 9x9.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
C++ for Engineers and Scientists Third Edition
Introduction to C Programming
Introduction to Sensors
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
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.
Program ultrasonic range sensor in autonomous mode
DAT602 Database Application Development Lecture 5 JAVA Review.
Programming Concepts (Part B) ENGR 10 Introduction to Engineering 1 Hsu/Youssefi.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Fundamental Programming: Fundamental Programming Introduction to C++
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Variables and Functions ROBOTC Software © 2012 Project Lead The Way, Inc.Principles of Engineering.
Vex Robotics Program four: reversing and turning.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CPS120: Introduction to Computer Science Decision Making in Programs.
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Vex Robotics Program Two: Using two motors. Program two: using the motors In the last section, you learned how to turn on one motor. Now, you will take.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Vex Robotics program three: using motors and sensors together.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Mechanical Components and Programming Ken Youssefi Introduction to Engineering – E10 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Introduction to Programming in RobotC
Programming Concepts (Part B) ENGR 10 Introduction to Engineering
REPETITION CONTROL STRUCTURE
The Selection Structure
Chapter 4 – Control Structures Part 1
Programming Concepts (Part B) ENGR 10 Introduction to Engineering
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
While Loops and If-Else Structures
Programming Concepts (Part B) ENGR 10 Introduction to Engineering
Presentation transcript:

Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)

Ken Youssefi/Ping HsuIntroduction to Engineering – E10 2 What is Computer Programming ? Programming is breaking a task into small steps As a computer programmer, your job is to instruct a computer to do things One thing that you will realize quickly is that a computer is very dumb, but obedient. It does exactly what you tell it to do, which is not always what you wanted.

A Math Example Ken Youssefi/Ping HsuIntroduction to Engineering – E10 3 y = [ 3 × ( a × a + 7) ] / b + 4 a2a2 a × (a 2 + 7) = 3a (3a ) / b (3a ) / b + 4

Consider the sequential execution of the above expressions to find the value of y: Ken Youssefi/Ping HsuIntroduction to Engineering – E10 4 p = a x a; q = p + 7; r = 3 x q; s = r / b; y = s + 4; y = [ 3 × ( a × a + 7) ] / b + 4

Ken Youssefi/Ping HsuIntroduction to Engineering – E10 5 What is a Programming Language? It is a languages that the computer understands. There are many different languages. The main choices, used by professional programmers, are: C, C++ and Java. Probably the most widely-used, it is the oldest, of the three languages. It's been in use since the 70s, and is a very compact, powerful and well-understood language. C

Ken Youssefi/Ping HsuIntroduction to Engineering – E10 6 What is a Programming Language? C++ This language includes pretty much all of C with more stuff added. Its main benefit is that it’s object oriented. Object oriented languages are more modern and are used now in the programming world. C++ is the choice for most commercial application software developed on Windows and Macs. Object Oriented is a modern programming concept where the programmer creates "objects" like real- life objects, with both properties and abilities.

Ken Youssefi/Ping HsuIntroduction to Engineering – E10 7 Java runs on more than one platform without needing to be recompiled. Java runs inside web browsers, letting programmers create little applications which can run on web sites. But Java is slower. Java What is a Programming Language? EasyC (Vex Robot) Over time, special purpose languages have been developed for specific applications - to make the programmers job easier. EasyC is a C++-based language specifically for telling the Vex robot what to do.

Application specific programming language based on C++ Ken Youssefi/Ping HsuIntroduction to Engineering – E10 8 Open a new file

EasyCPro – old version (PIC) Ken Youssefi/Ping HsuIntroduction to Engineering – E10 9 Function blocks Flow chart Program code Your program is inserted here

Ken Youssefi/Ping HsuIntroduction to Engineering – E10 10 EasyCcortex – new version Open a New Standslone Project and select Autonomous Only Project

Ken Youssefi/Ping HsuIntroduction to Engineering – E10 11 Function blocks Flow chart Program code EasyCcortex - new version Your program is inserted here Use the Window option to configure the appearance of the screen

Variables Ken Youssefi/Ping HsuIntroduction to Engineering – E10 12 A semicolon is required at the end of each instruction. A “variable” is a place where we store a value. A = 10 ; // The value 10 is stored in A. Any statement after the sign “//” is NOT part of the program. It is a comment made by the programmer. Value of 10 is placed in the storage location called “A”. So “A” is a variable

Variables Ken Youssefi/Ping HsuIntroduction to Engineering – E10 13 Every variable in the program needs to be declared in the beginning of the program. Declaration of the variable tells the program its name and its type int speed ; The word “int” indicates that the variable ‘speed’ is an integer variable.

Commonly Used Variable Types Ken Youssefi/Ping HsuIntroduction to Engineering – E10 14 Variable Type DescriptionRange Int Stores integer values EX: 5, ,768 to +32,767 Long Stores integer values with extended range EX: 56, 6000, -4,234,128 -2,147,483,648 to +2,147,483,647 float Stores values with decimal point EX: 1.245, [-10^+38, -10^-38] 0 [10^-38, 10^+38] char Stores characters* EX: A, # _

Variables Ken Youssefi/Ping HsuIntroduction to Engineering – E10 15 This variable is accessible from anywhere within your program. Global Variable This variable is only accessible from a “local area” within your program (“functions” will be discussed later). Local Variable

Global Variable Declaration Ken Youssefi/Ping HsuIntroduction to Engineering – E10 16 To declare a global variable, right click on the tab “Global” in the Main program and select Edit Block (or double click the block) Insert variable name Select variable type Starting value of the variable Cortex (new controller )

Example of Assigning Variables Ken Youssefi/Ping HsuIntroduction to Engineering – E10 17

Assignment Operator Ken Youssefi/Ping HsuIntroduction to Engineering – E10 18 In C language “ = ” is an assignment operator, not an “ is equal to ” statement. A = A+1; //means assign the value A+1 // back to A, i.e., increase A by 1. A = 10; // means assign 10 to A. A = B; // means assign the value of B to A This == symbol is used for checking ‘equal’ condition, not for value assignment.

Assignment Operator: Examples: Ken Youssefi/Ping HsuIntroduction to Engineering – E10 19 int A; // a variable named A of type integer int B; // a variable named B of type integer A = 10; // value 10 is assigned to variable A B = (24+16)/2; // 20 is assigned to variable B A = A + 15; // value of (A+15) is first evaluated and then assigned to A. So now A=(10+15)=25 B = A + B ; A = B – 40; // Now A=(45-40)=5, B=45 A = A * B; // Now A=(5*45)=225, B=45 B = A / 9; // Now A=225, B=25 // Now A = 25, B = (25+20)=45

Clicker Question Ken Youssefi/Ping HsuIntroduction to Engineering – E What is the value of B at the end of this program? int A; int B; A = 12; B = 15; A = A + (B/3) ; B = A + B – 7 ; (A)12, (B)15, (C)17, (D)20, (E)25

Decision Making Ken Youssefi/Ping HsuIntroduction to Engineering – E10 21 A linear (sequential) execution of instructions can only perform a simple task that does not involve decision making. The IF instruction gives a C program decision making ability The ability to make decision is the most basic form of intelligence.

IF Statement (logic statement) Ken Youssefi/Ping HsuIntroduction to Engineering – E10 22 if (Number == 0) { PrintToScreen (“The Number is Zero”); } if (Number < 0) { PrintToScreen (“The Number is negative”); } Example Only if this condition is true, this instruction is executed.

IF Statement Ken Youssefi/Ping HsuIntroduction to Engineering – E10 23 Select, drag and drop the IF statement into the flow chart

IF-ELSE Statement Ken Youssefi/Ping HsuIntroduction to Engineering – E10 24 IF-ELSE statement should be used where there are only two possible cases. If (score <60) { PrintToScreen(“You failed the test”);} else { PrintToScreen(“You passed the test”);}

EasyCPro - Example Ken Youssefi/Ping HsuIntroduction to Engineering – E10 25 Drag and drop the IF module, the Print To Screen, and Else module into the flow chart

Clicker question Ken Youssefi/Ping HsuIntroduction to Engineering – E What is the value of A and B at end of this program? (A) A=12, B=21 (B) A=9, B=12 (C) A= 9, B=9 (D) A=12, B=12 (E) A=12, B=9 A = 9 ; B = 12 ; if ((A + B) > 22) { A = B ; B = A; } else { B = A; A = B; }

WHILE Statement Ken Youssefi/Ping HsuIntroduction to Engineering – E10 27 The WHILE statement is useful for repeating a set of instructions Suppose we have to add the first 50 positive integers …………………… You could use a single statement: int SUM ; // integer variable for the result SUM = ……………… ;

Ken Youssefi/Ping HsuIntroduction to Engineering – E10 28 How to Add a User Code Enter the expression to be executed Drag and drop User Code

Ken Youssefi/Ping HsuIntroduction to Engineering – E10 29 Much better approach is To use “While” statement This block of instructions is executed repeatedly until the condition is not true. This condition is checked first. If it is true, the block of instructions enclosed by the curly brackets { } is executed. Initialize the variables SUM and COUNTER

Clicker Question Ken Youssefi/Ping HsuIntroduction to Engineering – E What is the final value of A ? int A; int i; A = 0; i = 0; while (i < 3) { A = A + i; i = i + 1; } (A)2 (B)3 (C)4 (D)6 (E)10

Solution to Clicker question 3 Ken Youssefi/Ping HsuIntroduction to Engineering – E10 31 Initially i = 0, A = 0. First iteration: condition 0<3 is true A = 0+0=0, i = 1 Second iteration : condition 1<3 is true A = 0+1=1, i = 2 Third iteration : condition 2<3 is true So A = 1+2=3, i = 3 Fourth iteration : condition 3<3 is false So the WHILE loop stops. Final Value of A is 3. int A; int i; A = 0; i = 0; while (i < 3) { A = A + i; i = i + 1; }

Finite and Infinite Loop Ken Youssefi/Ping HsuIntroduction to Engineering – E10 32 In the previous examples we have employed condition checking in order to control the flow of execution. – We have made the loop to repeat only a finite number of times. We can also make the loop to repeat infinitely The infinite loop is necessary to continuously check the signal coming from the sensor.

Ken Youssefi/Ping HsuIntroduction to Engineering – E10 33 Write a short program so that the robot moves forward as long as it has not encounter any obstacles (bumper sensor is not pressed), and it stops when bumper is activated (hit an obstruction). Most commonly used logic statements: IF statement IF – ELSE statement (for two cases only) ELSE – IF statement (for more than two cases) WHILE loop (finite or infinite) statement In Summary

Setting up the program for the Sensors Ken Youssefi/Ping HsuIntroduction to Engineering – E10 34 Double click the Variable block to create a new “int” variable named “bumper” Declare the variable Bumper

Setting up the program for the Sensors Ken Youssefi/Ping HsuIntroduction to Engineering – E10 35 Select, drag and drop the While Loop icon between the BEGIN and End In the config. window set the condition of the while loop to be 1==1, this will force the while loop to loop for an infinite amount of time

Setting up the program for the Sensors Ken Youssefi/Ping HsuIntroduction to Engineering – E10 36 Select, drag and drop the “Bumper Switch” icon into the program in the WHILE loop

Setting up the program for the Sensors Ken Youssefi/Ping HsuIntroduction to Engineering – E10 37 Set the Digital Input # to the port that the sensor is plugged in This is an infinite loop to check the signal from the bumper. If the bumper is pushed its value will change to 0 Select Bumper from the list of variables

Integrating Motors with Sensors Ken Youssefi/Ping HsuIntroduction to Engineering – E10 38 Drag and drop an IF and ELSE icons from the Program Flow into the program below the sensor Set the condition for the IF statement, bumper == 1, no obstacle.

Integrating Motors with Sensors Ken Youssefi/Ping HsuIntroduction to Engineering – E10 39 Your robot will be using two motors. Drag and drop two motors in the IF loop and two motors in the ELSE loop. Set the motor ports and speeds. Go forward Stop With this program, the robot moves forward as long as the bumper is not pressed (value=1). Robot will stop if it hits an obstacle (value=0)

Ken Youssefi/Ping HsuIntroduction to Engineering – E10 40 Bumper is not pressed Bumper is pressed