While Loops Part 2 PowerPoint

17
ROBOTC for CORTEX While Loops Part 2

Transcript of While Loops Part 2 PowerPoint

Page 1: While Loops Part 2 PowerPoint

ROBOTC for CORTEXWhile Loops Part 2

Page 2: While Loops Part 2 PowerPoint

Timers• More loop control please?

– Question: Where would the wait statement go if we wanted the loop to repeat for a controlled amount of time?

– Answer: Nowhere! We need something else.

• Solution: Timers– Can be thought of as internal stopwatches (4 available)– Like encoders, timers should be “cleared” anytime before they

are used• Watch where you clear them!

Page 3: While Loops Part 2 PowerPoint

While Loop Exercise 4• Example: Program the greenLED to

repeatedly turn on for 2 seconds, then off for 2 seconds, while less than 20 seconds have elapsed.

Page 4: While Loops Part 2 PowerPoint
Page 5: While Loops Part 2 PowerPoint
Page 6: While Loops Part 2 PowerPoint

Timers• Follow along with the Remote Control >

Timers Videos for Review

Page 7: While Loops Part 2 PowerPoint

Variables• A variable is a space in your robot’s memory

where you can store data, such as whole numbers, decimal numbers, and words.

• Variable names follow the same rules as custom motor and sensor names:– Capitalization, Spelling, Availability

• Usefulness– Can store data to be manipulated by your robot– Can improve the readability and expandability of your programs

Page 8: While Loops Part 2 PowerPoint

VariablesData Type Description Example Code

Integer Positive and negative whole numbers, as well as zero.

-35, -1, 0, 33, 100

int

Floating Point Number

Numeric values with decimal points (even if the decimal part is zero).

-.123, 0.56, 3.0, 1000.07

float

Boolean True or False. Useful for expressing the outcomes of comparisons.

true, false bool

Character Individual characters, placed in single quotes.

‘L’, ‘f’, ‘8’ char

String Strings of characters, such as words and sentences placed in double quotes.

“Hello World!”, “asdf”

string

Page 9: While Loops Part 2 PowerPoint

Creating a Variable• To create a variable, you must “declare” a type,

which is the type of data it will hold, as well as a name that it can be referenced by:

• Variables can be set to different values throughout your program. Giving a variable its initial value is called “initializing” it

Page 10: While Loops Part 2 PowerPoint

Variables Exercise• Example: Create a count-based while loop

that turns a motor on for 5 seconds, then off for 5 seconds, 3 times.

Page 11: While Loops Part 2 PowerPoint

Additional Information

Page 12: While Loops Part 2 PowerPoint

While Loop Exercise• 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: While Loops Part 2 PowerPoint

Solution Code

Page 14: While Loops Part 2 PowerPoint

Equivalent Solution Code

• “until” commands are implemented as while loops in the Natural Language

Page 15: While Loops Part 2 PowerPoint

Equivalent Solution Code

• Program Flow gets stuck in these locations. How do we avoid this?

Page 16: While Loops Part 2 PowerPoint

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: While Loops Part 2 PowerPoint

Homework• Program the flashlight or greenLED to

repeatedly turn on for 5 seconds, then off for 5 seconds, while less than 1 minute has elapsed.

• Create a count based while loop that turns on the left motor for 3 seconds, then off for 3 seconds, 5 times.