Array not working in if conditional?

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