unexplained appearance of buttons on TFT

Members
The video explain it much better than I can. Any ideas on what might cause this and how to fix it would be appreciated.

The sketch is attached.

Thanks

Due_SubMenu.ino (11.7 KB)

screenshot

You have an array of all buttons in your sketch, whether visible or not:

Adafruit_GFX_Button *buttons[] = {&on_btn, &off_btn, &on_btn1, &off_btn1, &on_btn2, &off_btn2,
                                  &on_btn3, &off_btn3, &on_btn10, &off_btn10, &on_btn11,
                                  &off_btn11 , &on_btn12, &off_btn12, &on_btn13, &off_btn13, NULL
                                 };

Each time through loop() you do:

update_button_list(buttons);
bool update_button(Adafruit_GFX_Button * b, bool down)
{
  b->press(down && b->contains(pixel_x, pixel_y));
  if (b->justReleased())
    b->drawButton(false);
  if (b->justPressed())
    b->drawButton(true);
  return down;
}


bool update_button_list(Adafruit_GFX_Button **pb)
{
  bool down = Touch_getXY();
  for (int i = 0 ; pb[i] != NULL; i++) {
    update_button(pb[i], down);
  }
  return down;
}

So, even if the button is not visible, you check to see if the press is within its bounds. If it is then you render the button thus making it visible.

How to fix...well there are probably lots of ways, so I'll suggest one based only on a cursory scan of your code. There may be better (or worse) options.

Keeping a similar structure to what you have now: You make multiple button lists instead of just buttons[]. Each button list corresponds to a page of your menu system, just the active buttons. You have a global variable which you assign the currently active button list to. You pass the currently active button list into update_button_list() as before.

[
Members
I receive the following error when I declare the variable for the buttons:

exit status 1
Initializer fails to determine size of ‘buttons’

Thanks for the help

Due_SubMenu.ino (12.1 KB)

Ive tried this but no luck as of yet

Any help or guidance is appreciated.

Thanks

buttons1.jpg

No, it won't work that way. buttons[] needs to be a global, and you won't be able to reinitialise it inside functions with an initialisation list like you would a locally declared global.

You should declare the button lists as globals, eg.:

Adafruit_GFX_Button *page1_buttons[] = { &on_btn, &off_btn, NULL };
Adafruit_GFX_Button *page2_buttons[] = { &off_btn1, &on_btn2, &off_btn2, &on_btn3, &off_btn3, NULL };
Adafruit_GFX_Button *page3_buttons[] = { &on_btn10, &on_btn11, &on_btn12, &on_btn13, NULL };

Then declare a global for which button list is currently in use:

Adafruit_GFX_Button **active_button_list = { NULL };

Then in the screen building functions it just becomes a simple assignment:

void display_page1() {
    ...
    active_button_list = page1_buttons;
    ...
}

I thought that I followed the instruction but the onscreen buttons does not work(go to other menu’s). I placed Serial.print into the code to track programming/ fault find but can’t seem to work out what is wrong.

When I press a button nothing is printed in Serial.print.

Thanks

Due_SubMenu.ino (12.1 KB)

Now you're not calling update_button_list() at all. Was that your intention?

arduarn:
Now you’re not calling update_button_list() at all. Was that your intention?

I receive the following error when I call

update_button_list(buttons)

exit status 1
‘buttons’ was not declared in this scope

buttons2.jpg

Arduino: 1.8.5 (Windows 10), Board: “Arduino Due (Programming Port)”

In file included from C:\Users\Johan\Documents\Arduino\libraries\RF24-master/RF24.h:18:0,

from C:\Users\Johan\Documents\Arduino\Due_SubMenu\Due_SubMenu.ino:19:

C:\Users\Johan\Documents\Arduino\libraries\RF24-master/RF24_config.h:149:0: warning: “PSTR” redefined [enabled by default]

#define PSTR(x) (x)

^

In file included from C:\Users\Johan\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.12\cores\arduino/Arduino.h:31:0,

from sketch\Due_SubMenu.ino.cpp:1:

C:\Users\Johan\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.12\cores\arduino/avr/pgmspace.h:34:0: note: this is the location of the previous definition

#define PSTR(str) (str)

^

In file included from C:\Users\Johan\Documents\Arduino\libraries\RF24-master/RF24.h:18:0,

from C:\Users\Johan\Documents\Arduino\Due_SubMenu\Due_SubMenu.ino:19:

C:\Users\Johan\Documents\Arduino\libraries\RF24-master/RF24_config.h:151:0: warning: “strlen_P” redefined [enabled by default]

#define strlen_P strlen

^

In file included from C:\Users\Johan\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.12\cores\arduino/Arduino.h:31:0,

from sketch\Due_SubMenu.ino.cpp:1:

C:\Users\Johan\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.12\cores\arduino/avr/pgmspace.h:69:0: note: this is the location of the previous definition

#define strlen_P(a) strlen((a))

^

In file included from C:\Users\Johan\Documents\Arduino\libraries\RF24-master/RF24.h:18:0,

from C:\Users\Johan\Documents\Arduino\Due_SubMenu\Due_SubMenu.ino:19:

C:\Users\Johan\Documents\Arduino\libraries\RF24-master/RF24_config.h:153:0: warning: “pgm_read_word” redefined [enabled by default]

#define pgm_read_word(p) (*(p))

^

In file included from C:\Users\Johan\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.12\cores\arduino/Arduino.h:31:0,

from sketch\Due_SubMenu.ino.cpp:1:

C:\Users\Johan\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.12\cores\arduino/avr/pgmspace.h:103:0: note: this is the location of the previous definition

#define pgm_read_word(addr) (*(const unsigned short *)(addr))

^

C:\Users\Johan\Documents\Arduino\Due_SubMenu\Due_SubMenu.ino: In function ‘void PressButton()’:

Due_SubMenu:162: error: cannot convert ‘Adafruit_GFX_Button*’ to ‘Adafruit_GFX_Button**’ for argument ‘1’ to ‘bool update_button_list(Adafruit_GFX_Button**)’

update_button_list(active_buttons);

^

Due_SubMenu:165: error: cannot convert ‘Adafruit_GFX_Button* [4]’ to ‘Adafruit_GFX_Button*’ in assignment

active_buttons = page0_buttons;

^

Due_SubMenu:185: error: ‘active_button_list’ was not declared in this scope

active_button_list = page1_buttons;

^

Due_SubMenu:210: error: ‘active_button_list’ was not declared in this scope

active_button_list = page2_buttons;

^

Due_SubMenu:232: error: ‘active_button_list’ was not declared in this scope

active_button_list = page3_buttons;

^

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

Due_SubMenu.ino (12.4 KB)

I managed to get this to compile without any errors but the on screen buttons are still inoperative.

Due_SubMenu.ino (12.4 KB)

There are multiple instances of this that need fixed:

    if (on_btn11.justPressed()) {
      Data();
      currentPage == '1'; // ERROR: not an assignment
    }

arduarn: There are multiple instances of this that need fixed:

    if (on_btn11.justPressed()) {
      Data();
      currentPage == '1'; // ERROR: not an assignment
    }

Are you talking about the warning: deprecated conversion .......?

No, currentPage == '1' is a comparison, not an assignment as it should be. You should have = It's not the problem though, since you have correct assignments in the sub-functions.

What is actually happening when you tap a button? 1. nothing. 2. button changes visual state but page doesn't change. 3. button doesn't change visual state but page changes.

fnb111: https://youtu.be/lwKE07nNTfY

Members The video explain it much better than I can. Any ideas on what might cause this and how to fix it would be appreciated.

The sketch is attached.

Thanks

has the original sketch attached. I can confirm that this is correct and working correctly at the time. Your question to the most recent post:

answer is 1

Strange. It looks like it should work to me.

While pondering your code, I made a few cosmetic tweaks and a couple of small structural ones. Nothing that should really explain the behaviour you are seeing. The changes are in the attachment, you can decide if they make sense or not.

I also attempted to test the attached version. By adding:

  down = !digitalRead(2);
  pixel_x = 122; pixel_y = 200;

at the beginning of update_button(), I was able to simulate a touch event on the On/Off button. It seemed to work OK.

If this doesn’t work for you, re-test with the code from post #1 again to make sure there is not something amiss with the hardware - loose wire or something. And if still no joy, go large with println().

Due_SubMenu_XXX.ino (11.3 KB)

The _XXX version works 100%. The way you programmed this makes sense and is easy to understand.

Thanks a lot.