While (true) breaks sketch

I don't think that the while statement is 'required' as my sketch works without it, but I would like to understand more about it.... and why it breaks my sketch?! :frowning:

This is the program, please excuse how rough it is, I have not gone in and cleaned up old information from the UNO I was originally working on, nor have I removed all of the 16x2 LCD info, I have quite a bit of crap commented out.

That said, I am not a programmer by any stretch! I've liberally borrowed from examples to get where I'm at now.
(that means I'll listen to any advice anyone is willing to give!)

The post will be broken into three parts as it was too large to fit in one.

The next post has the full code.

The post after that will have the specific question that I have about the 'while()' statement.

edit: sorry for the full code post, I even had to remove my section breaks to be able to post it, so now it looks even worse than it did :frowning:

  /*-----( Import needed libraries )-----*/
#include <dht.h>
#include <Wire.h>
#include <UTFT.h>
#include <UTFT_Geometry.h>
#include <UTouch.h>

// Initialize display
UTFT    myGLCD(ITDB32S,38,39,40,41);
UTFT_Geometry geo(&myGLCD);

// Initialize touchscreen
UTouch  myTouch( 6, 5, 4, 3, 2);

// Declare fonts
extern uint8_t BigFont[];
extern uint8_t SmallFont[];

//Touch Screen X,Y
int x, y;
char stCurrent[20]="";
int stCurrentLen=0;
char stLast[20]="";

//Declare Temp Sensor
#define DHT22PIN 15          //DHT22 sensor
dht DHT22;

//Thermosistor
#define THERMISTORPIN A0    //Thermosistor
#define NUMSAMPLES 5
#define SERIESRESISTOR 10000
#define THERMISTORNOMINAL 10000  // resistance at 25 degrees C
#define TEMPERATURENOMINAL 25    // temp. for nominal resistance (almost always 25 C)
#define BCOEFFICIENT 3950        // The beta coefficient of the thermistor (usually 3000-4000)

int samples[NUMSAMPLES];

/* -- COMMENTED OUT FOR TOUCH SCREEN TESTING --
//switch for offset adjustment
const int SW1pin = 16;       // Pushbutton switch, raise setpoint
const int SW2pin = 17;       // Pushbutton switch, lower setpoint
int SW1state = 0;           // Current reading of SW1
int SW2state = 0;           // Current reading of SW2
*/
double offset = 2.0;        //setPoint adjustment

//const int TEC1on = 18;            //TEC 1 output
//const int TEC2on = 19;            //TEC 2 output

//************Custom Functions*******************//
void drawButtons()
{
  //Up Button
  myGLCD.setColor(112, 138, 144);
  myGLCD.fillRoundRect (250, 10, 310, 110);
  myGLCD.setColor(0, 0, 255);
  myGLCD.drawRoundRect (250, 10, 310, 110);
  myGLCD.setColor(255, 0, 0);
  geo.drawTriangle(255,75,305,75,280,35);
  geo.fillTriangle(255,75,305,75,280,35);
  //Down Button
  myGLCD.setColor(112, 138, 144);
  myGLCD.fillRoundRect (250, 130, 310, 230);
  myGLCD.setColor(0, 0, 255);
  myGLCD.drawRoundRect (250, 130, 310, 230);
  myGLCD.setColor(50, 50, 255);
  geo.drawTriangle(255,165,305,165,280,205);
  geo.fillTriangle(255,165,305,165,280,205);  
}

// When called draws a red frame while a button is touched
void waitForIt(int x1, int y1, int x2, int y2)
{
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
  while (myTouch.dataAvailable())
    myTouch.read();
  myGLCD.setColor(0, 0, 255);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
}

void drawFrame()
{
  myGLCD.setColor(0,0,255);
  myGLCD.drawLine(0,0,0,147);
  myGLCD.drawLine(0, 0, 200, 0);
  myGLCD.drawLine(0, 27, 200, 27);
  myGLCD.drawLine(0, 57, 200, 57);
  myGLCD.drawLine(0, 87, 200, 87);
  myGLCD.drawLine(0, 117, 200, 117);
  myGLCD.drawLine(0, 147, 200, 147);
  myGLCD.drawLine(120, 0, 120, 147);
  myGLCD.drawLine(200, 0, 200, 147);  
}

//****************End of Custom Functions*************//

void setup()
{
  Serial.begin(9600);  
  
  //Touch Screen Initialization
  myGLCD.InitLCD();
  myGLCD.clrScr();

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  myGLCD.setFont(BigFont);
  //myGLCD.setBackColor(0, 0, 255);
  drawButtons();
  drawFrame();
  
  /* -- COMMENTED OUT FOR TOUCH SCREEN TESTING --
  //switch for offset adjustment
  pinMode(SW1pin, INPUT_PULLUP);  // Enable pull-ups on switches
  pinMode(SW2pin, INPUT_PULLUP);
  
  //TEC enable pins
  pinMode(TEC1on, OUTPUT);
  pinMode(TEC2on, OUTPUT);
  */
  
  //Thermosistor
  // connect AREF to 3.3V and use that as VCC, less noise
  analogReference(EXTERNAL);      //Telling the Arduino to use voltage applied to AREF pin as reference
  
}


void loop()
{ 
    if (myTouch.dataAvailable())
    {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
     
      Serial.print(F("Touch Screen X ")); 
      Serial.println(x);
      Serial.print(F("Touch Screen Y ")); 
      Serial.println(y);
      
      if ((x>=248) && (x<=315))  // Upper row
      {
        if ((y>=5) && (y<=105))  // Button: UP
        {
          waitForIt(250, 10, 310, 110);
          offset++;          
        }
        if ((y>=135) && (y<=315))  // Button: DOWN
        {
          waitForIt(250, 130, 310, 230);
          offset--;
        }
      }
    }

  
  /*  -- COMMENTED OUT FOR TOUCH SCREEN TESTING --
  //Offest switch instructions
  // Check states of pushbuttons, if pressed change setpoint up or down    
  SW1state = digitalRead(SW1pin);
    if (SW1state == 0) 
      offset++;
  SW2state = digitalRead(SW2pin);  
    if (SW2state == 0) 
      offset--; 
  */
  
  int chk = DHT22.read(DHT22PIN);
 
 //Thermosistor Instructions for A/D temp 
     uint8_t i;
  float average;

  // take N samples in a row, with a slight delay
  for (i=0; i< NUMSAMPLES; i++) 
  {
   samples[i] = analogRead(THERMISTORPIN);
   delay(10);
  }
 
  // average all the samples out
  average = 0;
  for (i=0; i< NUMSAMPLES; i++) 
  {
     average += samples[i];
  }
  
  average /= NUMSAMPLES;
 
  Serial.print(F("Average analog reading ")); 
  Serial.println(average);
 
  // convert the value to resistance
  average = 1023 / average - 1;
  average = SERIESRESISTOR / average;
  Serial.print(F("Thermistor resistance ")); 
  Serial.println(average);
 
  float steinhart;
  steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // convert to C
  
  /* -- COMMENTED OUT, NEED PWM ADJUSTMENT NOT ON/OFF
  //TEC instructions
  if (steinhart > (setPoint(DHT22.temperature, DHT22.humidity))) 
  {
    digitalWrite(TEC1on, HIGH);
  } 
  else 
  {
    digitalWrite(TEC1on, LOW); 
  }
  
  if ((steinhart - 2) > (setPoint(DHT22.temperature, DHT22.humidity))) 
  {
    digitalWrite(TEC2on, HIGH);
  } 
  else 
  {
    digitalWrite(TEC2on, LOW); 
  }
  */
  
  Serial.print(F("Read sensor: "));
  switch (chk)
  {
    case 0: Serial.println("OK"); break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  }
  
  myGLCD.setColor(255,255,255);
  
  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Case Temp(C):"),10, 7);
  myGLCD.setFont(BigFont);
  myGLCD.printNumI((float)DHT22.temperature, 125, 5);
  Serial.print(F("Temperature (C): "));
  Serial.println((float)DHT22.temperature, 2);

  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Humidity (%):"),10, 37);
  myGLCD.setFont(BigFont);
  myGLCD.printNumI((float)DHT22.humidity, 125, 35);
  Serial.print(F("Humidity (%): "));
  Serial.println((float)DHT22.humidity, 2);

  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Res Temp (C):"),10, 67);
  myGLCD.setFont(BigFont);
  if (steinhart < 10 && steinhart > 0) myGLCD.print(" ", 140, 65);
  myGLCD.printNumI(steinhart, 125, 65);
  Serial.print(F("Reservoir (C): "));
  Serial.println(steinhart, 2);  
  
  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Dew Point(C):"),10, 97);
  myGLCD.setFont(BigFont);
  if (dewPointFast(DHT22.temperature, DHT22.humidity) < 10 && dewPointFast(DHT22.temperature, DHT22.humidity) > 0) myGLCD.print(" ", 140, 95);
  myGLCD.printNumI(dewPointFast(DHT22.temperature, DHT22.humidity), 125, 95);
  Serial.print(F("Dew PointFast (C): "));
  Serial.println(dewPointFast(DHT22.temperature, DHT22.humidity));
  
  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Set Point(C):"),10, 127);
  myGLCD.setFont(BigFont);
  if (setPoint(DHT22.temperature, DHT22.humidity) < 10 && setPoint(DHT22.temperature, DHT22.humidity) > 0) myGLCD.print(" ", 140, 125);
  myGLCD.printNumI(setPoint(DHT22.temperature, DHT22.humidity), 125, 125);
  Serial.print(F("Set Point (C): "));
  Serial.println(setPoint(DHT22.temperature, DHT22.humidity));
}
//End Loop

//***Declare User-written Calculations ***

double dewPointFast(double celsius, double humidity)
{
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        return Td;
}
//Set Point for Reservoir, minimum offset temp = 2c
double setPoint(double celsius, double humidity)
{
        offset = constrain(offset, 1, 60);   //Limits offset to prevent going below Dew Point or too high
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        double Sp = Td + offset;
        if (Sp < 2) {return 2;}
        else {return Sp;}        
}
//End Program

If I take the very first section of the void loop()

if (myTouch.dataAvailable())
    {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
     
      Serial.print(F("Touch Screen X ")); 
      Serial.println(x);
      Serial.print(F("Touch Screen Y ")); 
      Serial.println(y);
      
      if ((x>=248) && (x<=315))  // Upper row
      {
        if ((y>=5) && (y<=105))  // Button: UP
        {
          waitForIt(250, 10, 310, 110);
          offset++;          
        }
        if ((y>=135) && (y<=315))  // Button: DOWN
        {
          waitForIt(250, 130, 310, 230);
          offset--;
        }
      }
    }

and add the while statement:

while (true)
{
  if (myTouch.dataAvailable())
    {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
     
      Serial.print(F("Touch Screen X ")); 
      Serial.println(x);
      Serial.print(F("Touch Screen Y ")); 
      Serial.println(y);
      
      if ((x>=248) && (x<=315))  // Upper row
      {
        if ((y>=5) && (y<=105))  // Button: UP
        {
          waitForIt(250, 10, 310, 110);
          offset++;          
        }
        if ((y>=135) && (y<=315))  // Button: DOWN
        {
          waitForIt(250, 130, 310, 230);
          offset--;
        }
      }
    }
  }

It breaks my entire sketch. The serial completely stops responding, nothing updates anywhere, the serial shows one quick X= -1, Y= -1 the first time I touch the screen, and then nothing.

Can anyone help explain if I should work more on that and include the 'while()' statement? If I should, can you point me in the right direction as to what I am doing wrong?

Let's ask this: If you do the same thing to a simple sketch, does it exhibit the same symptoms?

Please try it. If you get the same problem, then please post both simple sketches, with both of them complete. I'd like to try compiling them, and see what happens. I'd rather not have to hunt down any libraries, and I'd rather not have to guess which libraries you use.

Also. please say which Arduino you're using, and which version of the IDE.

Hi and thanks for the response!

I know that the 'while()' works in the button_test in the UTouch library (that's where I pulled the example from).

I am using an arduino Mega2560, with a TFT 3.2" QVT display SSD1289 controller. I am also using a TFT Mega shield, it is a knock off of Itead, but it does check out so far with buffers / data throughput and pinouts.

I am using is the UTFT, UTouch, UTFT_Geometery libraries from Henning Karlson.
http://www.rinkydinkelectronics.com/library.php

I'll see if I can simplify a sketch and remove everything non screen related.

Ok, here is a simplified version of the same code, everything but the screen functions and serial have been removed.

Without the while() statement it prints to the screen and I get serial responses. With the while() statement, I get only the setup functions (the lines being drawn), there are no loop functions(myGLCD.print).

The only other thing I can remove is the UTFT_Geometry library if you'd like.

broken:

#include <UTFT.h>
#include <UTFT_Geometry.h>
#include <UTouch.h>

// Initialize display
UTFT    myGLCD(ITDB32S,38,39,40,41);
UTFT_Geometry geo(&myGLCD);

// Initialize touchscreen
UTouch  myTouch( 6, 5, 4, 3, 2);

// Declare fonts
extern uint8_t BigFont[];
extern uint8_t SmallFont[];

//Touch Screen X,Y
int x, y;

//************Custom Functions*******************//
void drawButtons()
{
  //Up Button
  myGLCD.setColor(112, 138, 144);
  myGLCD.fillRoundRect (250, 10, 310, 110);
  myGLCD.setColor(0, 0, 255);
  myGLCD.drawRoundRect (250, 10, 310, 110);
  myGLCD.setColor(255, 0, 0);
  geo.drawTriangle(255,75,305,75,280,35);
  geo.fillTriangle(255,75,305,75,280,35);
  //Down Button
  myGLCD.setColor(112, 138, 144);
  myGLCD.fillRoundRect (250, 130, 310, 230);
  myGLCD.setColor(0, 0, 255);
  myGLCD.drawRoundRect (250, 130, 310, 230);
  myGLCD.setColor(50, 50, 255);
  geo.drawTriangle(255,165,305,165,280,205);
  geo.fillTriangle(255,165,305,165,280,205);  
}

// When called draws a red frame while a button is touched
void waitForIt(int x1, int y1, int x2, int y2)
{
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
  while (myTouch.dataAvailable())
    myTouch.read();
  myGLCD.setColor(0, 0, 255);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
}

void drawFrame()
{
  myGLCD.setColor(0,0,255);
  myGLCD.drawLine(0,0,0,147);
  myGLCD.drawLine(0, 0, 200, 0);
  myGLCD.drawLine(0, 27, 200, 27);
  myGLCD.drawLine(0, 57, 200, 57);
  myGLCD.drawLine(0, 87, 200, 87);
  myGLCD.drawLine(0, 117, 200, 117);
  myGLCD.drawLine(0, 147, 200, 147);
  myGLCD.drawLine(120, 0, 120, 147);
  myGLCD.drawLine(200, 0, 200, 147);  
}

//****************End of Custom Functions*************//

void setup()
{
  Serial.begin(9600);  
  
  //Touch Screen Initialization
  myGLCD.InitLCD();
  myGLCD.clrScr();

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  myGLCD.setFont(BigFont);
  //myGLCD.setBackColor(0, 0, 255);
  drawButtons();
  drawFrame();
}


void loop()
{
 while(true)
 {
    if (myTouch.dataAvailable())
    {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
     
      Serial.print(F("Touch Screen X ")); 
      Serial.println(x);
      Serial.print(F("Touch Screen Y ")); 
      Serial.println(y);
      
      if ((x>=248) && (x<=315))  // Upper row
      {
        if ((y>=5) && (y<=105))  // Button: UP
        {
          waitForIt(250, 10, 310, 110);
                    
        }
        if ((y>=135) && (y<=315))  // Button: DOWN
        {
          waitForIt(250, 130, 310, 230);
          
        }
      }
    }
 }
  myGLCD.setColor(255,255,255);
  
  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Case Temp(C):"),10, 7);
  

  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Humidity (%):"),10, 37);

  
  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Res Temp (C):"),10, 67);
 
  
  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Dew Point(C):"),10, 97);
  
  
  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Set Point(C):"),10, 127); 
 
}

working:

#include <UTFT.h>
#include <UTFT_Geometry.h>
#include <UTouch.h>

// Initialize display
UTFT    myGLCD(ITDB32S,38,39,40,41);
UTFT_Geometry geo(&myGLCD);

// Initialize touchscreen
UTouch  myTouch( 6, 5, 4, 3, 2);

// Declare fonts
extern uint8_t BigFont[];
extern uint8_t SmallFont[];

//Touch Screen X,Y
int x, y;

//************Custom Functions*******************//
void drawButtons()
{
  //Up Button
  myGLCD.setColor(112, 138, 144);
  myGLCD.fillRoundRect (250, 10, 310, 110);
  myGLCD.setColor(0, 0, 255);
  myGLCD.drawRoundRect (250, 10, 310, 110);
  myGLCD.setColor(255, 0, 0);
  geo.drawTriangle(255,75,305,75,280,35);
  geo.fillTriangle(255,75,305,75,280,35);
  //Down Button
  myGLCD.setColor(112, 138, 144);
  myGLCD.fillRoundRect (250, 130, 310, 230);
  myGLCD.setColor(0, 0, 255);
  myGLCD.drawRoundRect (250, 130, 310, 230);
  myGLCD.setColor(50, 50, 255);
  geo.drawTriangle(255,165,305,165,280,205);
  geo.fillTriangle(255,165,305,165,280,205);  
}

// When called draws a red frame while a button is touched
void waitForIt(int x1, int y1, int x2, int y2)
{
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
  while (myTouch.dataAvailable())
    myTouch.read();
  myGLCD.setColor(0, 0, 255);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
}

void drawFrame()
{
  myGLCD.setColor(0,0,255);
  myGLCD.drawLine(0,0,0,147);
  myGLCD.drawLine(0, 0, 200, 0);
  myGLCD.drawLine(0, 27, 200, 27);
  myGLCD.drawLine(0, 57, 200, 57);
  myGLCD.drawLine(0, 87, 200, 87);
  myGLCD.drawLine(0, 117, 200, 117);
  myGLCD.drawLine(0, 147, 200, 147);
  myGLCD.drawLine(120, 0, 120, 147);
  myGLCD.drawLine(200, 0, 200, 147);  
}

//****************End of Custom Functions*************//

void setup()
{
  Serial.begin(9600);  
  
  //Touch Screen Initialization
  myGLCD.InitLCD();
  myGLCD.clrScr();

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  myGLCD.setFont(BigFont);
  //myGLCD.setBackColor(0, 0, 255);
  drawButtons();
  drawFrame();
}


void loop()
{
    if (myTouch.dataAvailable())
    {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
     
      Serial.print(F("Touch Screen X ")); 
      Serial.println(x);
      Serial.print(F("Touch Screen Y ")); 
      Serial.println(y);
      
      if ((x>=248) && (x<=315))  // Upper row
      {
        if ((y>=5) && (y<=105))  // Button: UP
        {
          waitForIt(250, 10, 310, 110);
                    
        }
        if ((y>=135) && (y<=315))  // Button: DOWN
        {
          waitForIt(250, 130, 310, 230);
          
        }
      }
    }
 
  myGLCD.setColor(255,255,255);
  
  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Case Temp(C):"),10, 7);
  

  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Humidity (%):"),10, 37);

  
  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Res Temp (C):"),10, 67);
 
  
  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Dew Point(C):"),10, 97);
  
  
  myGLCD.setFont(SmallFont);
  myGLCD.print(F("Set Point(C):"),10, 127); 
 
}

The while loop NEVER exits and so the majority of your program will never execute.

I'm not sure I understand, shouldn't it exit when there is no data waiting on the touch screen? When you touch the screen it has data waiting so = true and enters the while() statement?

Here is the sample code from the UTouch library, it's quite possible I missed the ending statement...

// UTouch_ButtonTest 
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
// web: http://www.RinkyDinkElectronics.com/
//
// This program is a quick demo of how create and use buttons.
//
// This program requires the UTFT library.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin 
// numbers in the setup.
//

#include <UTFT.h>
#include <UTouch.h>

// Initialize display
// ------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield            : <display model>,19,18,17,16
// Standard Arduino Mega/Due shield            : <display model>,38,39,40,41
// CTE TFT LCD/SD Shield for Arduino Due       : <display model>,25,26,27,28
// Teensy 3.x TFT Test Board                   : <display model>,23,22, 3, 4
// ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
//
// Remember to change the model parameter to suit your display module!
UTFT    myGLCD(ITDB32S,38,39,40,41);

// Initialize touchscreen
// ----------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield            : 15,10,14, 9, 8
// Standard Arduino Mega/Due shield            :  6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due       :  6, 5, 4, 3, 2
// Teensy 3.x TFT Test Board                   : 26,31,27,28,29
// ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
//
UTouch  myTouch( 6, 5, 4, 3, 2);

// Declare which fonts we will be using
extern uint8_t BigFont[];

int x, y;
char stCurrent[20]="";
int stCurrentLen=0;
char stLast[20]="";

/*************************
**   Custom functions   **
*************************/

void drawButtons()
{
// Draw the upper row of buttons
  for (x=0; x<5; x++)
  {
    myGLCD.setColor(0, 0, 255);
    myGLCD.fillRoundRect (10+(x*60), 10, 60+(x*60), 60);
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (10+(x*60), 10, 60+(x*60), 60);
    myGLCD.printNumI(x+1, 27+(x*60), 27);
  }
// Draw the center row of buttons
  for (x=0; x<5; x++)
  {
    myGLCD.setColor(0, 0, 255);
    myGLCD.fillRoundRect (10+(x*60), 70, 60+(x*60), 120);
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (10+(x*60), 70, 60+(x*60), 120);
    if (x<4)
      myGLCD.printNumI(x+6, 27+(x*60), 87);
  }
  myGLCD.print("0", 267, 87);
// Draw the lower row of buttons
  myGLCD.setColor(0, 0, 255);
  myGLCD.fillRoundRect (10, 130, 150, 180);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (10, 130, 150, 180);
  myGLCD.print("Clear", 40, 147);
  myGLCD.setColor(0, 0, 255);
  myGLCD.fillRoundRect (160, 130, 300, 180);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (160, 130, 300, 180);
  myGLCD.print("Enter", 190, 147);
  myGLCD.setBackColor (0, 0, 0);
}

void updateStr(int val)
{
  if (stCurrentLen<20)
  {
    stCurrent[stCurrentLen]=val;
    stCurrent[stCurrentLen+1]='\0';
    stCurrentLen++;
    myGLCD.setColor(0, 255, 0);
    myGLCD.print(stCurrent, LEFT, 224);
  }
  else
  {
    myGLCD.setColor(255, 0, 0);
    myGLCD.print("BUFFER FULL!", CENTER, 192);
    delay(500);
    myGLCD.print("            ", CENTER, 192);
    delay(500);
    myGLCD.print("BUFFER FULL!", CENTER, 192);
    delay(500);
    myGLCD.print("            ", CENTER, 192);
    myGLCD.setColor(0, 255, 0);
  }
}

// Draw a red frame while a button is touched
void waitForIt(int x1, int y1, int x2, int y2)
{
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
  while (myTouch.dataAvailable())
    myTouch.read();
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
}

/*************************
**  Required functions  **
*************************/

void setup()
{
// Initial setup
  myGLCD.InitLCD();
  myGLCD.clrScr();

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 255);
  drawButtons();  
}

void loop()
{
  while (true)
  {
    if (myTouch.dataAvailable())
    {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
      
      if ((y>=10) && (y<=60))  // Upper row
      {
        if ((x>=10) && (x<=60))  // Button: 1
        {
          waitForIt(10, 10, 60, 60);
          updateStr('1');
        }
        if ((x>=70) && (x<=120))  // Button: 2
        {
          waitForIt(70, 10, 120, 60);
          updateStr('2');
        }
        if ((x>=130) && (x<=180))  // Button: 3
        {
          waitForIt(130, 10, 180, 60);
          updateStr('3');
        }
        if ((x>=190) && (x<=240))  // Button: 4
        {
          waitForIt(190, 10, 240, 60);
          updateStr('4');
        }
        if ((x>=250) && (x<=300))  // Button: 5
        {
          waitForIt(250, 10, 300, 60);
          updateStr('5');
        }
      }

      if ((y>=70) && (y<=120))  // Center row
      {
        if ((x>=10) && (x<=60))  // Button: 6
        {
          waitForIt(10, 70, 60, 120);
          updateStr('6');
        }
        if ((x>=70) && (x<=120))  // Button: 7
        {
          waitForIt(70, 70, 120, 120);
          updateStr('7');
        }
        if ((x>=130) && (x<=180))  // Button: 8
        {
          waitForIt(130, 70, 180, 120);
          updateStr('8');
        }
        if ((x>=190) && (x<=240))  // Button: 9
        {
          waitForIt(190, 70, 240, 120);
          updateStr('9');
        }
        if ((x>=250) && (x<=300))  // Button: 0
        {
          waitForIt(250, 70, 300, 120);
          updateStr('0');
        }
      }

      if ((y>=130) && (y<=180))  // Upper row
      {
        if ((x>=10) && (x<=150))  // Button: Clear
        {
          waitForIt(10, 130, 150, 180);
          stCurrent[0]='\0';
          stCurrentLen=0;
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(0, 224, 319, 239);
        }
        if ((x>=160) && (x<=300))  // Button: Enter
        {
          waitForIt(160, 130, 300, 180);
          if (stCurrentLen>0)
          {
            for (x=0; x<stCurrentLen+1; x++)
            {
              stLast[x]=stCurrent[x];
            }
            stCurrent[0]='\0';
            stCurrentLen=0;
            myGLCD.setColor(0, 0, 0);
            myGLCD.fillRect(0, 208, 319, 239);
            myGLCD.setColor(0, 255, 0);
            myGLCD.print(stLast, LEFT, 208);
          }
          else
          {
            myGLCD.setColor(255, 0, 0);
            myGLCD.print("BUFFER EMPTY", CENTER, 192);
            delay(500);
            myGLCD.print("            ", CENTER, 192);
            delay(500);
            myGLCD.print("BUFFER EMPTY", CENTER, 192);
            delay(500);
            myGLCD.print("            ", CENTER, 192);
            myGLCD.setColor(0, 255, 0);
          }
        }
      }
    }
  }
}

reading more on the while() loop, I'm not sure it's needed in my sketch as I am not trying to stop doing anything else while the buttons are pressed.

But it bothers me that it is broken and I can't understand why. It has to be the (true) statement on there, I was assuming that the

while(true)
{
  if (myTouch.dataAvailable())

was calling the while statement. But apparently something else in the button_test sketch is calling it, but I don't see it.

I think if I write it as

while(myTouch.dataAvailable())
  {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
     
      Serial.print(F("Touch Screen X ")); 
      Serial.println(x);
      Serial.print(F("Touch Screen Y ")); 
      Serial.println(y);
      
      if ((x>=248) && (x<=315))  // Upper row
      {
        if ((y>=5) && (y<=105))  // Button: UP
        {
          waitForIt(250, 10, 310, 110);
                    
        }
        if ((y>=135) && (y<=315))  // Button: DOWN
        {
          waitForIt(250, 130, 310, 230);
          
        }
      }
    }

that maybe it would work?

edit: It appears to be functional the way it is written above, but I still don't understand where the (true) came from in the original button_test sketch example. Guess I'll go read more.

while(true) is unconditional, it will loop forever. It has no way of knowing about the touchscreen data availability.

while(myTouch.dataAvailable()) will know about the touchscreen data availability.

Thank you!

That is why I came to ask :slight_smile:

I assumed the (true) was for something calling it, as you said it doesn't make sense to put a repeat call in a repeat call.... I'm just learning so it threw me for a loop (heh).

So, in my original code, would adding the while(myTouch.dataAvailable()) even be worth implementing? Would my code benefit from pausing while changing the offset?

Edit: Huh, I just had a nasty thought, would a while() statement cause your PWM outputs to stop also? If they are being controlled by the void.loop? Or would they hold in their last position until the while() had ended?

No matter if my present code benefits, I know I sure did, thank you guys for the explanations! Reading through the documentation, your explanations and playing with a shorter sketch really helped me to start understanding it a bit better.

There is no point in using both the while and the if. Use one or the other.

I guess I wasn't clear :slight_smile:

Would it benefit me to remove the if(myTouch.dataAvailable()) and replace it with while(myTouch.dataAvailable())?

I am starting to think no, but it's a learning process, and the only way to learn is to ask and do!

lmarklar:
I assumed the (true) was for something calling it, as you said it doesn't make sense to put a repeat call in a repeat call.... I'm just learning so it threw me for a loop (heh).

'true' in C is a boolean constant. Just as 23 and 0x17 are ways to write down the int twenty three, and 'a' is how you write the char constant sixty-five (ASCII lowercase A), true is how you write down a boolean yes rather than no.

Let's at take the expression 3*4-2 . The program evaluates this expression, its value is integer 10. Likewise, when the program evaluates the expression 1==2, its value is boolean false.

so

while(true) { /* do this */ }

is exactly the same as

while(1 == 1) { /* do this */ }

You are saying "do the following over and over for so long as one is equal to one". How long will that be? Well: one is always equal to one. So it will do the following forever (or until you hit the reset button or power off :slight_smile: ).

lmarklar:
Would it benefit me to remove the if(myTouch.dataAvailable()) and replace it with while(myTouch.dataAvailable())?

The arduino bootloader executes your loop() over and over. When your loop() finishes, the bootloader just runs it again. I get the impression, however, that in that pause between loop()s, the bootloader does the work of checking to see if a new sketch is being sent down the serial line.

In this case it won't matter because often there isnt data available on myTouch and your loop() function will "fall though" and exit.

Some I/O routines are "blocking", by which is meant that when you do a read() they stop ans sit there waiting until data comes in. If you do a while( /* some blocking read */ ), then the loop never exits - it will sit there waiting for input and handling the input that comes in forever.

I suspect that this means that if a sketch like that is running, then you won't be able to reload sketches into the arduino again - you'll need to press the reset button on the board to erase the sketch that's in it.

So you want to write your loop() so that it does a block of work in a reasonably short amount of time and then exits.

PLEASE NOTE: this is just an educated guess on my part.

The bootloader does no such thing. Your sketch will run without the bootloader.

The bootloader looks for incoming serial data, matching an attempt to reprogram the board. If it find it, the board is reprogrammed. Then it jumps to the first instruction in the sketch.

With no serial input, it jumps to the first instruction in the sketch. Its work is done.

The main sketch is basically this:

int main ()
  {
  init ();   // initialize hardware
  setup ();  // your initializtion
  while (true)
    loop ();
  } // end of main

lmarklar:
The post will be broken into three parts as it was too large to fit in one.

You can attach your code to a post.

How to use this forum

With paranoia levels what they are, I prefer to not ask people to download things from my posts. I'd rather take an extra post to fit it. I'm not quite to the point that I have crazy large sketches, if I get there and ask for help I'll definitely take your advice and attach the sketch though!

I appreciate the answers! Every little bit helps, I doubt that I'll ever be an 'expert', but hopefully I can learn enough to accomplish my own goals!

You can attach the .ino files. I'm not that paranoid that I think it might somehow mutate into a virus. A .zip file, maybe.

I'll give that a shot then next time, thanks Nick! I'm new to the Arduino and programming in general, I know that I don't click on downloads in 90% of any other forums I frequent.

heh, just thought of a relevant quote from a great book....

“Paranoid? Probably. But just because you're paranoid doesn't mean there isn't an invisible demon about to eat your face.”
*― Jim Butcher, Storm Front *