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
}
}
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.str.toCharArray(b,5);
andtest1++
and it still fragments. which I find odd. if I set test1=20 it locks up. am I still running out of ram? it just doesn't seem right. I know I'm over looking something. guess I will go back to trying to get the drawnumber to work again.