While Loops and If-Else Structures

Slides:



Advertisements
Similar presentations
3-1 Chapter 3 Flow of Control (part a - branching)
Advertisements

ROBOTC for CORTEX While Loops Part 1
Variables and Functions ROBOTC Software. Variables A variable is a space in your robots memory where data can be stored, including whole numbers, decimal.
Decision Making EE2372 Software Design I Dr. Gerardo Rosiles.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Automation and Robotics
VEX and Robot C Chris Patterson Presented by Modified by J. Andazola.
Autonomy using Encoders Intro to Robotics. Goal Our new task is to navigate a labyrinth. But this time we will NOT use motor commands in conjunction with.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Introduction to Sensors
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.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
Testbed: Exercises.
ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex.
ROBOTC for VEX On-Site Professional Development
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Programming Design ROBOTC Software Principles of Engineering
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
Wall Encounter By Made easy by Dwayne Abuel.
AUTOMATION WITH ROBOTC Starting to write and troubleshoot computer code.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
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.
Variables and Functions ROBOTC Software. Variables A variable is a space in your robots memory where data can be stored, including whole numbers, decimal.
Mindstorm NXT-G Introduction Towson University Robotics.
VEX and Robot C Chris Patterson Frisco ISD CTE Center Presented by.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Automation and Robotics.  First you select the platform type so that you can use Natural Language PLTW.
1 ©2006 INSciTE Lab Three Task: Move forward for 2 feet, turn right 90º repeat to complete a square path. End up exactly where you started.
Programming Design ROBOTC Software. Behavior-Based Programming A behavior is anything your robot does –Turning on a single motor or servo Three main types.
Robotics Programming Wall Follow Line tracking for a set amount of time Line tracking for a distance.
Decision Making: while loops and Boolean Logic. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as.
Sensor Information: while loops and Boolean Logic.
ROBOTC for CORTEX Teacher Training © 2011 Project Lead The Way, Inc. Automation and Robotics VEX.
Variables and Functions
Variables and Functions
ROBOTC for VEX On-Site Professional Development
Robotics Programming Using Shaft Encoders
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Automation and Robotics
Variables and Functions
Movement using Shaft Encoders
Using Encoders to go Straight
Programming – Touch Sensors
Variables and Functions
Variables and Functions
Variables and Functions
Variables and Functions
Automation and Robotics
While Loops and If-Else Structures
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Variables and Functions
Auto Straightening using VEX Shaft Encoders
While Loops and If-Else Structures
While Loops and If-Else Structures
While Loops and If-Else Structures
While Loops and If-Else Structures
RobotC While loop When and how to use the while loop
if-else Structures Principles of Engineering
Automation with RobotC
Robotics Programming Using Shaft Encoders
Robotics Programming Using Shaft Encoders
While Loops and If-Else Structures
Automation with RobotC
Robotics Programming Using Shaft Encoders
While Loops and If-Else Structures
While Loops And If-Else Structures
Presentation transcript:

While Loops and If-Else Structures ROBOTC Software

While Loops While loop is a structure within ROBOTC Presentation Name Course Name Unit # – Lesson #.# – Lesson Name While Loops While loop is a structure within ROBOTC Allows a section of code to be repeated as long as a certain condition remains true Three main parts to every while loop The word “while” The condition Commands to be repeated

1. The Word While Every while loop begins with the keyword while

2. The Condition Condition controls how long or how many times a while loop repeats When condition is true, the while loop repeats When condition is false, the while loop ends and the remainder of the program executes Condition is checked once every time loop repeats before commands between curly braces are run

3. Commands To Be Repeated Commands between curly braces will repeat while condition is true Program checks at the beginning of each pass through the loop

Boolean Logic Program decisions are always based on questions Only two possible answers yes or no true or false Statements that can be only true or false are called Boolean statements Their true-or-false value is called a truth value.

Boolean Logic

Boolean Logic

Writing a condition: Example While the bump switch is not pressed: wait until it’s dark, then turn on light; wait until it’s light, then turn off light

While loop: more flexible than an “until” statement In this code, a motor runs until an object is within 50 cm. The program can’t respond to an emergency shutoff switch. The program can’t control other outputs in response to other inputs. Program waits here until an object is near.

While loop: more flexible than an “until” statement Presentation Name Course Name Unit # – Lesson #.# – Lesson Name A while loop can do the same thing as the “until” statement. Example code using until statement: Program waits here until an object is near. While loop can do the same thing: Program loops here until an object is near.

While loop is more flexible than an “until” statement Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Other conditions can be added to the while condition, e.g. an emergency shutoff. Other code can be executed in the while loop. Can expand the condition Later, in slide #22, we’ll see how to make the “and” from the && operator. Can control other outputs inside this bracket.

While loop is more flexible than an “until” statement Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Example equivalent to “until”: Can expand the condition Can control other outputs inside this bracket. Example using this flexibility: && means “AND” In the top example, the leftMotor starts and then continues until 50<=sonar. In the bottom example, the leftMotor start and continues until 50<=sonar<70, and meanwhile, the potentiometer controls the rightMotor speed. Note: Potentiometer ranges from 0 to 4095, so dividing by 400 creates a number within the range required for the motor speed. Note: After the loop exits, the rightMotor will continue running at its last assigned speed, until reassigned or until the program ends. Note: Compound inequalities are correct math language, but not correct RobotC: 50<SensorValue(sonar)<70 is not an acceptable syntax for a condition. Use the &&. range from 0 to 100

Timers Loop control Solution: Timers Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Timers Loop control Where would the wait statement go if we wanted the loop to repeat for a controlled amount of time? Nowhere! We need something else. Solution: Timers Internal stopwatches (4 available) Like encoders, timers should be cleared before they are used Be careful: don’t clear a timer in a timed loop Clearing timers within a loop may continually reset the timers which may create an infinite loop.

Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Timers Timer T1 is used as the condition for the while loop, which will run for 30 seconds More information available at the Cortex Video Trainer Resource. Click Remote Control then Timers Videos.

If Statements If statement in the program is evaluated by condition contained in parentheses If condition is true, commands between braces are run If condition is false, those commands are ignored Very similar to how a while loop works, but does not repeat the code

If-Else Statements If-else statement is an expansion of if statement If checks condition and runs appropriate commands when it evaluates to true Else allows code to run when condition is false Either if or else branch is always run once

Multiple If-Else Statements Be careful when using two separate if-else statements, particularly if both are used to control the same mechanism One branch of each if-else statement is always run so that you may create a scenario where the two statements ‘fight’ one another

Multiple If-Else Statements In this example, if one of the touch sensors is pressed, the rightMotor will be turned on in one if-else statement and immediately turned off in the other

Multiple If-Else Statements This can be corrected by embedding the second if-else within the else branch of the first if-else. ,The second condition is only checked if the first condition is false.

Nested if-else statements: else if An else {if else} statement can also be represented as an else if - else

Using a range of values in a condition Two strategies will work: Boolean logic Nested if-else statements Example: Task: Control motor with potentiometer “knob”: Potentiometer Value Motor Speed 0-500 501-1000 63 1001-4095 127

Using a range of values in a condition Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Using a range of values in a condition Strategy #1: Boolean logic Potentiometer Value Motor Speed 0-500 501-1000 63 1001-4095 127 Boolean operator RobotC symbol AND && OR || True only if the sensor value is more than 500 AND less than 1000 . The AND symbol is a double ampersand, found as the “uppercase” version of the 7 on most keyboards. The OR symbol is typed using two of the “pipe” characters in a row. The “pipe” character can be found on most keyboards above the return/enter key, as the “uppercase” version of the backslash. Note: Since the potentiometer (nicknamed “knob” here using sensor and motor setup) has to be between 0 and 4095, there was no need to use a && in the first and third if statements.

Using a range of values in a condition Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Using a range of values in a condition Strategy #1: Boolean logic. In this example, this strategy wastes time and processor power. The next strategy is better… Four comparisons waste time here each loop. The AND symbol is a double ampersand, found as the “uppercase” version of the 7 on most keyboards. The OR symbol is typed using two of the “pipe” characters in a row. The “pipe” character can be found on most keyboards above the return/enter key, as the “uppercase” version of the backslash. Note: Since the potentiometer (nicknamed “knob” here using sensor and motor setup) has to be between 0 and 4095, there was no need to use a && in the first and third if statements.

Using a range of values in a condition Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Using a range of values in a condition Strategy #2: Nested if-else preferable in this example. In this case, the false value of the first condition can be used again by nesting a 2nd if statement inside the first else. Potentiometer Value Motor Speed 0-500 501-1000 63 1001-4095 127 The AND symbol is a double ampersand, found as the “uppercase” version of the 7 on most keyboards. The OR symbol is typed using two of the “pipe” characters in a row. The “pipe” character can be found on most keyboards above the return/enter key, as the “uppercase” version of the backslash. Note: Since the potentiometer (nicknamed “knob” here using sensor and motor setup) has to be between 0 and 4095, there was no need to use a && in the first and third if statements.

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