Go Down

Topic: Showing year as three digits! (Read 716 times) previous topic - next topic

picmetoo

Hi everyone,
This line has caused the year to be shown as 3 digits, (201). Could someone put me out of my misery and show me how to do it. The rest of the line works fine.

chars = sprintf( test, "%02d:%02d:%02d   %02d:%02d:%04d   ", hour(), minute(), second(), day(), month(), year() );

I changed the  %02d to %04d but this made no difference.

wildbill

Looks fine. Post the entire sketch for more help.

James C4S

Yes, need to see the whole sketch.  Specifically how test and chars are defined and how you are using them.  Also, what is creating these functions you are getting the data from.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

picmetoo

#3
Jul 26, 2012, 07:52 pm Last Edit: Jul 26, 2012, 08:11 pm by picmetoo Reason: 1
Thanks for your prompt replies, here is the code:

Code: [Select]
MSFTime MSF;// = MSFTime();

time_t msfTimeSync();
byte prevStatus = 255;



void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);

  LedSign::Init(GRAYSCALE);
 
  MSF.init( 255 ); // LED pin for status
   
  setSyncProvider(msfTimeSync);  // tell the Time library what to ask for a new time value

}

time_t msfTimeSync() // called periodically by Time library to syncronise itself
{
   return MSF.getTime();
}


void drawLine( int offset, int val, int range )
{
 
  int total = 9;

  val = val%range;
  int at = ( val * total ) / range; 
 

  for( int i =0; i < at-1; i ++ )
    LedSign::Set(i,offset, 1);
   
  LedSign::Set(at-1, offset, 3);
 
}



int offset = 13;

void loop()                     // run over and over again
{
  LedSign::Clear();
 
  //drawLine( 0, 7, 8);
 
  char buff[20];
  now(); // make sure Time has attempted to sync

  byte currStatus = MSF.getStatus();

  #define MESSAGE_LEN 20

  char test[MESSAGE_LEN];

  int chars =0;
 
  if( timeStatus() == timeNotSet )
  {
   

     if( ! (currStatus & MSF_STATUS_CARRIER))
       chars = sprintf( test, "NO CARRIER   ");
     else if( (currStatus & MSF_STATUS_WAITING))
     {
       drawLine( 8, MSF.getProgess(), 60 );

       chars = sprintf( test, "WAITING   ");
     }
     else if( (currStatus & MSF_STATUS_READING))
     {
       drawLine( 8, MSF.getProgess(), 60 );

       chars = sprintf( test, "READING   ");
     }
     else
       chars = sprintf( test, "CONFUSED   "); // should never happen
  }
  else
  {
    int mins = MSF.getFixAge()/60000L;
    if( mins < 0 )
      mins = 0;
     
    drawLine( 8, mins, 14 ); // fix age, one LED per min
   chars = sprintf( test, "%02d:%02d:%02d   %02d:%02d:%04d   ", hour(), minute(), second(), day(), month(), year() );
  }


  int8_t x, x2;

  x = offset;


  for(int i=0;i<chars;i++)
  {
    x2=Font::Draw(test[i],x,0,3);
    x+=x2;
    if (x>=13) break;
  } 
 
  if( x >= 13 )
    offset --; // there was more stuff to draw off the right, keep scrolling
  else
    offset = 13; // set the scroll back to draw at the right of the screen
   
  delay(80);
   

}

Msquare

#4
Jul 26, 2012, 08:02 pm Last Edit: Jul 26, 2012, 09:00 pm by Msquare Reason: 1
You MUST enclose your code in the code tags (use the "#" icon when entering the code). For example: I see a smiley with sunglasses suddenly in the code instead of a digit and closing bracket. I can guess this is a 8. Also some subscript brackets get "swallowed". Note you can edit the post you have submitted.The above was edited, so now legible.

You're also missing the "#include" of the libraries you are using.

James C4S

You defined test to be 20 characters long.  Sprintf() puts a null character at the end, which takes up a character.  Test needs to be bigger.

I'm not sure I understand what you are doing with the variable "chars".  sprintf returns the number of characters written.  "chars" isn't an int, it is an array of characters, why?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

picmetoo

Thanks James C4S,
I Increased it to 30 characters and it works fine.

Go Up