Array not working in if conditional?

Hi, I’m having trouble with my code. I have a condition where if I press a button ,that has its values in the int block , the button should either change its color to a different one or change back to the predefined one in int block. The Compiler compiles the project no problem but when I put it in the kit, there’s nothing happening.Drawing on the display still works (I put a spi.pixel(touch_x,touch_y,Blue); which makes pixel everytime it gets values from the touch) but anything after that the function does nothing.

void Button(){
    int is_pressed = 0;
    int block[] = {0,0,180,70,Blue};
    while(1)
    {
      if (touch.DataAvailable())
      {
        if(touch.Read())
        {
         unsigned short int touch_y = touch.GetX();
         unsigned short int  touch_x = touch.GetY();
         touch_x = touch_x - 10;
         touch_y = touch_y + 10;
         spi.pixel(touch_x,touch_y,Blue);
            if ((touch_x >= block[0] && touch_x <= block[1])&&(touch_y >=  block[2] && touch_y <=  block[3]) && is_pressed == 1){
               spi.fillrect( block[0], block[1], block[2], block[3], block[4]);
               is_pressed = 0;
               wait(0.3);  
               }else if ((touch_x >= pravy[0] && touch_x <= pravy[1])&&(touch_y >= pravy[2] && touch_y <=  block[3])){
              is_pressed = 1;
              different_color(is_pressed);
               wait(0.3);   
            }           
        }
      }
    }    
}

Hello Stepan,

Find below a few tips on debugging:

  • To cover all possibilities, after the last else if block put one more. For example,
// Only for debugging. To be deleted in final code.
else {
    spi.pixel(touch_x, touch_y, Yellow);
    wait(1);
}

the button should either change its color to a different one or change back to the predefined one in *int block

  • I’d suggest to first test whether the button was touched and then to test its last status. For example:
...
// Was the button touched?
if                                                 
(                                                  
    (touch_x >= block[0] && touch_x <= block[1]) &&
    (touch_y >= block[2] && touch_y <= block[3])                              
) { 
    // Yes.
    // What is its last status?
    if (is_pressed) {
        is_pressed = 0; // change status
        spi.fillrect(block[0], block[1], block[2], block[3], block[4]); // return color to the default/predefined
        wait(0.3);
    }
    else {
        is_pressed = 1; // change status
        different_color(is_pressed); // change color to new one
        wait(0.3);
    }
}
// Only for debugging. To be deleted in final code.
else {
    // No.
    spi.pixel(touch_x, touch_y, Yellow);
    wait(1);
} 
...
  • Try to replace

int block[] = {0,0,180,70,Blue};

with

int block[] = {0,180,0,70,Blue};

and then check whether the order of parameters when calling the spi.fillrect(...) function is correct.

1 Like

Hello Zoltan,

Sorry for the late reply

The debugging tips that you gave me were really useful. The problem was in my if statement where I compared x axis with y axis. I feel dumb that tI didn’t notice it in the first place…

Thank you so much!

Never mind! I’m glad that you fixed it.