Variable value being lost

Hi All,
Using Platformio in VSC, ESP32, with TFT touchscreen and TFT-eSPI, I have a touch screen with different pages (screens) selected by “Menu” buttons at the bottom of the display.
The pages have various buttons, to perform various control functions.
The system sends CAN messages, with various data.

At the top of my code (after #include <TFT_eSPI.h>, etc) I declare a variable uint8_t Started = 0;
On Page 1, Button[1] will make Started = 1, to start a motor and Button[2] will make Started = 0, to stop the motor again.
“Started” will also put text "Motor 1 - " and “ON” (or “OFF”) on the screen and control other functions to test the motor speed, etc.

void Buttons_Motor1() {
    uint16_t t_x = 0, t_y = 0;
    bool pressed = tft.getTouch(&t_x, &t_y);
    if (Button[1].contains(t_x, t_y)) {
         Started = 1;
         Motor1_text = "ON";
    }
    if (Button[2].contains(t_x, t_y)) {
         Started = 0;
         Motor1_text = "OFF";
    }
}

One CAN message confirms Started = 1 ok.
However, pressing the MENU buttons to a different page, then the CAN message states Started = 0.
Like the variable value is LOST ???

if I test “Started” in the CAN send routine, then “Started” remains at 1 in the CAN messages, on all pages.

void SendCAN_01() {               // Send a CAN message ID 01
    if (Motor1_text == "ON") {
           Started = 1;
    } else {
           Started = 0;
    }
    CAN.beginPacket(01);
    CAN.write(Started);          // byte 0
    CAN.endPacket();             // end of message .... DLC total 8 bytes
}

I’ve been through the whole code and the “Started” variable is not modofied anywhere else, only in the void Buttons_Motor1() function.
Can the “Started” variable value be lost somewhere ?
Thank you,
Trevor

This isn’t true because of this:

Started will be set to 0 everytime when SendCAN_01 is called and Motor1_text is not “ON”!

Hi Boris,
Sorry, I’ve explained that bit poorly.

void SendCAN_01() {               // Send a CAN message ID 01
    if (Motor1_text == "ON") {
           Started = 1;
    } else {
           Started = 0;
    }

This seems to be a cure for the problem, or maybe just masking the problem ?
Before I did that test in void SendCAN_01() , the variable “Started” was getting “lost” (= 0), when I change screen pages.
Surely, void Buttons_Motor1() sets Started = 1 when I press Button[1] and it should remain forever, unitl changed or a reset ?
Strange that “Started” can mysteriously become 0, but Motor1_text remains “ON” once setup.

Ok.

Probably this is your problem:

    if (Button[2].contains(t_x, t_y)) {
         Started = 0;
         Motor1_text = "OFF";
    }

I don’t know the touch API but you didn’t check for pressed! This if-block might also be executed, even if the button was not pressed, but any other button which is a the same xy position.

Check the example code:

Just to explain further, before I tried …

void SendCAN_01() {               // Send a CAN message ID 01
    if (Motor1_text == "ON") {
           Started = 1;
    } else {
           Started = 0;
    }

… when I went back to Page 1, the screen still showed "Motor 1 - ON", but mysteriously Started = 0, which would stop the motor over CAN. Very strange.

Thanks Boris,
I’ll try that (have used before “Pressed”, “justPressed”, “justReleased”, etc).
The MENU buttons that select Pages are across bottom edge of screen, so shouldn’t relate to Button[1] or Button[2]. And the “Motor 1 - ON” text is not changing, just the int value “Started”.
I’ll let you know.

I don’t believe that Started will be set to 0 magically.

Otherwise, check all lines in your code which modify the Started variable and add a log statement like Serial.printf("Started set to %d\r\n", Started);

Brilliant idea !!! thank you Boris.
Already, imediately after I press the MENU 2 button, Serial.print says “Started” is still 1 (OK).
But, once it’s drawn the new screen Page2, then Serial.print says “Started” is 0.
So, I’m moving the Serial.print around my code, to find where it changes.

Solved … You’re a Guru Wizard Boris !!!
I put the Serial.printf in various places in the code, to find where it goes wrong.
With it just after the function that draws the 4 Page buttons, I see the fault "Started set to 0", when I change away from Page 1.
For each Page, I clear the screen BLACK and redraw all the required buttons, including the Page buttons again at the bottom edge. I use a “Pointer” on each page button to indicate which Page I’m on. The page button for the current Page has a White pointer, the other page buttons have Black pointers (invisible).
I’ve declared uint16_t Pointer_Colour[4]; // coloured pointers on 4 page buttons (Black is invisible) at start up.
When I draw the 4 page buttons, I use Pointer_Colour[1] to Pointer_Colour[4].
But I now realise that uint16_t Pointer_Colour[4]; reserves 4 memory slots [0] to [3] … NOT [1] to [4].
When I draw the 4th button, with Pointer_Colour[4] … I’ve over-run the memory array, into some other memory … which obviously contains the “Started” variable value.
I could use Pointer_Colour[0] through Pointer_Colour[3], but that’s a bit confusing.

Declaring uint16_t Pointer_Colour[5]; // coloured pointers on 4 page buttons (Black is invisible) reserves 5 slots at start up, so Pointer_Colour[4] works fine now … problem cured !

Thank you again Boris.

1 Like

Just tidying up now and it seems my if (Button[2].contains(t_x, t_y)) { works OK, without any other code.
I’d already debounced the buttons with a millis timer, works fine.
Trevor