Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

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
  }
}
share|improve this question
    
When you say 'hangs it up', is there no error output? Have you tried printing String(test1) to see what happens? From what you've said I guess 'oops check' printed out but 'sanity check' didn't? –  bladepanthera 17 hours ago
    
when I say it hangs up the touch screen stops responding and the serial output stops with "oops check" being last thing printed. when this happens have to reboot processor. no I have not tried printing String(test1) will see if I can do that now. –  Michael 14 hours ago
    
just tried printing (test1) and (str) both will print but it still locks up when it goes to execute the line str = String(test1); I can't figure why. the way the screen acts it's like the program corrupts. I'm getting stray lines and shapes on the screen at times. –  Michael 13 hours ago

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.