Another sprintf question << SOLVED>>

I want to display the FILE and _DATE- on a lcd in the following: I do not know the correct format.

 sprintf(line0, " %s, %s", _FILE_, _DATE_); -- how do I do this
  lcd.setCursor(0, 0);  lcd.print(line0);
sprintf(line0, _FILE_ ", " _DATE_); 

you don't need sprintf for this. those are all string literals.
lcd.print(" " _FILE_ ", " _DATE_);

You need TWO underscores before and after FILE and DATE.

On my system, FILE is quite long:
/Users/john/Documents/Arduino/sketch_jan06b/sketch_jan06b.ino, Jan 11 2022
Are you sure 'line0' is big enough to hold it? Try:
snprintf(line0, sizeof line0, " %s, %s", __FILE__, __DATE__); -- how do I do this

What he had will also work just as well...

Yeah, I also forgot the leading space - I just went for the minimalist approach.

You are correct John, it FILE does show the complete path which is too long. I was looking for somethings that would indicate the name of the SKETCH and possibly the date - .
If that not possible I will stay with the date and setting the version number.
The lcd is 20x4.

This is so much more fun with a real live example:

// 11 Januari 2022, by Koepel, Public Domain
// Version 1 : Initial version, working in linux in Wokwi.
// Version 2 : added some fun code in the loop()
// Version 3 : fixed big bug for the Arduino IDE in Windows.

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd( 0x27, 20, 4);
char buffer[40];
float version = 1.00;


void setup()
{
  Serial.begin(115200);
  Serial.println( "__FILE__    : " __FILE__);
  Serial.println( "__DATE__    : " __DATE__);
  Serial.println( "__TIME__    : " __TIME__);
  Serial.println( "__VERSION__ : " __VERSION__);
  Serial.print(   "ARDUINO     : ");
  Serial.println( ARDUINO);

  // Trying to extract the name of the sketch
  // This part is bad code, my apologies in advance
  char *p = strrchr( __FILE__, '/');    // find last forward slash
  if( p == nullptr)
  {
    p = strrchr( __FILE__, '\\');  // perhaps it is a backward slash
  }
  
  if( p != nullptr)
  {
    p++;                                // strip the leading forward or backward slash
    strcpy( buffer, p);

    // strip the extension
    char *pDot = strrchr( buffer, '.');
    if( pDot != nullptr)
    {
      *pDot = '\0';
    }
    Serial.print( "Name of the sketch : ");
    Serial.print( '\"');
    Serial.print( buffer);
    Serial.print( '\"');
    Serial.println();
  }
  else
  {
    Serial.println( "Oops, can not find the sketch name");
  }

  lcd.init();
  lcd.backlight();

  lcd.setCursor( 0, 0);
  lcd.print( "\"Real\" Live Example");
  lcd.setCursor( 0, 1);
  lcd.print( "Version ");
  lcd.print( version);

  lcd.setCursor( 0, 2);
  lcd.print( "Name : ");
  lcd.print( '\"');
  lcd.print( buffer);
  lcd.print( '\"');

  lcd.setCursor( 0, 3);
  lcd.print( __DATE__ " " __TIME__);  // One space in the middle for exactly 20 characters

  delay( 10000);
}


void loop() 
{
  // Have some fun with the digits of the time and the number of the version
  int col = random( 0, 6);
  col += col / 2;
  int digit = random( 0, 10);
  lcd.setCursor( col + 12, 3);
  lcd.print( digit);

  if( millis() % 4000 < 2000)
    version += float( digit) / 100.0;
  else
    version -= float( digit) / 100.0;

  lcd.setCursor( 8, 1);
  lcd.print( version, 2);

  delay( 150);
}

Well, maybe not "real", but the sketch is alive in a simulation:

[EDIT] Oops, there was a big bug. I have updated the sketch above. Version 3 should work in Windows.

Thanks for the real life experience - however when run on my system the following is showing on the Serial Monitor and on the lcd.

FILE : C:\Temp\sketch\sketch.ino
DATE : Jan 11 2022
TIME : 16:54:30
Oops, can not find the sketch name

windows 10

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd( 0x27, 20, 4);
char buffer[40];

void setup()
{
  Serial.begin(115200);
  Serial.println( "__FILE__ : " __FILE__);
  Serial.println( "__DATE__ : " __DATE__);
  Serial.println( "__TIME__ : " __TIME__);

  // Trying to extract the name of the sketch
  // This part is bad code, my apologies in advance
  char *p = strrchr( __FILE__, '/');    // find last forward slash
  if( p == nullptr)
  {
    char *p = strrchr( __FILE__, '\\');  // perhaps it is a backward slash
  }
  
  if( p != nullptr)
  {
    p++;                                // strip the leading forward or backward slash
    strcpy( buffer, p);

    // strip the extension
    char *pDot = strrchr( buffer, '.');
    if( pDot != nullptr)
    {
      *pDot = '\0';
    }
    Serial.print( "Name of the sketch : ");
    Serial.print( '\"');
    Serial.print( buffer);
    Serial.print( '\"');
    Serial.println();
  }
  else
  {
    Serial.println( "Oops, can not find the sketch name");
  }

  lcd.init();
  lcd.backlight();

  lcd.setCursor( 0, 0);
  lcd.print( "\"Real\" Live Example");
  lcd.setCursor( 0, 1);
  lcd.print( "Version 1.00");

  lcd.setCursor( 0, 2);
  lcd.print( "Name : ");
  lcd.print( '\"');
  lcd.print( buffer);
  lcd.print( '\"');

  lcd.setCursor( 0, 3);
  lcd.print( __DATE__ " " __TIME__);  // One space in the middle for exactly 20 characters
}

void loop() {}

I am very ashamed, I declared the pointer p twice.
afbeelding

I have updated the sketch here in Reply #8 and also on Wokwi.

Thank you for reminding me that coding is FUN. Also for the informative way of resenting a solution.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.