Recent Changes - Search:

TEAMS Academy Wiki

THS 2011-2012

Explore TEAMS!
for visiting sophomores & juniors

Bodies & Bones

Interactive Robotics

Environmental BioTech

Bat Engineering Design

Assistive Technology & Electronics

Students (Forum)

Instructors

School Calendar

FY09 Planning

TEAMS Web Site

Wiki Info

edit SideBar

Flow Control: Do This or That

A program that blindly executes a list of commands may be useful, but without the ability to detect changes in its data or make choices based on the data, the program will remain very limited. In robotics, making choices based on the data is essential: Has the robot bumped into something? Are the batteries low? How close is the robot to the wall? Is the floor below the robot black, white, or blue? These are the types of questions your robot will need to answer and respond to.

The most basic C command that you will use to make a choice is the if command. If statements can look like this:

// If statement with one command
if(condition_is_true)
   do_this_command;

// If statement with multiple commands
if(condition_is_true)
{
   do_this_command;
   do_that_command;
}

The first part of the if statement is the conditional test. The conditional test is made up of logical operations that will result in either a 1 or a 0 value. You can also think of it as true or false, yes or no, +5V or GND, respectively. When the conditional test results in a 1 (true, yes), the next line of code will be executed; otherwise, if the result is a 0 (false, no), the next line of code will be skipped. If you have curly braces after the if statement, then all of the lines of code between the braces will be either executed or skipped. Make sure you do not put a semicolon after the if statement; the compiler will think that the semicolon is the next line to be executed, so the if statement will do nothing even if the condition test is true.

One of the simplest things you can do with the Create is to detect if one of it's bumpers is pressed.

Show how if is used to sense bumper. Then expand to use an else. Then show if-elseif-else for both bumpers. Make note of use of cr8_update()
Then show how a simple while(1) loop will keep the robot running constantly. Expand that to safe-mode like sensing. NOTE THAT UPDATE MUST BE IN EVERY LOOP WITH SENSORS.
Then, show off the other types of loops, and cool things you can do with them.


<< Data Structure | Home Page | User Input >>


Have a Question? Please ask below.

Edit - History - Print - Recent Changes - Search
Page last modified on January 18, 2008, at 12:29 PM