user12129: I'm new to arduino and currently trying to build a small program with multiple screens and buttons and a counter. when I try to convert a int to a chararray it messes up the world. with out it what I have so far works any suggestions on what I'm doing wrong? with the code to convert the int to chararray it seems to lock up the program when I switch from main screen to options screen then back. I put some serial outs to narrow down where the flaw is. 
My results are it is between
   test1++;  Serial.println("oops check");
   str = String(test1); Serial.println("sanity check"); 

Something hangs it up after it outputs "oops check" so I figure it is str = String(test1); I have been searching and trying every thing I can find. if someone can give me a kick in the right direction. I'm usin the tft seedstudio v2 display with the modified tft library for text direction.
   /* 
  Title: TouchScreenButton Tutorial
  About: Example/tutorial file for how to create buttons with the TouchScreenButtons library
  Author: Richard Kirkpatrick
  Date: 20 July 2014
*/

// Libraries
#include <stdint.h>  
#include <SeeedTouchScreen.h>  // Library for the Seeed Studio TFT Touch Shield 
#include <TFTv2.h>      // Library for the Seeed Studio TFT Touch Shield 
#include <TouchScreenGeometry.h> // Library for drawing geometric shapes for the touch screen
#include <TouchScreenStrings.h> // Library for drawing strings for the touch screen
#include <TouchScreenButtons.h> // Library for creating buttons for the keypad
#include <SPI.h>


//Measured ADC values for (0,0) and (210-1,320-1)
//TS_MINX corresponds to ADC value when X = 0
//TS_MINY corresponds to ADC value when Y = 0
//TS_MAXX corresponds to ADC value when X = 240 -1
//TS_MAXY corresponds to ADC value when Y = 320 -1
#define TS_MINX 890*2
#define TS_MAXX 116*2
#define TS_MINY 913*2
#define TS_MAXY 83*2

TouchScreen ts = TouchScreen(XP, YP, XM, YM);

// Global variables
String str;
Button bigButton;
Button buttons[12];
Button reset;
Button back;
int screen = 1;
int test;
int test1 = 0;
char b[5];

void setup() 
{
  // Initializes the TFT library
   Tft.TFTinit();      

  // Start the Serial port. Used to determine which button was pressed.
  Serial.begin(9600); 
 screen = 1; 

}

void loop()
{ test1++;  Serial.println("oops check");
  str = String(test1); Serial.println("sanity check");
  str.toCharArray(b,5);

  Serial.print("screen = "); Serial.print(screen );
  // A point objects holds x, y, and z coordinates
  Point p = ts.getPoint(); 
  p.x = map(p.x, TS_MINX, TS_MAXX, 240, 0);
  p.y = map(p.y, TS_MINY, TS_MAXY, 320, 0);

  // Determine which button was pressed
  int userInput = -1; // -1 means no button is pressed
  if (p.z > __PRESURE){ 
    userInput = getButtonNumber(p.x, p.y); 
     Serial.print("X = "); Serial.print(p.x);
     Serial.print("\tY = "); Serial.print(p.y);
     Serial.print("\tPressure = "); Serial.println(p.z);
  }

   // Show which button was pressed on the Serial monitor

   if (userInput == 13) {
    Serial.println("Options button was pressed!");
    bigButton.buttonDisplay();
  }
   if (userInput == 15) {
    Serial.println("back button was pressed!");
    back.buttonDisplay();
  }
  else if (userInput == 14) {
    Serial.println("reset button was pressed!");
    reset.buttonDisplay();
  } 



  delay(10);

if (screen == 1)  {

if (test == 1) {

    Tft.fillScreen();    }


  Serial.print("test1 =  "); Serial.println(b);      // prints "Hello String"

    // Draw reset button
   reset.setValues(75, 215, 35, 100);
  reset.draw();

 // Draw the big button
  bigButton.setValues(15, 185, 35, 130);
  bigButton.draw();
Tft.drawRectangle(40, 15, 70, 120,BLUE);


Tft.setDisplayDirect(UP2DOWN);


  Tft.drawString("Frays",100,20,2,RED);

   Tft.drawString(b,65,20, 2, BLUE);


  //Draw reset buttons text
  TouchScreenString resetText("RESET", 100, 230, 2, RED);
  resetText.drawText();
 Serial.println("a");      // prints "Hello String"

  // Draw the big button's text
  TouchScreenString bigButtonText("OPTIONS", 40, 197, 2, WHITE);
  bigButtonText.drawText();
test = 0;

}

if (screen == 2)  {

  if (test == 0) {Tft.fillScreen();}
 // draw back button
  back.setValues(15, 15, 35, 85);
  back.draw();


Tft.setDisplayDirect(UP2DOWN);


   // Draw the back button's text
  TouchScreenString backText("BACK", 40, 30, 2, WHITE);
  backText.drawText();
test = 1;
}}

// Gets which button was pressed.  If no button is pressed, -1 is returned.
int getButtonNumber(int xInput, int yInput)
{



if (screen == 1) {
  if (bigButton.isPressed(xInput, yInput)) {
    screen = 2;
    return 13; // Signifies big button was pressed
  } }
  if (screen ==2) {
  if (back.isPressed(xInput, yInput)) {
    screen = 1;
    return 15; // Signifies big button was pressed
  }}
  if (screen == 1) {
  if (reset.isPressed(xInput, yInput)) {
    test1 = 0;
    return 14; // Signifies reset button was pressed
  }}
  else {
  return -1; // Signifies no button was pressed
  }
}


user10794: My guess is you are running out of RAM. Your various string constants alone take up 202 bytes. Then there is this:
  test1++;  
  str = String(test1); 
  str.toCharArray(b,5);

That is very likely to fragment memory, as test1 gets larger and larger. So you are fragmenting RAM, of which you don't have much to start with.
Why are you even converting test1 into a String object? And then converting it back into an array?
The array b can only hold 4 digits as it needs one byte for the trailing null-terminator.
I suggest you stop doing that String stuff, and also use the F() macro like this ...
Change:
    Serial.println("Options button was pressed!");

to:
    Serial.println(F("Options button was pressed!"));

Ditto for all the other places you are displaying stuff.

12129: 

<p>I'm new to arduino and currently trying to build a small program with multiple screens and buttons and a counter. when I try to convert a int to a chararray it messes up the world. with out it what I have so far works any suggestions on what I'm doing wrong? with the code to convert the int to chararray it seems to lock up the program when I switch from main screen to options screen then back. I put some serial outs to narrow down where the flaw is. </p>

<p>My results are it is between</p>

<pre class="lang-C++ prettyprint-override"><code>   test1++;  Serial.println("oops check");
   str = String(test1); Serial.println("sanity check"); 
</code></pre>

<p>Something hangs it up after it outputs "oops check" so I figure it is <code>str = String(test1);</code> I have been searching and trying every thing I can find. if someone can give me a kick in the right direction. I'm usin the tft seedstudio v2 display with the modified tft library for text direction.</p>

<pre class="lang-C++ prettyprint-override"><code>   /* 
  Title: TouchScreenButton Tutorial
  About: Example/tutorial file for how to create buttons with the TouchScreenButtons library
  Author: Richard Kirkpatrick
  Date: 20 July 2014
*/

// Libraries
#include &lt;stdint.h&gt;  
#include &lt;SeeedTouchScreen.h&gt;  // Library for the Seeed Studio TFT Touch Shield 
#include &lt;TFTv2.h&gt;      // Library for the Seeed Studio TFT Touch Shield 
#include &lt;TouchScreenGeometry.h&gt; // Library for drawing geometric shapes for the touch screen
#include &lt;TouchScreenStrings.h&gt; // Library for drawing strings for the touch screen
#include &lt;TouchScreenButtons.h&gt; // Library for creating buttons for the keypad
#include &lt;SPI.h&gt;


//Measured ADC values for (0,0) and (210-1,320-1)
//TS_MINX corresponds to ADC value when X = 0
//TS_MINY corresponds to ADC value when Y = 0
//TS_MAXX corresponds to ADC value when X = 240 -1
//TS_MAXY corresponds to ADC value when Y = 320 -1
#define TS_MINX 890*2
#define TS_MAXX 116*2
#define TS_MINY 913*2
#define TS_MAXY 83*2

TouchScreen ts = TouchScreen(XP, YP, XM, YM);

// Global variables
String str;
Button bigButton;
Button buttons[12];
Button reset;
Button back;
int screen = 1;
int test;
int test1 = 0;
char b[5];

void setup() 
{
  // Initializes the TFT library
   Tft.TFTinit();      

  // Start the Serial port. Used to determine which button was pressed.
  Serial.begin(9600); 
 screen = 1; 

}

void loop()
{ test1++;  Serial.println("oops check");
  str = String(test1); Serial.println("sanity check");
  str.toCharArray(b,5);

  Serial.print("screen = "); Serial.print(screen );
  // A point objects holds x, y, and z coordinates
  Point p = ts.getPoint(); 
  p.x = map(p.x, TS_MINX, TS_MAXX, 240, 0);
  p.y = map(p.y, TS_MINY, TS_MAXY, 320, 0);

  // Determine which button was pressed
  int userInput = -1; // -1 means no button is pressed
  if (p.z &gt; __PRESURE){ 
    userInput = getButtonNumber(p.x, p.y); 
     Serial.print("X = "); Serial.print(p.x);
     Serial.print("\tY = "); Serial.print(p.y);
     Serial.print("\tPressure = "); Serial.println(p.z);
  }

   // Show which button was pressed on the Serial monitor

   if (userInput == 13) {
    Serial.println("Options button was pressed!");
    bigButton.buttonDisplay();
  }
   if (userInput == 15) {
    Serial.println("back button was pressed!");
    back.buttonDisplay();
  }
  else if (userInput == 14) {
    Serial.println("reset button was pressed!");
    reset.buttonDisplay();
  } 



  delay(10);

if (screen == 1)  {

if (test == 1) {

    Tft.fillScreen();    }


  Serial.print("test1 =  "); Serial.println(b);      // prints "Hello String"

    // Draw reset button
   reset.setValues(75, 215, 35, 100);
  reset.draw();

 // Draw the big button
  bigButton.setValues(15, 185, 35, 130);
  bigButton.draw();
Tft.drawRectangle(40, 15, 70, 120,BLUE);


Tft.setDisplayDirect(UP2DOWN);


  Tft.drawString("Frays",100,20,2,RED);

   Tft.drawString(b,65,20, 2, BLUE);


  //Draw reset buttons text
  TouchScreenString resetText("RESET", 100, 230, 2, RED);
  resetText.drawText();
 Serial.println("a");      // prints "Hello String"

  // Draw the big button's text
  TouchScreenString bigButtonText("OPTIONS", 40, 197, 2, WHITE);
  bigButtonText.drawText();
test = 0;

}

if (screen == 2)  {

  if (test == 0) {Tft.fillScreen();}
 // draw back button
  back.setValues(15, 15, 35, 85);
  back.draw();


Tft.setDisplayDirect(UP2DOWN);


   // Draw the back button's text
  TouchScreenString backText("BACK", 40, 30, 2, WHITE);
  backText.drawText();
test = 1;
}}

// Gets which button was pressed.  If no button is pressed, -1 is returned.
int getButtonNumber(int xInput, int yInput)
{



if (screen == 1) {
  if (bigButton.isPressed(xInput, yInput)) {
    screen = 2;
    return 13; // Signifies big button was pressed
  } }
  if (screen ==2) {
  if (back.isPressed(xInput, yInput)) {
    screen = 1;
    return 15; // Signifies big button was pressed
  }}
  if (screen == 1) {
  if (reset.isPressed(xInput, yInput)) {
    test1 = 0;
    return 14; // Signifies reset button was pressed
  }}
  else {
  return -1; // Signifies no button was pressed
  }
}
</code></pre>
 10794: 

<p>My guess is you are running out of RAM. Your various string constants alone take up 202 bytes. Then there is this:</p>

<pre class="lang-C++ prettyprint-override"><code>  test1++;  
  str = String(test1); 
  str.toCharArray(b,5);
</code></pre>

<p>That is very likely to fragment memory, as <code>test1</code> gets larger and larger. So you are fragmenting RAM, of which you don't have much to start with.</p>

<p>Why are you even converting test1 into a String object? And then converting it back into an array?</p>

<p>The array <code>b</code> can only hold 4 digits as it needs one byte for the trailing null-terminator.</p>

<p>I suggest you stop doing that String stuff, and also use the <code>F()</code> macro like this ...</p>

<p>Change:</p>

<pre class="lang-C++ prettyprint-override"><code>    Serial.println("Options button was pressed!");
</code></pre>

<p>to:</p>

<pre class="lang-C++ prettyprint-override"><code>    Serial.println(F("Options button was pressed!"));
</code></pre>

<p>Ditto for all the other places you are displaying stuff.</p>
