Sunday, May 18, 2008

TouchShield: Did you just touch my RECT?

It's a good question: Was a touch press inside a particular area?

Using a helper function,


We now can do stuff if an area is touched.

But first, how do we make a rectangle? Let's introduce you to a new type on the TouchShield,

LCD_RECT button;

This creates a rectangle named button. It is initialized this way,

button.left = 0; = 0;

button.right = 80; button.bottom = 60;

Or if you're more hardcore and like shorthand,
LCD_RECT button = {0,0,80,60};

Either way you chose to create it, the rectangle is 80 wide by 60 tall, and starts at location (0,0).

Don't believe it's that easy and still skeptical? Then lets prove it by drawing it on the screen. To do this, we first need to open the Crayola box and grab a color,

COLOR blue = {0,0,255};

You can now draw it,

lcd_rect(button, blue, blue);

Now that we've got that covered, lets do something with our fancy pointInRect function.

As the function name tends to suggest, pointInRect needs a POINT and an LCD_RECT. We've got the LCD_RECT covered, now B.Y.O.P. (bring you're own POINT).


And call pointInRect like this,

if ( pointInRect(p, button) )
//button pressed, do something!

Let's put it all together and paint the button if it was touched. We'll do this by polling the touchscreen. Here is the (complete) code,

COLOR blue = {0,0,255};
LCD_RECT button = {0,0,80,60}; //initialized hardcore style

void setup() { ; }

void loop()

if ( touch_get_cursor(&p) )
if ( pointInRect(p, button) )
lcd_rect(button, blue, blue);

} //loop

See how simple yet powerful the pointInRect function can be?


michelef said...


I tried the code but I get this:

Connecting to programmer: .avrdude: butterfly_recv(): programmer is not responding

What am I doing wrong?
Arduino Diecimila v011 - Touch (007) mac

Chris said...


The PC doesn't see TouchShield.
Make sure you do 2 things,

1.) Set the board to TouchShield in the Arduino IDE menu

2.) Press the program button on the TouchShield before uploading.

Hope that helps!

Anonymous said...

I'm having trouble using more than one rectangle button or TOUCH point. I have uploaded an example of erratic behaviour I'm getting.

[color=#777755]// pointInRect(); (dual use bug example) [/color]
[color=#777755]// I tried using two different POINT's . Can this be done? [/color]
[color=#777755]// I get the orange rectangle have the wrong width and x-position, and is overwriting the blue rectangle[/color]
COLOR blue = {
COLOR orange = {
LCD_RECT buttona = {
0,0,30,128}; [color=#777755]//initialized hardcore style[/color]
LCD_RECT buttonb = {
64,20,30,128}; [color=#777755]//initialized hardcore style[/color]

[color=#CC6600]void[/color] [color=#993300][b]setup[/b][/color]() {

[color=#CC6600]void[/color] [color=#993300][b]loop[/b][/color]()

[color=#CC6600]if[/color] ( touch_get_cursor(&a) ) [color=#777755]// [/color]
[color=#CC6600]if[/color] ( pointInRect(a, buttona) )
lcd_rect(buttona, blue, blue); [color=#777755]// part of this blue rectangle is not suposed to be overwritten with orange rectangle (wich should start at x pixel 64)[/color]
[color=#996600]delay[/color] (1000); [color=#777755]// rediculously long delay [/color]

[color=#CC6600]if[/color] ( touch_get_cursor(&b) )
[color=#CC6600]if[/color] ( pointInRect(a, buttona) )
lcd_rect(buttonb, orange, orange); [color=#777755]// top of rectangle is lowered on purpose. It gets draw too wide , overlapping the blue rectangle. [/color]

} [color=#777755]//loop[/color]


Could someone plase try it and have a look at it?

Chris said...


The touch_get_cursor() function returns the screen coordinates of 1 touch. So calling touch_get_cursor twice, would mean your expecting the person to have touched the screen twice, is this what you want?

If not, try this instead...

if (touch_get_cursor(&a))
//do something for button a

if (pointInRect(a,buttonb))
//do something for button b

Also, I haven't tested your code just yet, but I noticed that your rectangle coordinates are too large for the screen.

The x-pixels can be 0-127
The y-pixels can be 0-127