How do you make buttons on the brain screen??

So I was wondering since i know it is possible, how would you make buttons on the brain screen? Like how can you tell your screen if it’s pressing this certain range of positions do this while pressing another position do something else?

You can get x position and get y position of the screen that was touched.
So you draw a few boxes at know positions and then check if the coordinates within that box have been touched.

Look under Brain, then Sensing.

Do you perhaps have an example code of it because I can’t think of how to do and if statement for it. I’m sorry

Something like this:

int main() {
    int originX = 10;
    int width = 100;
    int originY = 10;
    int height = 100;
    int endX = originX + width;
    int endY = originY + height;
    Brain.Screen.drawRectangle(originX, originY, width, height);
        if (Brain.Screen.pressing()){

            int X = Brain.Screen.xPosition();//X pos of press
            int Y = Brain.Screen.yPosition();// Y pos of press

            //Checks if press is within boundaries of rectangle
            if ((X >= originX && X <= endX) && (Y >= originY && Y <= endY)){
                //Do something

I haven’t tested it but it should work

1 Like

Since I have multiple buttons and would have some else ifs would i need an else at the end (sorry for all the questions)?

Unless you want to do something if none of the buttons are pressed you don’t need an else at the end

1 Like

PROS uses the littlevgl graphics library, which provides objects for buttons, labels, animations, bars, guages, and more. It’s considerably more powerful than just drawing a square and checking whenever/wherever a touch event happens.

ok just checking thank you.

Generally for anything more than a couple of buttons you want to define a C structure or C++ object to hold the button information and state. Here is a simple example that shows several on screen buttons that can be toggled on and off.
buttons.vex (7.5 KB)


You can draw the button in anyway you would like, they don’t have to be square (although it’s generally always easier to detect in a rectangular area) and can contain text if you would like. Here is a screen shot of another demo I wrote a few months ago with two circular buttons.

1 Like

do you mind sharing the source for this demo? very pretty :0

Well, it should sort of work. When making selections this way it is important to remember that the human hands is not very quick compared to the rate at which the processor can cycle through such a while loop. So you’ll do that “do something” essentially a random number of times based on how long it takes you to press and release the button. The solution to this is, once you’ve detected a button press and you don’t want to receive continuous input like we usually do with joysticks, you wait. The best is to wait for a release, but you can manage it with time, which may be better in certain circumstances.

I will release at some point in the future. Although the code will build in VCS, it uses some native C API’s that have not been ported into the VCS brain::lcd class yet, so I will release it when everything works with the documented C++ API.

1 Like

Here are links to programs which show how to draw shapes on the brain display, and then use them for touch inputs:

In Blockly
In Python
In C++

So I’m testing a button code on the brain screen and its not responding I’ll put my code below if you could help me figure out why it isn’t working that’d be awesome!

int main() {
    int RectangleWidth = 239;
    int RectangleHeight = 119;
    int xTLOrigin = 1;
    int xTLEnd = 240;
    int yTLOrigin = 1;
    int yTLEnd = 120;
    int xTROrigin = 241;
    int xTREnd = 480;
    int yTROrigin = 0;
    int yTREnd = 120;
    int xBROrigin = 241;
    int xBREnd = 480;
    int yBROrigin = 121;
    int yBREnd = 240;
    int xBLOrigin = 0;
    int xBLEnd = 121;
    int yBLOrigin = 121;
    int yBLEnd = 240;
    //Drawing the screen buttons (modeled after the windows logo lol)
  while (1)
      //Brain Screen button programming
      if (Brain.Screen.pressing())
         int Xpos = Brain.Screen.xPosition();
         int Ypos = Brain.Screen.yPosition();
          if ((Xpos <= xTLOrigin && Xpos >= xTLEnd) && (Ypos <= yTLOrigin && Ypos >= yTLEnd))//declaring if the screen is being touched within the top right box
              //Red side programs go here
              Brain.Screen.print("Top Left button was touched");
          else if ((Xpos >= xBLOrigin && Xpos <= xBLEnd) && (Ypos >= yBLOrigin && Ypos <= yBLEnd))//declaring if the screen is being touched within the bottom left box box
              Brain.Screen.print("Bottom Left button was touched");
              if ((Xpos >= 0 && Xpos <= 480) && (Ypos >= 0 && Ypos <= 240)) //Since I tapped the screen again it will now print out the intro to the bee movieto the bee movie
                  Brain.Screen.print("Test of second touch of screen");
          else if ((Xpos >= xBROrigin && Xpos <= xBREnd) && (Ypos >= yBROrigin && Ypos <= yBREnd))//declaring if the screen is being touched within the bottom right box box
              //Blue side programs go here
              Brain.Screen.print("Bottom Right was touched");
          else if ((Xpos >= xTROrigin && Xpos <= xTREnd) && (Ypos >= yTROrigin && Ypos <= yTREnd))//declaring if the screen is being touched within the top left box box
              Brain.Screen.print("The Battery level is");
              Brain.Screen.print("The Battery temperature is");
              Brain.Screen.print("Top Right was touched");

@abennett5139 have you tried doing it with just one rectangle?

No but I can

@eggplant I did the same thing this time with one and it worked, could it be that they’re possibly overlapping in their rectangles and I should make the boxes spread out?

I would add one rectangle at a time and check if it works so if it stops working you know where the error is

Either way, your first rectangle check is wrong:

Xpos, obviously, can’t be smaller than 1 and bigger than 240 at the same time.
But the rest of the code looks OK.