FUNCTIONS

In the program Drive with Arrows, there are four functions declared, defined, and called. When you use a function, you are calling it.

Write the following, download it and run it.

 Drive With Arrows:

/***************************************************************************
******* DRIVE WITH ARROWS by Azi Crawford 2011:07:06:09:55:10
******* This program is to controll your robot with the virtual buttons
*******  While the up arrow is touched - the robot with go forward at half power
*******  While the down arrow is touched -the robot will go backward at half power
*******  While the right arrow is touched - the robot will spin clockwise
*******  While the left arrow is touched - the robot will spin widdershins
*******  (counter-clockwise)
***************************************************************************/
/*Function prototypes below*/
void spin_right();
void spin_left();
void go_forward();
void go_backward();

int main()
{
     /*Now that I have declared functions that will tell the robot what to do
      ** I'll need a way of telling the robot to run them when and *only* when
      ** the buttons are pressed. Ideally, it is best to write this out on 
      ** paper first - this is called "paper-coding," for this exercise - I'll 
      ** be writing out my process in the comments*/
      //use display to tell operator how to select options
     printf("Use the arrow keys to control the robot.\n");
     printf("\n");
     printf("Up arrow moves robot forward.\n");
     printf("Down arrow moves the robot backward. \n");
     printf("Right arrow spins the robot clockwise. \n");
     printf("Left arrow spins the robot counter-clockwise.\n");
     printf("\n");
     printf("Press the black button to end program. \n");
     while(!black_button())
	{//keep the program going until the black (or side) button is pressed
        //if no buttons are pressed - then there should be no movement -> ao()
	  if (up_button()) /*I will have an if statement call the appropriate
             function when the button is pushed.*/
              {
		go_forward();
	      }
	   while(up_button()){ } /* an empty while loop that will keep the robot
                               spinning while the up arrow is pressed*/
	   if (down_button()) // same as above, but for moving backwards
	       {
		go_backward();
		}
	   while (down_button()){ }
	   if (right_button()) // same as above, but for spinning clockwise
		{
		 spin_right();
		}
	   while (right_button()){ }
	   if (left_button()) // same as above, but for spinning counter-clockwise
		{
		  spin_left();
		}
	   while (left_button()){ }
	   ao(); // if nothing is touched - nothing should move
	}
  printf("DONE!");
} // end of main

/*Function definitions go below.*/
void spin_right()
 {
	motor(3,-50);
	motor(0,50);
 }

 void spin_left()
 {
	motor(3,50);
	motor(0,-50);
 }

 void go_forward()
 {
	motor(3,50);
	motor(0,50);
 }

 void go_backward()
 {
	motor(3,-50);
	motor(0,-50);
 }

Notes:
Prototypes are how you warn the compiler that  your new functions follow.
The definitions are where you… um… well… actually define your new functions.

In general functions have input and output parameters, these simple ones don’t, [nothing in the ‘()’ and ‘void’ intro] since their objective is just to power the two motors.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s