Virtual Sensors

On your CBC, you may notice that the screen shows virtual buttons like a Game-Boy controller:

A four way directional controller, an A button, and a B button.

These buttons can be checked in the same way as the black button, using library function calls:

  • up_button()
  • down_button()
  • left_button()
  • right_button()
  • a_button()
  • b_button()

For example, if the A button is touched, a_button() will return a value of 1.

Try this program out, it uses all six of the above:

The robot will wait for the A button to start, then when commanded by the directional buttons,

it with go forward, reverse, spin right or left, for 2 seconds.  All action will end if the B button

is pushed

int main()

{

while(!a_button()) // program waits until you press A before continuing

{

}

while(!b_button()) //program continues to run until you press B

{

if(up_button()) // makes your robot go forward for 2 seconds

{

motor(0,100);

motor(3,100);

sleep(2);

}

if(down_button()) // make your robot go backwards for 2 seconds

{

motor(0,-100);

motor(3,-100);

sleep(2);

}

if(left_button()) // makes your robot spin to the left for 2 seconds

{

motor(0,-100); //if 0 is your left motor port

motor(3,100);

sleep(2);

}

if(right_button())

{

motor(0,100);

motor(3,-100);

sleep(2);

}

ao(); //turns off the motors before looping to recheck the buttons

}

}

Here’s an example of a program designed to use the buttons to steer a robot around ->

Drive with Arrows

NEXT: Digital Sensors

Advertisements

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