Need some help with subroutine * RESOLVED*

Hello all,

I have some code that I need help with. It seems that the subroutine that I created for updating my time and date bar is not functioning correctly. When the subroutine is called from the Setup loop, it runs correctly and populates the data. However when it is called from the main loop, it never executes. I have added a bunch of Serial.prints to try and help troubleshoot it. Here is the code. Please let me know if you need more information or what a possible solution might be. I cant include all the code as its over the maximum allow length for a single post. So here are the subroutine I am calling, the setup loop, and the main loop.

Subroutine being called

//--------------------------------- 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 ("-----------");
}

And the setup and main loop

//--------------------------------------- Setup ------------------------------------------//
void setup()
{
  Serial.begin (9600);
  // Initial setup
  pinMode(RenaFiltPin, OUTPUT); 
  pinMode(SunFiltPin, OUTPUT);
  pinMode(MainLtPin, OUTPUT);
  pinMode(MoonLtPin, OUTPUT);
  pinMode(HeaterPin, OUTPUT);
  pinMode(CO2Pin, OUTPUT);
  pinMode(DosePmp1Pin, OUTPUT);
  pinMode(DosePmp2Pin, OUTPUT);

  digitalWrite(RenaFiltPin, HIGH);
  digitalWrite(SunFiltPin, HIGH);
  digitalWrite(MainLtPin, HIGH);
  digitalWrite(MoonLtPin, HIGH);
  digitalWrite(HeaterPin, HIGH);
  digitalWrite(CO2Pin, HIGH);
  digitalWrite(DosePmp1Pin, HIGH);
  digitalWrite(DosePmp2Pin, HIGH);

  myGLCD.InitLCD();
  myGLCD.clrScr();

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

  sensors.begin();     //start up temperature library
  // set the resolution to 9 bit
  sensors.setResolution(HoodTempSen, 9);
  sensors.setResolution(WaterTempSen, 9);

  t = rtc.getTime();
  checkTemp();
  TimeDateBar();
  mainScreen(true);  
}

//------------------------------------- Main Loop ----------------------------------------//
void loop()
{
  unsigned long currentMillis = millis();

  if (myTouch.dataAvailable())  
  { 
    processMyTouch();
  }
  if ((currentMillis - prevMillis30 >= 30000) || (UpdateTandDBar))    //Update Time every 30 Seconds
  {
    prevMillis30 = currentMillis;
    //checkTemp();
    if (dispScreen == 0)
    {
      mainScreen(false);
    }
      
    // Get data from the DS1307
    t = rtc.getTime();
    //delay(50);
	Serial.println ("Entering Sub");
	TimeDateBar();
    //checkMainLt();
  }
}

Thanks,
Grimm

Where and how is 'prevMillis30' declared, UpdateTandDBar = 0 so the if is probably never entered.

They are declared in the beginning. As I said, all of my code can not be posted because it is above the maximum allowed characters for a post.

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,145765

all 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 forum

Thank 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.

Not sure what your other codes are doing, but if you are using Timers or Interupts that would kill your ability to use millis()

Thanks again Nick. The string fix appears to have resolved the issue.