1804 views

# How do you make buttons on the brain screen??

1. 4 months ago

### abennett5139

Sep 15 Concord, NC 5139

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?

2. ### calvc01

Sep 15 Event Partner, V5 Beta Tester Colchester, United Kingdom

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.

http://help.vexcodingstudio.com

Look under Brain, then Sensing.

3. ### abennett5139

Sep 15 Concord, NC 5139

@calvc01 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.

http://help.vexcodingstudio.com

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

4. ### eggplant

Sep 15 Wisconsin 9144B

@abennett5139 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);

while(true){
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

5. ### abennett5139

Sep 15 Concord, NC 5139

@eggplant 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);

while(true){
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

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

6. ### eggplant

Sep 15 Wisconsin 9144B

@abennett5139 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

7. ### edjubuh

Sep 15 V5 Beta Moderator West Lafayette, IN
Edited 4 months ago by edjubuh

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.

8. ### abennett5139

Sep 15 Concord, NC 5139

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

ok just checking thank you.

9. ### jpearman

Sep 16 Moderator, ROBOTC Tech Support, V5 Beta Moderator Los Angeles 8888

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.

10. ### jpearman

Sep 16 Moderator, ROBOTC Tech Support, V5 Beta Moderator Los Angeles 8888

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.

11. ### phantom285A

Sep 16 285A

@jpearman 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.
[attachment:5b9dd241c61bd]

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

12. ### callen

Sep 16 Braintree, MA, USA

@eggplant Something like this:
...
I haven't tested it but it should work

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.

13. ### jpearman

Sep 16 Moderator, ROBOTC Tech Support, V5 Beta Moderator Los Angeles 8888

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

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.

14. ### Rick TYler

Sep 17 Teachers/Coaches, Event Partner, V5 Beta Moderator Redmond, Washington Founder of Exothermic Robotics

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

15. ### abennett5139

Sep 18 Concord, NC 5139
Edited 4 months ago by abennett5139

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)
Brain.Screen.drawRectangle(xTLOrigin,yTLOrigin,RectangleWidth,RectangleHeight,color::red);
Brain.Screen.drawRectangle(xTROrigin,yTROrigin,RectangleWidth,RectangleHeight,color::yellow);
Brain.Screen.drawRectangle(xBROrigin,yBROrigin,RectangleWidth,RectangleHeight,color::blue);
Brain.Screen.drawRectangle(xBLOrigin,yBLOrigin,RectangleWidth,RectangleHeight,color::green);

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.clearScreen();
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.clearScreen();
Brain.Screen.setCursor(1,1);
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.clearScreen();
Brain.Screen.setCursor(1,1);
Brain.Screen.setFont(fontType::mono15);
Brain.Screen.setPenColor(color::yellow);
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.clearScreen();
Brain.Screen.setCursor(1,1);
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.clearScreen();
Brain.Screen.setCursor(1,1);
Brain.Screen.setFont(fontType::mono20);
Brain.Screen.print("The Battery level is");
Brain.Screen.print(Brain.Battery.capacity(percentUnits::pct));
Brain.Screen.newLine();
Brain.Screen.newLine();
Brain.Screen.print("The Battery temperature is");
Brain.Screen.print(Brain.Battery.temperature(percentUnits::pct));
Brain.Screen.newLine();
Brain.Screen.newLine();
Brain.Screen.print("Top Right was touched");
}

}

}
}
}```
16. ### eggplant

Sep 18 Wisconsin 9144B

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

17. ### abennett5139

Sep 18 Concord, NC 5139

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

No but I can

18. ### abennett5139

Sep 18 Concord, NC 5139

@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?

19. ### eggplant

Sep 18 Wisconsin 9144B

@abennett5139 @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

20. ### nenik

Sep 19 V5 Beta Tester

Either way, your first rectangle check is wrong:

if ((Xpos <= xTLOrigin && Xpos >= xTLEnd) && (Ypos <= yTLOrigin && Ypos >= yTLEnd))

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