# I know this can be done with less code!!

**URL:** <https://forum.arduino.cc/t/i-know-this-can-be-done-with-less-code/374950>\
**Category:** Programming\
**Created:** [March 25, 2016, 4:32pm UTC](https://forum.arduino.cc/t/i-know-this-can-be-done-with-less-code/374950 "2016-03-25T16:32:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZachhacZ](https://avatars.discourse-cdn.com/v4/letter/z/a6a055/32.png) [@ZachhacZ](https://forum.arduino.cc/u/ZachhacZ)\
**Post date:** [March 25, 2016, 4:32pm UTC](https://forum.arduino.cc/t/i-know-this-can-be-done-with-less-code/374950/1 "2016-03-25T16:32:12Z")

</div>

I'm making a midi sequencer with the seeedstudio TFT shield.  
I made a little gird for the sequencer and need the squares to toggle between black and blue to show if it's active or not. I have a way to place the square in the correct spot but I can't figure out how to do it efficiently. I need to be able to toggle them at any time...

Heres the chunch of code that plots the square.  
I'll put the whole thing in the attachment.

I assume I would have to iterate over it somehow and change the the values in the if statements but I'm still kindof a noob and I'm not entirely sure what I'm doing.  
Thanks for the help!

//GRID TOGGLES  
if (x \> 0 && x \< 60 && y \> 180 && y \< 240) //first square  
{  
//have to have way to tell if it's already selected or not  
if (seqBool[0]==0)  
{  
//fill square  
Tft.fillRectangle(1, 181, 59,59,CYAN); //slightly smaller square fits inside blue grid square. hense 61 instead of 60  
seqBool[0]=1;  
}

else  
{  
Tft.fillRectangle(1, 181, 59,59,BLACK); //slightly smaller square fits inside blue grid square. hense 61 instead of 60  
seqBool[0]=0;  
}  
}

[tftSequencer.ino](https://forum.arduino.cc/uploads/short-url/9XieuS1V50pwKUvAPTbwNNEEFzD.ino) (10.7 KB)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 25, 2016, 4:45pm UTC](https://forum.arduino.cc/t/i-know-this-can-be-done-with-less-code/374950/2 "2016-03-25T16:45:15Z")

</div>

> I assume I would have to iterate over it somehow

Yes. A for loop would be good. Actually a pair of them for handling the rows and the columns.

> and change the the values in the if statements

Yes, based on the loop indices.

> but I'm still kindof a noob and I'm not entirely sure what I'm doing.

Try something. If it doesn't work, and you can't figure out why, we'll be here all week.

---

<div class="post-metadata">

**Author:** ![backwoodsjack](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/backwoodsjack/32/1098571_2.png) [@backwoodsjack](https://forum.arduino.cc/u/backwoodsjack)\
**Post date:** [March 25, 2016, 5:09pm UTC](https://forum.arduino.cc/t/i-know-this-can-be-done-with-less-code/374950/3 "2016-03-25T17:09:21Z")

</div>

I ran into the same 'slow' fillRectangle problem with the 3.2 TFT display.. fillRectangle is a slow function..  
drawRectangle is fast if u can use that instead..  
U might check the TFT.h and see if theres any notes on speeding up fill functions

---

<div class="post-metadata">

**Author:** ![lloyddean](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@lloyddean](https://forum.arduino.cc/u/lloyddean)\
**Post date:** [March 25, 2016, 6:22pm UTC](https://forum.arduino.cc/t/i-know-this-can-be-done-with-less-code/374950/4 "2016-03-25T18:22:30Z")

</div>

I see nothing in 'TFTv2.h' that suggests a way to speed it up, but as PaulS say's looping the drawing of like sized rectangles can be simplified and made easier to read.

```cpp
{
    int16_t    _left;
    int16_t    _top;
    int16_t    _width;
    int16_t    _height;

    rect_t(int16_t left = 0, int16_t top = 0, int16_t width = 0, int16_t height = 0)
        : _left(left), _top(top)
        , _width(width), _height(height)
    { }

    rect_t& operator= (const rect_t& rhs) 
    {
        _left  = rhs._left;
        _top    = rhs._top;
        _width  = rhs._width;
        _height = rhs._height;

        return *this;
    }

    rect_t& offset(int16_t dx, int16_t dy)
    {
        _width  += dx;
        _height  += dy;

        return *this;
    }

    int16_t left() const    { return _left; }
    int16_t top() const    { return  _top; }
    int16_t width() const  { return _width; }
    int16_t height() const  { return _height; }
};

void drawGrid()
{
    // ... allocate an instance of rect_t named 'rect' ...
    // ... 'rect' will be initialized a to 0, 0 and a width and height of 12, 60 ...

    rect_t  rect(0, 0, 12, 60);

    for ( size_t iCol = 0; iCol < 3; iCol++ )
    {
        // ... allocate another instance of 'rect_t' named 'rc' and initialize it with the existing rectangle 'rect' ...
        rect_t  rc = rect;

        // ... offset instance 'rc' by 'iCol' times its width ...
        rc.offset(iCol * rc.left(), 0);
        Tft.drawRectangle(rc.left(), rc.top(), rc.width(), rc.height(), BLUE);
    }
    ... 
}

```

---

<div class="post-metadata">

**Author:** ![backwoodsjack](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/backwoodsjack/32/1098571_2.png) [@backwoodsjack](https://forum.arduino.cc/u/backwoodsjack)\
**Post date:** [March 25, 2016, 6:36pm UTC](https://forum.arduino.cc/t/i-know-this-can-be-done-with-less-code/374950/5 "2016-03-25T18:36:40Z")

</div>

this likely wont work for u, but u might find similar--  
[http://forum.arduino.cc/index.php?topic=140111.30](http://forum.arduino.cc/index.php?topic=140111.30)

---

<div class="post-metadata">

**Author:** ![backwoodsjack](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/backwoodsjack/32/1098571_2.png) [@backwoodsjack](https://forum.arduino.cc/u/backwoodsjack)\
**Post date:** [March 25, 2016, 6:43pm UTC](https://forum.arduino.cc/t/i-know-this-can-be-done-with-less-code/374950/6 "2016-03-25T18:43:36Z")

</div>

i should have included this link--

> **[Arduino - Improving refresh rate of a TFT LCD display](http://matthewcmcmillan.blogspot.com/2014/01/arduino-improving-refresh-rate-of-tft.html)**
>
> Matthew McMillan Linux Raspberry Pi System Administration Arduino Electronics Ubuntu Maker

theres some good ideas there

---

<div class="post-metadata">

**Author:** ![econjack](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/econjack/32/1105255_2.png) [@econjack](https://forum.arduino.cc/u/econjack)\
**Post date:** [March 25, 2016, 6:46pm UTC](https://forum.arduino.cc/t/i-know-this-can-be-done-with-less-code/374950/7 "2016-03-25T18:46:43Z")

</div>

I've recently been using the Adafruit GFX and MCUFRIEND\_kbv libraries and find them extremely fast on the UNO. For reasons I don't fully understand, the routines seem about half as fast on a Mega board.

BTW, I have this 3.95" TFT color display

[http://www.ebay.com/itm/322009754655?\_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT](http://www.ebay.com/itm/322009754655?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT)

working on the UNO with the libraries above and it is very nice. However, I am running out of Flash, hence the try with the Mega, but the graphics are not as fast as I'd like. I'd really like to get the code moved to a Teensy 3.2, but have not been able to get the libraries to compile with it. If there are any Teensy experts out there who have a good graphics engine, I'd love to hear about it.

---

<div class="post-metadata">

**Author:** ![ZachhacZ](https://avatars.discourse-cdn.com/v4/letter/z/a6a055/32.png) [@ZachhacZ](https://forum.arduino.cc/u/ZachhacZ)\
**Post date:** [March 25, 2016, 7:28pm UTC](https://forum.arduino.cc/t/i-know-this-can-be-done-with-less-code/374950/8 "2016-03-25T19:28:51Z")

</div>

I'm having trouble trying to figure this out with for loops.  
It works fine if I type it ALL out but that takes forever and is hard to read.

---

<div class="post-metadata">

**Author:** ![lloyddean](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@lloyddean](https://forum.arduino.cc/u/lloyddean)\
**Post date:** [March 25, 2016, 7:39pm UTC](https://forum.arduino.cc/t/i-know-this-can-be-done-with-less-code/374950/9 "2016-03-25T19:39:17Z")

</div>

> ZachhacZ:  
> I'm having trouble trying to figure this out with for loops.  
> It works fine if I type it ALL out but that takes forever and is hard to read.

Reply #3

---

<div class="post-metadata">

**Author:** ![ZachhacZ](https://avatars.discourse-cdn.com/v4/letter/z/a6a055/32.png) [@ZachhacZ](https://forum.arduino.cc/u/ZachhacZ)\
**Post date:** [March 25, 2016, 8:39pm UTC](https://forum.arduino.cc/t/i-know-this-can-be-done-with-less-code/374950/10 "2016-03-25T20:39:06Z")

</div>

> lloyddean:  
> Reply #3

Sorry I failed to notice that...  
It's a little over my head could you explain whats going on?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:48pm UTC](https://forum.arduino.cc/t/i-know-this-can-be-done-with-less-code/374950/11 "2021-05-05T21:48:13Z")

</div>


