modified for /if loop to replace many if statements

Editing the wiki is a bit over my head, but just took a quick break from programming to post this orderly code snippet to dissolve at minimum 36 lines from a main loop, both being there and having to write them. I dont think it makes things any faster, but scanning for a Y row then an X button I think is easier this way.

  unsigned int touchX,touchY,ta,tb;
  while(true){
  if(touch.dataAvailable()){              //if the screen was touched
    touch.read();                        //get raw data.. DON'T FORGET THIS LINE
    touchX = touch.getX();                //get formatted data from X
    touchY = touch.getY();                //get formatted data from Y

    if(touchY > 0 && touchY < 100){//my 12 color squares are 100px Y, test if touch was within Y
      for(int u =0; u <=12; u++){       //genius compact loop!!
        ta = u*27;                //make sure to adv Xtouch location (LT side of a virtual screen button)
        tb = ta + 24;             //X size of color squares (x size of virtual screen buttons)      
        if(touchX > ta && touchX < tb){// <<--------------explained in post
          myLED(u);  //send myLED the value of u, see post.
          break;    //"button" picked, exit detector loop
        }
      }      //first test if within color band, then check which color was touched, then set led color
    }
  }
 }

Code snippet from a color picker demo i’m working on. I have an Itead ITD32S for the MEGAadk, using UTFT/UTFTouch latest library from Rinky-Dink Electronics. A separate for loop uses a 1D word array of 12 RGB565 values to set the pen color in UTFT, that loop then draws 12 colored 24x100y px filled rectangles across the screen. I know, so what.
Then i have the snippet of code pictured above, this one for loop gets rid of typing/copy/pasting 12 ugly if(x is in this range){send this exact color to myLED function, then break;} statements in your main control structure.

Each time through this loop “ta”(the left side of a color bar) and “tb”(the right side of a color bar) gets incremented equally. When (touchX is greater than “ta” AND lesser than “tb”), evaluates TRUE, the statement “myLED(u);” gets run followed by a break; statement.

myLED(u) is a function elsewhere that uses “u” as an argument to get an RGB888 color from a 2D byte array:

const byte colorStep[][3] ={
  {255,  0,  0},  //RGBW red
  {255,128,  0},  //orn
  {255,255,  0},  //yel
  {128,255,  0},  //lime
  {  0,224,  0},  //grn
  {  0,255,128},  //aqua
  {  0,255,255},  //sky
  {  0,128,255},  //ind
  {  0,  0,255},  //blu
  {128,  0,255},  //vio
  {255,  0,255},  //pnk
  {255,  0,128},  //mag
  };

“u” is used as “colorStep u , x” to populate r,g,b variables that get further mapped before analogWrite(); is called. This isn’t a question so much as a performance in code readability. It makes it easier to pinpoint bugs, or to make an array of easily detectable buttons by adding more of these loops and changing the Y range in the first IF statement. Hope this helps some of you.

the inner loop can be without multiplication (slightly faster, more reactive)

for(byte u = 0, ta = 0, tb = 24; u <= 12; u++, ta += 27, tb += 27)
{
  if (touchX > ta && touchX < tb)
  { 
    myLED(u);   
    break; 
  }
}

Reading that particular part of the code shows that when touchX == 25 or 26, myLED(u) is never called.
is that on purpose?

to really speed up the inner loop you calculate the value of u directly from touchX

u = touchX / 27;
ta = u * 27;
tb = ta + 24;
if (touchX > ta && touchX < tb)
{ 
  myLED(u);   
}

This will result in a constant performance while a lookup loop will perform different depending on the position touched. (OK it are just uSec’s)

robtillaart:
to really speed up the inner loop you calculate the value of u directly from touchX

u = touchX / 27;

ta = u * 27;
tb = ta + 24;
if (touchX > ta && touchX < tb)
{
 myLED(u);  
}




This will result in a constant performance while a lookup loop will perform different depending on the position touched. (OK it are just uSec's)

divides take about 40 intructions, bitshift wherever possible take 1 to 2 instructions as you may know. Switching my screen button sizes to binary friendly values, bitshift divides (>>1, >>2 ,>>n).

But I like your first post of updating the for loop itself with the values, I had no idea that could be done! Is that some C magic?

rinkrides: divides take about 40 intructions, bitshift wherever possible take 1 to 2 instructions as you may know. Switching my screen button sizes to binary friendly values, bitshift divides (>>1, >>2 ,>>n).

Yes, but GCC is an optimizing compiler. If you divide or multiply by a constant power of two, it will produce a bit shift in machine code.