Problem with serial print.

I have a simple program with a problem I can't find an answer to. When a button is pressed on the TFT screen it shows at the bottom. I would also like it to send that info to the serial monitor. The sketch as below does this but free runs with a new repeat message every few milliseconds. I want a single line display.
If I un select the // Serial.end(); lines it does correct for the first button pressed but then there is no further output. I (obviously) wrongly thought that the break; line reverted to the situation before the "case"
was selected.

Can anyone help as I am getting very dry.

// My TFT sketch
//#include <TouchScreen.h>
#include <UTFT.h>
#include <URTouch.h>
#include <UTFT_Buttons.h>
extern uint8_t BigFont[];

UTFT myGLCD(CTE70, 38, 39, 40, 41); // was UTFT myGLCD(CTE70, 25, 26, 27, 28)for Due;
URTouch myTouch(6, 5, 4, 3, 2);
UTFT_Buttons myButtons(&myGLCD, &myTouch);
const int redLed = 10;
const int greenLed = 9;
const int blueLed = 8;
int but1, but2, but3, but4, but5, but6, but7, pressed_button;
void setup()
{
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(BigFont);
myTouch.InitTouch();
myTouch.setPrecision(PREC_LOW);

myButtons.setTextFont(BigFont);
Serial.begin(9600);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);

}

void loop()
{
Serial.println("TEST TO MAKE SURE SERIAL WORKING");
boolean default_colors = false;
drawHomeScreen();
while (1)
{
if (myTouch.dataAvailable() == true)
pressed_button = myButtons.checkButtons();

switch (pressed_button) {
case 1:
myGLCD.print("Button 1", CENTER, 450);
Serial.println("Button 1");
//Serial.end();
break;
case 2:
myGLCD.print("Button 2", CENTER, 450);
Serial.println("Button 2");
//Serial.end();
break;
case 3:
myGLCD.print("Button 3", CENTER, 450);
Serial.println("Button 3");
// Serial.end();
break;
case 4:
myGLCD.print("Button 4", CENTER, 450);
Serial.println("Button 4");
// Serial.end();
break;
case 5:
myGLCD.print("Button 5", CENTER, 450);
Serial.println("Button 5");
// Serial.end();
break;
case 6:
myGLCD.print("Button 6", CENTER, 450);
Serial.println("Button 6");
//Serial.end();
break;
case 7:
myGLCD.print("Button 6", CENTER, 450);
Serial.println("Button 6");
// Serial.end();
break;

}
}
}
void drawHomeScreen() {
but1 = myButtons.addButton( 10, 5, 767, 40, "DOG HOUSE BAR");
but2 = myButtons.addButton( 10, 50, 767, 60, "Whiskey and Lemonade");
but3 = myButtons.addButton( 10, 115, 767, 60, "Gin and Tonic");
but4 = myButtons.addButton( 10, 180, 767, 60, "Rum and Coke");
but5 = myButtons.addButton( 10, 245, 767, 60, "Whiskey");
but6 = myButtons.addButton( 10, 310, 767, 60, "Tonic");
but7 = myButtons.addButton( 10, 375, 767, 60, "Coke");
myButtons.drawButtons();
}

Many thanks for reading this, and thanks for any advice>

Try printing only when the value of pressed_button changes ?

UKHeliBob:
Try printing only when the value of pressed_button changes ?

See the "State Change Example" for more information.

Hi Delta G,
Which example are you meaning, I have looked at several but none are relevant.
Many thanks for your help

PyrusBoy:
Hi Delta G,
Which example are you meaning, I have looked at several but none are relevant.
Many thanks for your help

StateChangeDetection in the 02.Digital section

Hi again,

This is one I had already tried, it still printed out multi times on serial, changed when button pressed but just went on printing the same line.
This is the sketch I used:

// My TFT sketch
//#include <TouchScreen.h>
#include <UTFT.h>
#include <URTouch.h>
#include <UTFT_Buttons.h>
extern uint8_t BigFont[];

UTFT myGLCD(CTE70, 38, 39, 40, 41); // was UTFT myGLCD(CTE70, 25, 26, 27, 28)for Due;
URTouch myTouch(6, 5, 4, 3, 2);
UTFT_Buttons myButtons(&myGLCD, &myTouch);
const int redLed = 10;
const int greenLed = 9;
const int blueLed = 8;
int but1, but2, but3, but4, but5, but6, but7, pressed_button;
int buttonState = 0;
int lastbuttonState = 0;
int buttonCount = 0;

void setup()
{
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(BigFont);
myTouch.InitTouch();
myTouch.setPrecision(PREC_LOW);
myButtons.setTextFont(BigFont);
Serial.begin(9600);
//pinMode(redLed, OUTPUT);
//pinMode(greenLed, OUTPUT);
//pinMode(blueLed, OUTPUT);
}

void loop()
{
Serial.println("Test");
boolean default_colors = false;
drawHomeScreen();
while (1)
{
if (myTouch.dataAvailable() == true)
pressed_button = myButtons.checkButtons();

switch (pressed_button) {
case 1:
myGLCD.print("Button 1", CENTER, 450);
Serial.println("Button 1");
buttonState = 1;
buttonNumber();
break;
case 2:
myGLCD.print("Button 2", CENTER, 450);
Serial.println("Button 2");
buttonState = 2;
buttonNumber();
break;
case 3:
myGLCD.print("Button 3", CENTER, 450);
Serial.println("Button 3");
buttonState = 3;
buttonNumber();
break;
case 4:
myGLCD.print("Button 4", CENTER, 450);
Serial.println("Button 4");
buttonState = 4;
buttonNumber();
break;
case 5:
myGLCD.print("Button 5", CENTER, 450);
Serial.println("Button 5");
buttonState = 5;
buttonNumber();
break;
case 6:
myGLCD.print("Button 6", CENTER, 450);
Serial.println("Button 6");
buttonState = 6;
buttonNumber();
break;
case 7:
myGLCD.print("Button 6", CENTER, 450);
Serial.println("Button 6");
buttonState = 7;
buttonNumber();
break;
}
}
}
void drawHomeScreen() {
but1 = myButtons.addButton( 10, 5, 767, 40, "DOG HOUSE BAR");
but2 = myButtons.addButton( 10, 50, 767, 60, "Whiskey and Lemonade");
but3 = myButtons.addButton( 10, 115, 767, 60, "Gin and Tonic");
but4 = myButtons.addButton( 10, 180, 767, 60, "Rum and Coke");
but5 = myButtons.addButton( 10, 245, 767, 60, "Whiskey");
but6 = myButtons.addButton( 10, 310, 767, 60, "Tonic");
but7 = myButtons.addButton( 10, 375, 767, 60, "Coke");
myButtons.drawButtons();
}
void buttonNumber() {
if (buttonState != lastbuttonState){
Serial.println(buttonState);
}
delay(100);
lastbuttonState = buttonState;
}

It seems ironic that sometimes it is hard to get something to happen, then you get something which is happening too often.. At 75 years old it makes the old grey matter work overtime.

All the best and thank you for your help.

Done it, it was my fault I hadn't taken out the serial.print on every button.

Many thanks

I was going to have a shot at debugging, but the style is too far different from mine !
Here's the OP code in a block

// My TFT sketch
//#include <TouchScreen.h>
#include <UTFT.h>
#include <URTouch.h>
#include <UTFT_Buttons.h>
extern uint8_t BigFont[];

UTFT myGLCD(CTE70, 38, 39, 40, 41); // was UTFT myGLCD(CTE70, 25, 26, 27, 28)for Due;
URTouch        myTouch(6, 5, 4, 3, 2);
UTFT_Buttons  myButtons(&myGLCD, &myTouch);
const int redLed = 10;
const int greenLed = 9;
const int blueLed = 8;
int but1, but2, but3, but4, but5, but6, but7, pressed_button;
int buttonState = 0;
int lastbuttonState = 0;
int buttonCount = 0;

void setup()
{
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_LOW);
  myButtons.setTextFont(BigFont);
  Serial.begin(9600);
  //pinMode(redLed, OUTPUT);
  //pinMode(greenLed, OUTPUT);
  //pinMode(blueLed, OUTPUT);
}

void loop()
{
  Serial.println("Test");
  boolean default_colors = false;
  drawHomeScreen();
  while (1)
  {
    if (myTouch.dataAvailable() == true)
      pressed_button = myButtons.checkButtons();

    switch (pressed_button) {
      case 1:
        myGLCD.print("Button 1", CENTER, 450);
        Serial.println("Button 1");
        buttonState = 1;
        buttonNumber();
        break;
      case 2:
        myGLCD.print("Button 2", CENTER, 450);
        Serial.println("Button 2");
        buttonState = 2;
        buttonNumber();
        break;
      case 3:
        myGLCD.print("Button 3", CENTER, 450);
        Serial.println("Button 3");
        buttonState = 3;
        buttonNumber();
        break;
      case 4:
        myGLCD.print("Button 4", CENTER, 450);
        Serial.println("Button 4");
        buttonState = 4;
        buttonNumber();
        break;
      case 5:
        myGLCD.print("Button 5", CENTER, 450);
        Serial.println("Button 5");
        buttonState = 5;
        buttonNumber();
        break;
      case 6:
        myGLCD.print("Button 6", CENTER, 450);
        Serial.println("Button 6");
        buttonState = 6;
        buttonNumber();
        break;
      case 7:
        myGLCD.print("Button 6", CENTER, 450);
        Serial.println("Button 6");
        buttonState = 7;
        buttonNumber();
        break;
    }
  }
}
void drawHomeScreen() {
  but1 = myButtons.addButton( 10,   5, 767, 40, "DOG HOUSE BAR");
  but2 = myButtons.addButton( 10,  50, 767, 60, "Whiskey and Lemonade");
  but3 = myButtons.addButton( 10, 115, 767, 60, "Gin and Tonic");
  but4 = myButtons.addButton( 10, 180, 767, 60, "Rum and Coke");
  but5 = myButtons.addButton( 10, 245, 767, 60, "Whiskey");
  but6 = myButtons.addButton( 10, 310, 767, 60, "Tonic");
  but7 = myButtons.addButton( 10, 375, 767, 60, "Coke");
  myButtons.drawButtons();
}
void buttonNumber() {  
  if (buttonState != lastbuttonState){
    Serial.println(buttonState);
      }
      delay(100);
   lastbuttonState = buttonState;
}

My feeling is that you're handling the button data too much.
That coiuld be simplified by losing and rethinking your strategy

Now reduce the amount of code by sensibly using the value of pressed_button, noting that buttonState is set to equal it, so why not just use the original value ? and that the text printed on the LCD is always the same apart from the button number, which you already have in the variable.

You could build the string to be printed using sprintf() or a combination of other functions. The switch/case is not needed

Something like this - UNTESTED

char buffer[20];  //an array to hold the text
sprintf(buffer, "Button %d", pressed_button);  //build the string
myGLCD.print(buffer, CENTER, 450);  //print the string