robotc for cortex while loops part 1. while loops a while loop is a structure within robotc which...

17
ROBOTC for CORTEX While Loops Part 1

Upload: laurel-stoner

Post on 31-Mar-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

ROBOTC for CORTEXWhile Loops Part 1

Page 2: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

While Loops

• A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a certain condition remains true.

• There are three main parts to every while loop.

Page 3: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

1. The word “while”

• Every while loop begins with the keyword “while”.

Page 4: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

2. The Condition• The condition controls how long or how many times a

while loop repeats. While the condition is true, the while loop repeats; when the condition is false, the while loop ends and the robot moves on in the program.

• The condition is checked every time the loop repeats, before the commands between the curly braces are run.

Page 5: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

3. Commands to be Repeated• Commands placed between the curly braces will repeat

while the (condition) is true when the program checks at the beginning of each pass through the loop.

Page 6: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

Boolean Logic

Decisions robots make must always based on questions which have only two possible answers: yes or no, true or false.

Statements that can be only true or false are called Boolean statements, and their true-or-false value is called a truth value.

Page 7: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

Boolean Logic

Page 8: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

Boolean Logic

Page 9: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

Boolean Logic

Page 10: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

Boolean Logic

Page 11: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

While Loop Exercises

• Example 1: Program the green LED to repeatedly turn on for 2 seconds, then off for 2 seconds, forever.

• Example 2: Expand the previous program to loop only while the limit switch is not pressed.

Page 12: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

While Loop Exercises

• Example 3: Program the right motor to spin forward until the potentiometer value is greater than 2048. Then program it to spin in reverse until the potentiometer value is less than 2048. Repeat while the limit switch isn’t pressed.

Page 13: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a
Page 14: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a
Page 15: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

Additional Information

Page 16: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

CMU Resources• ROBOTC.net: http://www.robotc.net/

– The ROBOTC Forum: http://www.robotc.net/forums/– Post your homework and questions here!

• ROBOTC PLTW Page: http:// www.robotc.net/pltw• VEX Cortex Video Trainer

– http://www.education.rec.ri.cmu.edu/products/teaching_robotc_cortex/index.html

• Robotics Academy– http://www.education.rec.ri.cmu.edu/content/vex/

index.htm

Page 17: ROBOTC for CORTEX While Loops Part 1. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a

Homework• Program 1: Program the right motor to spin forward

for 3 seconds, then reverse for 3 seconds, forever.

• Program 2: Program the left and right motors to spin forward until the potentiometer value is greater than 2048. Then program the motors to turn off and the LED to turn on until the potentiometer value is less than 2048. Repeat while the limit switch is not pressed.

• Watch the “Robot Decision Making” video (should be included with this webinar.