uint16_t will not set to zero

I have a problem with the below loop. It reads tx and ty values from the touch screen just fine, the IF argument works and the LoRaSend() loop sends a Hello message to the receiver if the screen is touched on the “Once” button.

I only want this button to loop once and so after the LoRaSend() loop I update the tx and ty values to zero with tx=0; and ty=0; but this doesn’t work. The IF statement remains true, it keeps looping over and over again and keeps repeatedly sending the LoRa message. If I touch the screen anywhere else it updates the tx and ty values and the IF loop stops but tx=0; and ty=0; do not work.

I was playing with the Serial.println command to try to figure this out and if I put any Serialprint command in the IF loop underneath tx=0; and ty=0; then they update to zero correctly fine but without the Serialprint command they don’t. Why not?

This Serial.print command doesn’t even have to read the tx or ty values, ie Serial.print(tx); because Serialprint(""); will also fix it. Why will tx and ty not update to zero without a Serialprint command?

The touch display is the standard ili9341 with the 2046 touch controller. I am using the tft-espi library.

void loop()
{
uint16_t tx, ty;

if (tft.getTouch(&tx, &ty))
{
// Draw a block spot to show where touch was calculated to be
#ifdef BLACK_SPOT
tft.fillCircle(tx, ty, 2, TFT_BLACK);
#endif
}

if ((tx > ONCE_X) && (tx < (ONCE_X + ONCE_W))) {
if ((ty > ONCE_Y) && (ty <= (ONCE_Y + ONCE_H))) {

LoRaSend();
tx = 0;
ty = 0;

Serial.println ("");

}
}

Please post your complete sketch

Please follow the advice on posting code given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

Setting them zero won't help, because the first thing the loop function does is to read them again from the display.

wildbill:
Setting them zero won't help, because the first thing the loop function does is to read them again from the display.

In addition to that, the variables are local to the loop() function so cannot be expected to retain their value after loop exits.

the variables are local to the loop() function so cannot be expected to retain their value after loop exits.

Unless they are declared as static

I think you may have wanted something like this:

void loop()
{
  static uint16_t old_x = 0, old_y = 0;
  uint16_t tx, ty;

  if (tft.getTouch(&tx, &ty))
  {
    // Act on the position only if it has changed
    if (tx != old_x || ty != old_y)
    {
      old_x = tx;
      old_y = ty;
      
      // Draw a block spot to show where touch was calculated to be
#ifdef BLACK_SPOT
      tft.fillCircle(tx, ty, 2, TFT_BLACK);
#endif


      // Act on the position only if it is in the box
      if ((tx > ONCE_X) && (tx < (ONCE_X + ONCE_W)))
      {
        if ((ty > ONCE_Y) && (ty <= (ONCE_Y + ONCE_H)))
        {
          LoRaSend();
        }
      }
    }
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.