Built-In Sensors

If you are using the CBC, you have one built-in, hardware sensor.
That is the little black button next to Motor Port 0. (The pencil is pointing to it in the photo below)

If you write black_button(); your program will check to see if the black button is pressed, if yes then it will return a value of 1 if not, a value of 0.

Here is a simple and silly program to demonstrate how to use the black button.

int main()
{
     printf("Push the black button to move forward.\n"); /*prints instructions*/
     while(black_button()==0) /* computer waits as long as black button is not pressed */
// no function, just wait for button}
     motor(0,100);
     motor(3,100);
     msleep(2000); // wait for 2000 millisec
} // end propram & turnoff motors

Write it. Download it. Run it.

This will cause your robot to tell you to press the black button in order to move forward and then wait until you do so. When you do push the button, the robot will move forward at full motor power for 2 seconds.  (Refer to “Printing” and “Looping [while]” pages for more information on these functions.)



NEXT: Virtual Buttons

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