1 / 17

ROBOT C for CORTEX While Loops Part 2

ROBOT C for CORTEX While Loops Part 2. 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

queenie
Download Presentation

ROBOT C for CORTEX While Loops Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ROBOTC for CORTEXWhile Loops Part 2

  2. 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!

  3. 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.

  4. Timers • Follow along with the Remote Control > Timers Videos for Review

  5. 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

  6. Variables

  7. 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

  8. Variables Exercise • Example: Create a count-based while loop that turns a motor on for 5 seconds, then off for 5 seconds, 3 times.

  9. Additional Information

  10. 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.

  11. Solution Code

  12. Equivalent Solution Code • “until” commands are implemented as while loops in the Natural Language

  13. Equivalent Solution Code • Program Flow gets stuck in these locations. How do we avoid this?

  14. 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

  15. 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.

More Related