Adafruit GFX buttons On/Off (display only one)?

Members
I get an error when I try to display only one button at a time on the TFT. If I press ‘On’ then ‘Off’ should appear & vice versa.

/*
   DUE_OnOff_multiUnits
  TX &TFT &nRF 
  sub menu buttons
  unit with 2 leds can be switched via nRF
 

*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>

//Touchscreen X+ X- Y+ Y- pins
#define YP A5
#define XM A4
#define YM 10
#define XP 11
//tested as correct for new ADAFRUIT screen
#define TS_MINX 150
#define TS_MINY 90
#define TS_MAXX 900
#define TS_MAXY 800

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define BLACK       0x0000
#define BLUE        0x001F
#define CYAN        0x07FF
#define DARKGREEN   0x03E0
#define DARKCYAN    0x03EF
#define DARKGREY    0x7BEF
#define GREEN       0x07E0
#define GREENYELLOW 0xB7E0
#define LIGHTGREY   0xC618
#define MAGENTA     0xF81F
#define MAROON      0x7800
#define NAVY        0x000F
#define OLIVE       0x7BE0
#define ORANGE      0xFDA0
#define PINK        0xFC9F
#define PURPLE      0x780F
#define RED         0xF800
#define WHITE       0xFFFF
#define YELLOW      0xFFE0

#define MINPRESSURE 10
#define MAXPRESSURE 1000
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button on_btn, off_btn, on_btn1, off_btn1,
                    on_btn2, off_btn2, on_btn3, off_btn3,
                    on_btn4, off_btn4, on_btn5, off_btn5;
//      DUE pins ................CHECK
#define CE_PIN 50  //confirm pin connections for nrf
#define CSN_PIN 51
RF24 radio(CE_PIN, CSN_PIN);

const byte slaveAddress1[5] = {'R', 'x', 'A', 'A', 'A'}; //need address for each slave 1
const byte slaveAddress2[5] = {'R', 'x', 'A', 'A', 'B'}; //need address for each slave 2
const byte slaveAddress3[5] = {'R', 'x', 'A', 'A', 'C'}; //need address for each slave 3
const byte slaveAddress4[5] = {'R', 'x', 'A', 'A', 'D'}; //need address for each slave 4
const byte slaveAddress5[5] = {'R', 'x', 'A', 'A', 'E'}; //need address for each slave 5
const byte slaveAddress6[5] = {'R', 'x', 'A', 'A', 'F'}; //need address for each slave 6

int msg[1];
int pixel_x, pixel_y;

bool Touch_getXY(void)
{
  TSPoint p = ts.getPoint();
  bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
  if (pressed) {
    pixel_x = map(p.y, TS_MAXX, TS_MINX, tft.width(), 0);
    pixel_y = map(p.x, TS_MAXY, TS_MINY, 0, tft.height());
    //Serial.println( pixel_x);
    //Serial.println( pixel_y);
  }
  return pressed;
}

void setup(void) {
  Serial.begin(9600);
  radio.begin();
  uint16_t ID = tft.readID();
  tft.begin(ID);
  tft.setRotation(1);
  tft.fillScreen(BLACK);
  OnOffGraphics ();
}

void OnOffGraphics () {
  on_btn.initButton(&tft,  230, 15, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
  off_btn.initButton(&tft, 285, 15, 50, 30, CYAN, RED, BLACK, "OFF", 1);
  if (on_btn.drawButton(false)) else (off_btn.drawButton(true));
  on_btn1.initButton(&tft,  230, 50, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
  off_btn1.initButton(&tft, 285, 50, 50, 30, CYAN, RED, BLACK, "OFF", 1);
  on_btn1.drawButton(false);
  off_btn1.drawButton(false);
  on_btn2.initButton(&tft,  230, 85, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
  off_btn2.initButton(&tft, 285, 85, 50, 30, CYAN, RED, BLACK, "OFF", 1);
  on_btn2.drawButton(false);
  off_btn2.drawButton(false);
  on_btn3.initButton(&tft,  230, 120, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
  off_btn3.initButton(&tft, 285, 120, 50, 30, CYAN, RED, BLACK, "OFF", 1);
  on_btn3.drawButton(false);
  off_btn3.drawButton(false);
  on_btn4.initButton(&tft,  230, 155, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
  off_btn4.initButton(&tft, 285, 155, 50, 30, CYAN, RED, BLACK, "OFF", 1);
  on_btn4.drawButton(false);
  off_btn4.drawButton(false);
  on_btn5.initButton(&tft,  230, 190, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
  off_btn5.initButton(&tft, 285, 190, 50, 30, CYAN, RED, BLACK, "OFF", 1);
  on_btn5.drawButton(false);
  off_btn5.drawButton(false);

}

// Array of button addresses to behave like a list
Adafruit_GFX_Button *buttons[] = {&on_btn, &off_btn, &on_btn1, &off_btn1,
                                  &on_btn2, &off_btn2, &on_btn3, &off_btn3,
                                  &on_btn4, &off_btn4, &on_btn5, &off_btn5 , NULL
                                 };


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;
}

void loop(void) {
  PressButton();
}

void PressButton() {
  update_button_list(buttons);     //2 buttons & leds / unit
  radio.openWritingPipe(slaveAddress1);
  if (on_btn.justPressed()) {       // unit 1
    msg[0] = 001;                  // LED0=on
    radio.write(msg, 1);
  }
  if (off_btn.justPressed()) {       // unit 1
    msg[0] = 000;                     // LED0=off
    radio.write(msg, 1);
  }
  if (on_btn1.justPressed()) {    // unit 1
    msg[0] = 101;                 // LED1=on
    radio.write(msg, 1);          // transmitt button two is pressed

  }
  if (off_btn1.justPressed()) {    // unit 1
    msg[0] = 100;                 // LED1=off
    radio.write(msg, 1);          // transmitt button two is pressed
  }
  radio.openWritingPipe(slaveAddress6);
  if (on_btn2.justPressed()) {       // unit 2
    msg[0] = 001;                  // LED0=on
    radio.write(msg, 1);
  }
  if (off_btn2.justPressed()) {       // unit 2
    msg[0] = 000;                     // LED0=off
    radio.write(msg, 1);
  }
  if (on_btn3.justPressed()) {    // unit 2
    msg[0] = 101;                 // LED1=on
    radio.write(msg, 1);          // transmitt button two is pressed

  }
  if (off_btn3.justPressed()) {    // unit 2
    msg[0] = 100;                 // LED1=off
    radio.write(msg, 1);          // transmitt button two is pressed
  }
}

This line

 if (on_btn.drawButton(false)) else (off_btn.drawButton(true));

Arduino: 1.8.12 (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 last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:12:

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_OnOff_multiUnits.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 last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:12:

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_OnOff_multiUnits.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 last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:12:

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_OnOff_multiUnits.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 last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino: In function 'void OnOffGraphics()':

C:\Users\Johan\Documents\Arduino last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:100:72: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

on_btn.initButton(&tft, 230, 15, 50, 30, CYAN, GREEN, BLACK, "ON", 1);

^

C:\Users\Johan\Documents\Arduino last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:101:71: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

off_btn.initButton(&tft, 285, 15, 50, 30, CYAN, RED, BLACK, "OFF", 1);

^

DUE_OnOff_multiUnits:102:30: error: could not convert 'on_btn.Adafruit_GFX_Button::drawButton(false)' from 'void' to 'bool'

if (on_btn.drawButton(false)) else (off_btn.drawButton(false));

^

DUE_OnOff_multiUnits:102:33: error: expected primary-expression before 'else'

if (on_btn.drawButton(false)) else (off_btn.drawButton(false));

^

DUE_OnOff_multiUnits:102:33: error: expected ';' before 'else'

C:\Users\Johan\Documents\Arduino last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:103:73: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

on_btn1.initButton(&tft, 230, 50, 50, 30, CYAN, GREEN, BLACK, "ON", 1);

^

C:\Users\Johan\Documents\Arduino last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:104:72: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

off_btn1.initButton(&tft, 285, 50, 50, 30, CYAN, RED, BLACK, "OFF", 1);

^

C:\Users\Johan\Documents\Arduino last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:107:73: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

on_btn2.initButton(&tft, 230, 85, 50, 30, CYAN, GREEN, BLACK, "ON", 1);

^

C:\Users\Johan\Documents\Arduino last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:108:72: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

off_btn2.initButton(&tft, 285, 85, 50, 30, CYAN, RED, BLACK, "OFF", 1);

^

C:\Users\Johan\Documents\Arduino last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:111:74: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

on_btn3.initButton(&tft, 230, 120, 50, 30, CYAN, GREEN, BLACK, "ON", 1);

^

C:\Users\Johan\Documents\Arduino last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:112:73: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

off_btn3.initButton(&tft, 285, 120, 50, 30, CYAN, RED, BLACK, "OFF", 1);

^

C:\Users\Johan\Documents\Arduino last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:115:74: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

on_btn4.initButton(&tft, 230, 155, 50, 30, CYAN, GREEN, BLACK, "ON", 1);

^

C:\Users\Johan\Documents\Arduino last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:116:73: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

off_btn4.initButton(&tft, 285, 155, 50, 30, CYAN, RED, BLACK, "OFF", 1);

^

C:\Users\Johan\Documents\Arduino last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:119:74: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

on_btn5.initButton(&tft, 230, 190, 50, 30, CYAN, GREEN, BLACK, "ON", 1);

^

C:\Users\Johan\Documents\Arduino last\OnOff both DUE UNO\DUE_OnOff_multiUnits\DUE_OnOff_multiUnits.ino:120:73: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

off_btn5.initButton(&tft, 285, 190, 50, 30, CYAN, RED, BLACK, "OFF", 1);

^

exit status 1
could not convert 'on_btn.Adafruit_GFX_Button::drawButton(false)' from 'void' to 'bool'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

How can I write this so it only display the opposite button too the one that was pressed?

Thanks for the help in advance.

Am I correct if I say that

 if (b->justPressed())
    b->drawButton(true);
  return down;

is suppose to NOT draw the button color after a press?

Still here.

So I tried declaring globals

bool tbtn = 0;
bool fbtn = 0;

and change the code

 on_btn.initButton(&tft,  230, 15, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
  off_btn.initButton(&tft, 285, 15, 50, 30, CYAN, RED, BLACK, "OFF", 1);
  if (tbtn = false)(fbtn = true);
  else(fbtn = false);
  on_btn.drawButton(tbtn);
  off_btn.drawButton(fbtn);

no!

I’ve changed the code so that the ‘On’ buttons is displayed at startup. When I press the ‘On’ button it goes dark( that’s what I would like to see) BUT how do I connect the ‘Off’ button to work in the opposite way?

/*
   DUE_OnOff_multiUnits1
  TX &TFT &nRF
  sub menu buttons
  unit with 2 leds can be switched via nRF
  25Dec2018
  2 Jan 2019

*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>

//Touchscreen X+ X- Y+ Y- pins
#define YP A5
#define XM A4
#define YM 10
#define XP 11
//tested as correct for new ADAFRUIT screen
#define TS_MINX 150
#define TS_MINY 90
#define TS_MAXX 900
#define TS_MAXY 800

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define BLACK       0x0000
#define BLUE        0x001F
#define CYAN        0x07FF
#define DARKGREEN   0x03E0
#define DARKCYAN    0x03EF
#define DARKGREY    0x7BEF
#define GREEN       0x07E0
#define GREENYELLOW 0xB7E0
#define LIGHTGREY   0xC618
#define MAGENTA     0xF81F
#define MAROON      0x7800
#define NAVY        0x000F
#define OLIVE       0x7BE0
#define ORANGE      0xFDA0
#define PINK        0xFC9F
#define PURPLE      0x780F
#define RED         0xF800
#define WHITE       0xFFFF
#define YELLOW      0xFFE0

#define MINPRESSURE 10
#define MAXPRESSURE 1000

#define DEBOUNCE 10// button debouncer, how many ms to debounce, 5+ ms is usually plenty

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button on_btn, off_btn, on_btn1, off_btn1,
                    on_btn2, off_btn2, on_btn3, off_btn3,
                    on_btn4, off_btn4, on_btn5, off_btn5;
//      DUE pins ................CHECK
#define CE_PIN 50  //confirm pin connections for nrf
#define CSN_PIN 51
RF24 radio(CE_PIN, CSN_PIN);
//need address for each slave
const byte slaveAddress1[5] = {'R', 'x', 'A', 'A', 'A'};
const byte slaveAddress2[5] = {'R', 'x', 'A', 'A', 'B'};
const byte slaveAddress3[5] = {'R', 'x', 'A', 'A', 'C'};
const byte slaveAddress4[5] = {'R', 'x', 'A', 'A', 'D'};
const byte slaveAddress5[5] = {'R', 'x', 'A', 'A', 'E'};
const byte slaveAddress6[5] = {'R', 'x', 'A', 'A', 'F'};
bool tbtn = 1;
bool fbtn = 0;
int msg[1];
int pixel_x, pixel_y;

bool Touch_getXY(void)
{
  TSPoint p = ts.getPoint();
  bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
  if (pressed) {
    pixel_x = map(p.y, TS_MAXX, TS_MINX, tft.width(), 0);
    pixel_y = map(p.x, TS_MAXY, TS_MINY, 0, tft.height());
    //Serial.println( pixel_x);
    //Serial.println( pixel_y);
  }
  return pressed;
}

void setup(void) {
  Serial.begin(9600);
  radio.begin();
  uint16_t ID = tft.readID();
  tft.begin(ID);
  tft.setRotation(1);
  tft.fillScreen(BLACK);
  OnOffGraphics ();
}

void OnOffGraphics () {
  on_btn.initButton(&tft,  230, 15, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
  off_btn.initButton(&tft, 285, 15, 50, 30, CYAN, RED, BLACK, "OFF", 1);
  on_btn.drawButton(false);
  off_btn.drawButton(true);
  on_btn1.initButton(&tft,  230, 50, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
  off_btn1.initButton(&tft, 285, 50, 50, 30, CYAN, RED, BLACK, "OFF", 1);
  on_btn1.drawButton(false);
  off_btn1.drawButton(true);
  on_btn2.initButton(&tft,  230, 85, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
  off_btn2.initButton(&tft, 285, 85, 50, 30, CYAN, RED, BLACK, "OFF", 1);
  on_btn2.drawButton(false);
  off_btn2.drawButton(true);
  on_btn3.initButton(&tft,  230, 120, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
  off_btn3.initButton(&tft, 285, 120, 50, 30, CYAN, RED, BLACK, "OFF", 1);
  on_btn3.drawButton(false);
  off_btn3.drawButton(true);
  on_btn4.initButton(&tft,  230, 155, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
  off_btn4.initButton(&tft, 285, 155, 50, 30, CYAN, RED, BLACK, "OFF", 1);
  on_btn4.drawButton(false);
  off_btn4.drawButton(true);
  on_btn5.initButton(&tft,  230, 190, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
  off_btn5.initButton(&tft, 285, 190, 50, 30, CYAN, RED, BLACK, "OFF", 1);
  on_btn5.drawButton(false);
  off_btn5.drawButton(true);

}

// Array of button addresses to behave like a list
Adafruit_GFX_Button *buttons[] = {&on_btn, &off_btn, &on_btn1, &off_btn1,
                                  &on_btn2, &off_btn2, &on_btn3, &off_btn3,
                                  &on_btn4, &off_btn4, &on_btn5, &off_btn5 , NULL
                                 };


bool update_button(Adafruit_GFX_Button *b, bool down)
{
  b->press(down && b->contains(pixel_x, pixel_y));
  if (b->justPressed())
   b->drawButton(true);
  if (b->justReleased())
  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;
}

void loop(void) {
  PressButton();
}

void PressButton() {
  update_button_list(buttons);     //2 buttons & leds / unit
  radio.openWritingPipe(slaveAddress5);
  if (on_btn.justPressed()) {       // unit 1
    msg[0] = 001;                  // LED0=on
    radio.write(msg, 1);
  }
  if (off_btn.justPressed()) {       // unit 1
    msg[0] = 000;                     // LED0=off
    radio.write(msg, 1);
  }
  if (on_btn1.justPressed()) {    // unit 1
    msg[0] = 101;                 // LED1=on
    radio.write(msg, 1);          // transmit button two is pressed

  }
  if (off_btn1.justPressed()) {    // unit 1
    msg[0] = 100;                 // LED1=off
    radio.write(msg, 1);          // transmit button two is pressed
  }
  radio.openWritingPipe(slaveAddress6);
  if (on_btn2.justPressed()) {       // unit 2
    msg[0] = 001;                  // LED0=on
    radio.write(msg, 1);
  }
  if (off_btn2.justPressed()) {       // unit 2
    msg[0] = 000;                     // LED0=off
    radio.write(msg, 1);
  }
  if (on_btn3.justPressed()) {    // unit 2
    msg[0] = 101;                 // LED1=on
    radio.write(msg, 1);          // transmit button two is pressed

  }
  if (off_btn3.justPressed()) {    // unit 2
    msg[0] = 100;                 // LED1=off
    radio.write(msg, 1);          // transmit button two is pressed
  }
}

Any help or tips are appreciated. Thanks