Please note that, at present, the
String library has bugs as discussed
here and
here.
In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.
I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).
Alternatively, install the fix described here:
http://arduino.cc/forum/index.php/topic,145765all of my code can not be posted because it is above the maximum allowed characters for a post.
You can do attachments.
How to use this forumThank you Nick for the reply. I will look into redoing the code to make it work.
As a side note, I have paired down this code to isolate just where I am having a problem.
Here is the new code:
#include <UTFT.h>
#include <UTouch.h>
#include <EEPROM.h>
#include <writeAnything.h>
#include <DS1307.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//TOUCH PANEL and TFT MEGA SHIELD V2.0
//(Mega Shield utilizes pins 5V, 3V3, GND, 2-6 (Touch Control), 22-41 (LCD), & (50-53 for SD Card))
UTFT myGLCD(TFT01_32, 38,39,40,41);
UTouch myTouch(6,5,4,3,2);
// Init the DS1307
DS1307 rtc(8, 9);
//Declare which fonts to be utilized
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
#define LARGE true
#define SMALL false
// Init a Time-data structure
Time t, tSet;
byte UpdateTandDBar;
unsigned long prevMillis30 = 0;
//------------------------------------- Set Font -----------------------------------------//
void setFont(boolean font, byte cr, byte cg, byte cb, byte br, byte bg, byte bb)
{
myGLCD.setBackColor(br, bg, bb); //font MenuBtnground black
myGLCD.setColor(cr, cg, cb); //font color white
if (font==LARGE)
myGLCD.setFont(BigFont); //font size LARGE
else if (font==SMALL)
myGLCD.setFont(SmallFont);
}
//--------------------------------- Time and Date Bar ------------------------------------//
void TimeDateBar() //This needs to be updated once RTC is connected
{
String time = "", minute = "", hour = "",ampm = "";
myGLCD.setColor (0,0,255);
myGLCD.fillRect (0, 0, 319, 18);
Serial.print ("t.hour entering Sub = ");
Serial.println (t.hour,DEC);
//Pad minute with 0 if necessary
if ((t.min >= 0) && (t.min <= 9))
{
minute = "0" + String(t.min);
}
else
{
minute = String(t.min);
}
Serial.print ("t.min = ");
Serial.println (t.min,DEC);
Serial.print ("minute = ");
Serial.println (minute);
//Figure out hour
if (t.hour == 0)
{
hour = "12";
}
else if (t.hour > 12)
{
hour = String(t.hour - 12);
}
else
{
hour = String(t.hour);
}
Serial.print ("t.hour after adjustment = ");
Serial.println (t.hour,DEC);
Serial.print ("hour after adjustment = ");
Serial.println (hour);
//Determin AM or PM
if (t.hour < 12)
{
ampm = " AM";
}
else
{
ampm = " PM";
}
Serial.print ("ampm after adjustment = ");
Serial.println (ampm);
time = hour + ":" + minute + ampm;
Serial.println ("Time is " + time);
setFont(LARGE,255,255,255,0,0,255);
UpdateTandDBar = 0;
myGLCD.print(time, RIGHT, 1);
myGLCD.print(rtc.getDateStr(FORMAT_LONG,FORMAT_MIDDLEENDIAN,'/'),LEFT,1);
Serial.println ("-----------");
}
//--------------------------------------- Setup ------------------------------------------//
void setup()
{
Serial.begin (9600);
myGLCD.InitLCD();
myGLCD.clrScr();
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
t = rtc.getTime();
TimeDateBar();
}
//------------------------------------- Main Loop ----------------------------------------//
void loop()
{
unsigned long currentMillis = millis();
if ((currentMillis - prevMillis30 >= 30000) || (UpdateTandDBar)) //Update Time every 30 Seconds
{
prevMillis30 = currentMillis;
// Get data from the DS1307
t = rtc.getTime();
delay(10);
Serial.println ("Entering Sub");
TimeDateBar();
}
}
and the serial output:
t.hour entering Sub = 17
t.min = 54
minute = 54
t.hour after adjustment = 17
hour after adjustment = 5
ampm after adjustment = PM
Time is 5:54 PM
-----------
Entering Sub
t.hour entering Sub = 17
t.min = 54
minute = 54
t.hour after adjustment = 17
hour after adjustment = 5
ampm after adjustment = PM
so the upper part of the serial output would be the call from the setup loop. The bottom part is the subroutine call.