Writing variables to LCD display

As part of my (steep) learning curve I have bought a 20x4 character display (Newhaven NHD-0420CW) which runs on the US2066 controller. Having poured over and over the datasheets and other's code online I am still stuck on something.

All the example sketches declare the text strings globally as:

const byte TEXT[4][21] = {"1-Newhaven Display--",
                          "2-------Test--------",
                          "3-16/20-Characters--",
                          "4!@#$%^&*()_+{}[]<>?"};         // Strings to be displayed

Which is great, but I want to be able to display the values of analog inputs on the screen, so I need to be able to insert variables.

I have tried:

134 void output(void)  {
135    inValue = analogRead(inPin);
136    String stringinValue = String(inValue);
137    stringOne = String(stringValue + stringinValue);  // FYI: String stringValue = "Value = ";
138    char TEXT2[]={stringOne};
139 
140    byte r = 0;                                         // Row index
141    byte c = 0;                                         // Column index
142    command(0x01);                               // Clears display (and cursor home)
143       delay(2);                                         // After a clear display, a minimum pause of 1-2 ms is required
144    for (r=0; r<ROW_N; r++)  {               // One row at a time
145       command(new_line[r]);                  // Moves the cursor to the first column of that line
146       for (c=0; c<COLUMN_N; c++)  {    // One character at a time
147    data(TEXT2[r][c]);                           // Displays the corresponding string

But on line 138 I get the error message: cannot convert 'String' to 'char' in initialization

If I delete line 138 and instead do:

147    data(stringOne);                           // Displays the corresponding string

I get error message on line 147: cannot convert 'String' to 'byte {aka unsigned char}' for argument '1' to 'void data(byte)'

Do I need to make it in a different datatype? and if so what?

No need to convert to a String or a string.

What command do you use to output to the display ?
If you have the display declared as an object named lcd then why not just

lcd.print(inValue);

If that is not how you are using the display then please post a complete program showing how you output to it

Using an Arduino, I'd avoid using the String class; it can use up memory and cause odd problems.

Stick to strings of byte or char.

You can 'print' into a string buffer like:

  char buff[21];
  int invalue= 1234;
  snprintf (buff, sizeof(buff), "Value= %d", invalue );

Yours,
TonyWilk

Hi UKHeliBob,

I haven't seen any example codes for this display using lcd.print(). Having just looked into that function it seems it relies on liquidcrystal.h library which requires a chipset similar to HD44780, which it seems this one is not. I am also using SPI currently.

The full program code (without strings) will follow.

Thanks for the tip Tony. Could you explain each element of your code excerpt? I understand the meaning of "char buff[21];" but not the rest.

const byte inPin = A0; //Analog Sensor Input
int inValue; //Analog Sensor Value

const byte COLUMN_N = 20;          // Number of display columns
const byte ROW_N = 4;                   // Number of display rows

const byte SCLK = 13;                     // Arduino pin assigned to the SCLK line
const byte SDIN = 11;                     // Arduino pin assigned to the SID line

const byte TEXT[4][21] = {"1-Newhaven Display--",
                                          "2-------Test--------",
                                          "3-16/20-Characters--",
                                          "4!@#$%^&*()_+{}[]<>?"};         // Strings to be displayed

byte new_line[4] = {0x80, 0xA0, 0xC0, 0xE0};            // DDRAM address for each line of the display (128, 160, 192, 224)
byte rows = 0x08;                                                     // Display mode: 2/4 lines; default (0x08 = 8)


void command(byte c)  {              // SUBROUTINE: PREPARES THE TRANSMISSION OF A COMMAND
   byte i = 0;                                  // Bit index
   for(i=0; i<5; i++)  {
      digitalWrite(SDIN, HIGH);
      clockCycle();
   }
   for(i=0; i<3; i++)  {
      digitalWrite(SDIN, LOW);
      clockCycle();
   }
   send_byte(c);                             // Transmits the byte
}


void data(byte d)  {                    // SUBROUTINE: PREPARES THE TRANSMISSION OF A BYTE OF DATA
   byte i = 0;                               // Bit index
   for(i=0; i<5; i++)  {
      digitalWrite(SDIN, HIGH);
      clockCycle();
   }
   digitalWrite(SDIN, LOW);
   clockCycle();
   digitalWrite(SDIN, HIGH);
   clockCycle();
   digitalWrite(SDIN, LOW);
   clockCycle();
   send_byte(d);                           // Transmits the byte
}


void send_byte(byte tx_b)  {        // SUBROUTINE: SEND TO THE DISPLAY THE BYTE IN tx_b
   byte i = 0;                                 // Bit index
   for(i=0; i<4; i++)  {
      if((tx_b & 0x01) == 1)  {
         digitalWrite(SDIN, HIGH);
      }
      else  {
         digitalWrite(SDIN, LOW);
      }
      clockCycle();
      tx_b = tx_b >> 1;
   }
   for(i=0; i<4; i++)  {
      digitalWrite(SDIN, LOW);
      clockCycle();
   }
   for(i=0; i<4; i++)  {
      if((tx_b & 0x1) == 0x1)  {           // <------- Change (?)
         digitalWrite(SDIN, HIGH);
      }
      else  {
         digitalWrite(SDIN, LOW);
      }
      clockCycle();
      tx_b = tx_b >> 1;
   }
   for(i=0; i<4; i++)  {
      digitalWrite(SDIN, LOW);
      clockCycle();
}  }


void clockCycle(void)  {               // SUBROUTINE: EXECUTE THE CLOCK SIGNAL CYCLE
//digitalWrite(CS, LOW);             // Sets LOW the /CS line of the display (optional, can be always low)
//   delayMicroseconds(1);
   digitalWrite(SCLK, LOW);         // Sets LOW the SCLK line of the display
      delayMicroseconds(1);
   digitalWrite(SCLK, HIGH);        // Sets HIGH the SCLK line of the display
      delayMicroseconds(1);
//digitalWrite(CS, HIGH);            // Sets HIGH the /CS line of the display (optional, can be always low)
//   delayMicroseconds(1);
}


void output(void)  {                              // SUBROUTINE: DISPLAYS THE FOUR STRINGS, THEN THE SAME IN REVERSE ORDER
   byte r = 0;                                         // Row index
   byte c = 0;                                         // Column index
   command(0x01);                               // Clears display (and cursor home)
      delay(2);                                        // After a clear display, a minimum pause of 1-2 ms is required
   for (r=0; r<ROW_N; r++)  {               // One row at a time
      command(new_line[r]);                  // Moves the cursor to the first column of that line
      for (c=0; c<COLUMN_N; c++)  {    // One character at a time
         data(TEXT[r][c]);                           // Displays the corresponding string
}  }  }


void setup(void)  {
   pinMode(SCLK, OUTPUT);        // Initializes Arduino pin for the SPI Clock line
   digitalWrite(SCLK, HIGH);         // Sets HIGH the SPI Clock line of the display
   pinMode(SDIN, OUTPUT);        // Initializes Arduino pin for the SPI MOSI line
   digitalWrite(SDIN, LOW);          // Sets LOW the SPI MOSI line of the display
//pinMode(SDOUT, INPUT);         // Initializes Arduino pin for the SPI MISO line (optional, can be not connected)
//pinMode(CS, OUTPUT);             // Initializes Arduino pin for the SPI Chip Select line (optional - for multiple slaves)
//digitalWrite(CS, HIGH);              // Sets HIGH the SPI Chip Select line of the display (optional, can be always low)
//pinMode(RES, OUTPUT);           // Initializes Arduino pin for the Reset line (optional)
//digitalWrite(RES, HIGH);            // Sets HIGH the Reset line of the display (optional, can be always high)
      delayMicroseconds(200);
   
   command(0x22 | rows);      // Function set: extended command set (RE=1), lines # (0x22 = 34)
      command(0x71);             // Function selection A: (0x71 = 113)
         data(0x5C);                   // Enable internal Vdd regulator at 5V I/O mode (default) (0x00 for disable: 2.8V I/O) (0x5C = 92)
   command(0x20 | rows);      // Function set: fundamental command set (RE=0) (exit from extended command set), lines # (0x20 = 32)

   command(0x08);                // Display ON/OFF control: display off, cursor off, blink off (default values) (0x08 = 8)

   command(0x22 | rows);      // Function set: extended command set (RE=1), lines # (0x22 = 34)
      command(0x79);             // OLED characterization: OLED command set enabled (SD=1) (0x79 = 121)
         command(0xD5);          // Set display clock divide ratio/oscillator frequency: (0xD5 = 213)
         command(0x70);           // Divide ratio=1, frequency=7 (default values) (0x70 = 112)
      command(0x78);              // OLED characterization: OLED command set disabled (SD=0) (exit from OLED command set) (0x78 = 120)
      command(0x09);              // Extended function set (RE=1): 5-dot font, B/W inverting disabled (def. val.), 3/4 lines (0x09 = 9)
      command(0x06);              // Entry Mode set - COM/SEG direction: COM0->COM31, SEG99->SEG0 (BDC=1, BDS=0) (0x06 = 6)
      command(0x72);              // Function selection B: (0x72 = 114)
         data(0x0A);                    // ROM/CGRAM selection: ROM C, CGROM=250, CGRAM=6 (ROM=10, OPR=10) (0x0A = 10)
      command(0x79);              // OLED characterization: OLED command set enabled (SD=1) (0x79 = 121)
         command(0xDA);          // Set SEG pins hardware configuration: (0xDA = 218)
            command(0x10);        // Alternative odd/even SEG pin, disable SEG left/right remap (default values) (0x10 = 16)
         command(0xDC);          // Function selection C: (0xDC = 220)
            command(0x00);        // Internal VSL, GPIO input disable (0x00 = 0)
         command(0x81);           // Set contrast control: (0x81 = 129)
            command(0x7F);        // Contrast=127 (default value) (0x7F = 127)
         command(0xD9);          // Set phase length: (0xD9 = 217)
            command(0xF1);        // Phase2=15, phase1=1 (default: 0x78) (0x78 = 120)
         command(0xDB);          // Set VCOMH deselect level: (0xDB = 219)
            command(0x40);        // VCOMH deselect level=1 x Vcc (default: 0x20=0,77 x Vcc) (0x40 = 64)
      command(0x78);              // OLED characterization: OLED command set disabled (SD=0) (exit from OLED command set) (0x78 = 120)
   command(0x20 | rows);       // Function set: fundamental command set (RE=0) (exit from extended command set), lines # (0x20 = 32)

   command(0x01);               // Clear display (0x01 = 1)
      delay(2);
   command(0x80);               // Set DDRAM address 0x00 in address counter (cursor home) (default value) (0x80 = 128)
   command(0x0C);               // Display ON/OFF control: display ON, cursor off, blink off (0x0C = 12)
      delay(250);
}


void loop(void)  {
    command(0x22 | rows);
        command(0x79);
            command(0x81);  //set contrast control
            command(0x00);  //set contrast control
        command(0x78);
    command(0x20 | rows);
    output();                            // Execute subroutine "output"
        delay(2000);

    command(0x22 | rows);
        command(0x79);
             command(0x81);  //set contrast control
             command(0xFF);  //set contrast control
        command(0x78);
    command(0x20 | rows);
    output();                            // Execute subroutine "output"
        delay(2000);
}

DataL:
Could you explain each element of your code excerpt? I understand the meaning of "char buff[21];" but not the rest.

snprintf (buff, sizeof(buff), "Value= %d", invalue );

snprintf() outputs characters to a string buffer buff which is of length sizeof(buff)
The string printed is "Value= %d" where the %d indicates there is an int ("decimal") variable to follow, in this example 'invalue'.

You can then print the string in 'buff' in the same way as you said the examples used the TEXT arrays.

Yours,
TonyWilk

see: snprintf

  • although I believe the standard Arduino version does not print float variables.

formatting is as per printf()

I haven't seen any example codes for this display using lcd.print().

Hence my request that you post your code, which I have now seen.

Thanks for the explanation Tony,

I implemented that in the code but seem to be getting similar errors:

invalid types 'char[byte {aka unsigned char}]' for array subscript

In line: data(buff[r]

);

of this sketch:

[code]
const byte inPin = A0; //Analog Sensor Input
int inValue; //Analog Sensor Value

char buff [13];

const byte COLUMN_N = 20;          // Number of display columns
const byte ROW_N = 4;                   // Number of display rows

const byte SCLK = 13;                     // Arduino pin assigned to the SCLK line
const byte SDIN = 11;                     // Arduino pin assigned to the SID line

const byte TEXT[4][21] = {"1-Newhaven Display--",
                                          "2-------Test--------",
                                          "3-16/20-Characters--",
                                          "4!@#$%^&*()_+{}[]<>?"};         // Strings to be displayed

byte new_line[4] = {0x80, 0xA0, 0xC0, 0xE0};            // DDRAM address for each line of the display (128, 160, 192, 224)
byte rows = 0x08;                                                     // Display mode: 2/4 lines; default (0x08 = 8)


void command(byte c)  {              // SUBROUTINE: PREPARES THE TRANSMISSION OF A COMMAND
   byte i = 0;                                  // Bit index
   for(i=0; i<5; i++)  {
      digitalWrite(SDIN, HIGH);
      clockCycle();
   }
   for(i=0; i<3; i++)  {
      digitalWrite(SDIN, LOW);
      clockCycle();
   }
   send_byte(c);                             // Transmits the byte
}


void data(byte d)  {                    // SUBROUTINE: PREPARES THE TRANSMISSION OF A BYTE OF DATA
   byte i = 0;                               // Bit index
   for(i=0; i<5; i++)  {
      digitalWrite(SDIN, HIGH);
      clockCycle();
   }
   digitalWrite(SDIN, LOW);
   clockCycle();
   digitalWrite(SDIN, HIGH);
   clockCycle();
   digitalWrite(SDIN, LOW);
   clockCycle();
   send_byte(d);                           // Transmits the byte
}


void send_byte(byte tx_b)  {        // SUBROUTINE: SEND TO THE DISPLAY THE BYTE IN tx_b
   byte i = 0;                                 // Bit index
   for(i=0; i<4; i++)  {
      if((tx_b & 0x01) == 1)  {
         digitalWrite(SDIN, HIGH);
      }
      else  {
         digitalWrite(SDIN, LOW);
      }
      clockCycle();
      tx_b = tx_b >> 1;
   }
   for(i=0; i<4; i++)  {
      digitalWrite(SDIN, LOW);
      clockCycle();
   }
   for(i=0; i<4; i++)  {
      if((tx_b & 0x1) == 0x1)  {           // <------- Change (?)
         digitalWrite(SDIN, HIGH);
      }
      else  {
         digitalWrite(SDIN, LOW);
      }
      clockCycle();
      tx_b = tx_b >> 1;
   }
   for(i=0; i<4; i++)  {
      digitalWrite(SDIN, LOW);
      clockCycle();
}  }


void clockCycle(void)  {               // SUBROUTINE: EXECUTE THE CLOCK SIGNAL CYCLE
//digitalWrite(CS, LOW);             // Sets LOW the /CS line of the display (optional, can be always low)
//   delayMicroseconds(1);
   digitalWrite(SCLK, LOW);         // Sets LOW the SCLK line of the display
      delayMicroseconds(1);
   digitalWrite(SCLK, HIGH);        // Sets HIGH the SCLK line of the display
      delayMicroseconds(1);
//digitalWrite(CS, HIGH);            // Sets HIGH the /CS line of the display (optional, can be always low)
//   delayMicroseconds(1);
}


void output(void)  {                              // SUBROUTINE: DISPLAYS THE FOUR STRINGS, THEN THE SAME IN REVERSE ORDER
   inValue = analogRead(inPin);
   snprintf (buff, 13, "Value = %d", inValue );

   byte r = 0;                                         // Row index
   byte c = 0;                                         // Column index
   command(0x01);                               // Clears display (and cursor home)
      delay(2);                                        // After a clear display, a minimum pause of 1-2 ms is required
   for (r=0; r<ROW_N; r++)  {               // One row at a time
      command(new_line[r]);                  // Moves the cursor to the first column of that line
      for (c=0; c<COLUMN_N; c++)  {    // One character at a time
         data(buff[r][c]);                           // Displays the corresponding string
}  }  }


void setup(void)  {
   pinMode(SCLK, OUTPUT);        // Initializes Arduino pin for the SPI Clock line
   digitalWrite(SCLK, HIGH);         // Sets HIGH the SPI Clock line of the display
   pinMode(SDIN, OUTPUT);        // Initializes Arduino pin for the SPI MOSI line
   digitalWrite(SDIN, LOW);          // Sets LOW the SPI MOSI line of the display
//pinMode(SDOUT, INPUT);         // Initializes Arduino pin for the SPI MISO line (optional, can be not connected)
//pinMode(CS, OUTPUT);             // Initializes Arduino pin for the SPI Chip Select line (optional - for multiple slaves)
//digitalWrite(CS, HIGH);              // Sets HIGH the SPI Chip Select line of the display (optional, can be always low)
//pinMode(RES, OUTPUT);           // Initializes Arduino pin for the Reset line (optional)
//digitalWrite(RES, HIGH);            // Sets HIGH the Reset line of the display (optional, can be always high)
      delayMicroseconds(200);
   
   command(0x22 | rows);      // Function set: extended command set (RE=1), lines # (0x22 = 34)
      command(0x71);             // Function selection A: (0x71 = 113)
         data(0x5C);                   // Enable internal Vdd regulator at 5V I/O mode (default) (0x00 for disable: 2.8V I/O) (0x5C = 92)
   command(0x20 | rows);      // Function set: fundamental command set (RE=0) (exit from extended command set), lines # (0x20 = 32)

   command(0x08);                // Display ON/OFF control: display off, cursor off, blink off (default values) (0x08 = 8)

   command(0x22 | rows);      // Function set: extended command set (RE=1), lines # (0x22 = 34)
      command(0x79);             // OLED characterization: OLED command set enabled (SD=1) (0x79 = 121)
         command(0xD5);          // Set display clock divide ratio/oscillator frequency: (0xD5 = 213)
         command(0x70);           // Divide ratio=1, frequency=7 (default values) (0x70 = 112)
      command(0x78);              // OLED characterization: OLED command set disabled (SD=0) (exit from OLED command set) (0x78 = 120)
      command(0x09);              // Extended function set (RE=1): 5-dot font, B/W inverting disabled (def. val.), 3/4 lines (0x09 = 9)
      command(0x06);              // Entry Mode set - COM/SEG direction: COM0->COM31, SEG99->SEG0 (BDC=1, BDS=0) (0x06 = 6)
      command(0x72);              // Function selection B: (0x72 = 114)
         data(0x0A);                    // ROM/CGRAM selection: ROM C, CGROM=250, CGRAM=6 (ROM=10, OPR=10) (0x0A = 10)
      command(0x79);              // OLED characterization: OLED command set enabled (SD=1) (0x79 = 121)
         command(0xDA);          // Set SEG pins hardware configuration: (0xDA = 218)
            command(0x10);        // Alternative odd/even SEG pin, disable SEG left/right remap (default values) (0x10 = 16)
         command(0xDC);          // Function selection C: (0xDC = 220)
            command(0x00);        // Internal VSL, GPIO input disable (0x00 = 0)
         command(0x81);           // Set contrast control: (0x81 = 129)
            command(0x7F);        // Contrast=127 (default value) (0x7F = 127)
         command(0xD9);          // Set phase length: (0xD9 = 217)
            command(0xF1);        // Phase2=15, phase1=1 (default: 0x78) (0x78 = 120)
         command(0xDB);          // Set VCOMH deselect level: (0xDB = 219)
            command(0x40);        // VCOMH deselect level=1 x Vcc (default: 0x20=0,77 x Vcc) (0x40 = 64)
      command(0x78);              // OLED characterization: OLED command set disabled (SD=0) (exit from OLED command set) (0x78 = 120)
   command(0x20 | rows);       // Function set: fundamental command set (RE=0) (exit from extended command set), lines # (0x20 = 32)

   command(0x01);               // Clear display (0x01 = 1)
      delay(2);
   command(0x80);               // Set DDRAM address 0x00 in address counter (cursor home) (default value) (0x80 = 128)
   command(0x0C);               // Display ON/OFF control: display ON, cursor off, blink off (0x0C = 12)
      delay(250);
}


void loop(void)  {
    output();                            // Execute subroutine "output"
        delay(2000);
}

[/code]

Or if I exclude the [r][c] to leave: data(buff);   // Displays the corresponding string
it compiles okay but I get a flickery LCD screen of eighty letter "d"'s.

Assuming your data() function works, you are only outputting ONE line, not 4 lines !

//  for (r=0; r<ROW_N; r++)  {             // One row at a time
    r= 0;                    // ONLY ONE LINE !

    command(new_line[r]);                  // Moves the cursor to the first column of that line

    for (c=0; c<COLUMN_N; c++)  {          // One character at a time
       data(buff[c]);                      // Displays the corresponding string
    }

//}

Yours,
TonyWilk

You were right. And I failed to declare A0 as input.
Novices ey?

Thankyou so much for helping me out. I have it working in principle now which has cheered me up no end!

And for anyone else reading this, it's a lot easier on the eyes if you cancel out the clear display command.

I have one(two) last question though, if you don't mind.

When the values (there are two per line now) reduce, causing the number of characters in the string to reduce, the last character(s) on that line are garbage. I noticed it did that earlier when I had the buffer size larger than necessary.
Is there a way to fill that character with a space if otherwise not used, and also to set the second half of the string to start at a given location? To stop the second "Value =" from jumping around.

DataL:
And I failed to declare A0 as input.

No need for that. Pins are 'input' by default.
Leo..

Ahh, thanks. That's good to know.

Edit to add; I have solved the garbage problem by adding spaces to the end of the text in quotation marks. It doesn't seem to interfere with the next line down.

Setting text to a column isn't so easy.
If I write: c= 11;
and/or: for (c=11; c<COLUMN_N; c++) { }
it either ignores me and continues from the beginning of the line or it doesn't display that line at all.

DataL:
When the values (there are two per line now) reduce, causing the number of characters in the string to reduce, the last character(s) on that line are garbage. I noticed it did that earlier when I had the buffer size larger than necessary.
Is there a way to fill that character with a space if otherwise not used, and also to set the second half of the string to start at a given location? To stop the second "Value =" from jumping around.

#define CHARS_PER_LINE 20                 // Number of display characters/line
#define BUFF_SIZE (CHARS_PER_LINE + 1 )   // chars + 1 for '\0' terminator

char buff [BUFF_SIZE];    // Make buffer the full width of the display line

// function to fill buff with spaces and add '\0' terminator
//
void blankBuffer()
{
 for( int i=0; i<BUFF_SIZE; i++ )
 {
   buff[i]= ' ';
 }
 buff[BUFF_SIZE-1]= '\0';
}

// Then you can do....
//
int val1= 42;
int val2= 1234;

blankBuffer();

// print at start of buffer
snprintf (buff, BUFF_SIZE, "V1= %d", val1 );

// print starting at 11th character in buffer, note there are only BUFF_SIZE-10 characters left
snprintf (buff+10, BUFF_SIZE-10, "V2= %d", val2 );

Yours,
TonyWilk

Brilliant, thankyou. The blank buffer caused one object of garbage after each subsequent buffer alteration, but spaces in the quotation marks will do just fine.

The buffer+xx trick works a treat though.

Hi.
I know this is a bit old but I am facing a very similar problem.

Due to constraints of performance, it is expedient for me to write a single string to a display and I want to write a line with two values, one on each half of the line.

I tried TonyWilk's suggestion of using snprintf but inserting the first value into the buffer adds a null terminator after it. The second value is then ignored.

Is there another way to construct the string with the first value starting at position 0 and the second at position 10 in the 20 char buffer?

Thanks

I tried TonyWilk's suggestion of using snprintf but inserting the first value into the buffer adds a null terminator after it. The second value is then ignored.

Please post an example of that

First code uses the example concept (I hope) that was provided by TonyWilk.
I have written to Serial so I can cut/paste output.

#define LINE_WIDTH 20                 // Display width
#define BUFF_SIZE (LINE_WIDTH + 1 )   // allow '\0' terminator

char buff [BUFF_SIZE];    // Make buffer the full width of the display line
int val1 = -123;
int val2 = 456;

void setup() {
  Serial.begin(9600);

  snprintf (buff, BUFF_SIZE, "X=%d", val1 );

  // put val2 half way across screen
  snprintf (buff + (LINE_WIDTH / 2), BUFF_SIZE - (LINE_WIDTH / 2) , " Y=%d", val2 );

  Serial.println(buff);

}

void loop() {
  // put your main code here, to run repeatedly:

}

This gives the output: X=-123
in the serial monitor.

I then changed the code and positioned the second snprintf to hit the null terminator of the first one as follows:

#define LINE_WIDTH 20                 // Display width
#define BUFF_SIZE (LINE_WIDTH + 1 )   // allow '\0' terminator

char buff [BUFF_SIZE];    // Make buffer the full width of the display line
int val1 = -123;
int val2 = 456;

void setup() {
  Serial.begin(9600);

  snprintf (buff, BUFF_SIZE, "X=%d", val1 );

  // put val2 half way across screen
  snprintf (buff + 6, BUFF_SIZE - (LINE_WIDTH / 2) , " Y=%d", val2 );

  Serial.println(buff);

}

void loop() {
  // put your main code here, to run repeatedly:

}

which gives the output : X=-123 Y=456

I realise I could calculate the number of spaces needed, add them in etc. but I need performance.
I'm a real beginner in C and I seek the wisdom of others to find an efficient method of getting the string to contain the two variables so that they write to the display on each half of the line.

Thanks

UKHeliBob:
Please post an example of that

Above. (Still finding out how to do these things - apologies)

@Forkbeard

What display do you have. This thread originated because the OP had a Newhaven NHD-0420CW which runs on the US2066 controller and the HD44780 controller libraries were not useful.

How is your display connected to the arduino. parallel, i2c, spi?

Due to constraints of performance, it is expedient for me to write a single string to a display and I want to write a line with two values, one on each half of the line.

Why do you think that concatenating before printing is important for performance?

The display is connected by I2C.
I measured the times for writing, moving cursor and writing again (with appropriate length strings) against writing a single string. There’s a small advantage. However, I can’t factor in time for the assembly of the string until I find a suitable way to do it.
It may end up being insignificant but in the quest for knowledge, I was interested how those who know better than me would build the string.