im trying to build a bit of test equipment that will talk to some software and it needs to send a fix string. when the voltage changes the string length does as well. i need to cap the results to 4 in length. (sorry for not knowing the correct terminology. this is the output i need - Where,

X = Flag CP Value
_ = Space
P = Polarity
C = CP Received value
Y = Flag Field Gradient
F = Field Gradient Received value
Z = Flag Contact CP
T = Contact CP Received value

There is also a line feed and carriage return present at the end of each string. The polarity indicator will only display a minus sign ‘-‘ if the received value is negative otherwise no assignation will be transmitted. Although the overall string length is constant the values for each measurement are right justified and hence a space will be present should the value of the measurement not necessitate the utilisation of all allocated character spaces. Sample Outputs

< X -89 Y -1989 Z -1096 >
< X 1039 Y 8 Z 78 >

this is my code so far

#include <Wire.h>
#include <Adafruit_ADS1015.h>

Adafruit_ADS1115 ads1(0x48);     // Use this for the 16-bit version
Adafruit_ADS1115 ads2(0x49);

void setup(void) {


  ads1.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      
  ads2.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      


void loop(void) {

  int16_t results01,results02,results03;

  results01 = ads1.readADC_Differential_0_1();
  results02 = ads1.readADC_Differential_2_3();
  results03 = ads2.readADC_Differential_0_1();

  Serial.print("X "); 
  Serial.print(round(results01 * multiplier));

  Serial.print(" Y "); 
  Serial.print(round(results02 * multiplier)); 

  Serial.print(" Z "); 
  Serial.print(round(results03 * multiplier));

  Serial.println();              // send a cr/lf

Any help pointing me in the right direction would be Amazing. Thanks in advance


  • What is it that your trying to "cap to 4 in length"? I'm assuming you have no control over how the device sends its string, and that you want to format the device's results for the terminal? Can you show a sample of what you want the end-result to look like?
    – JRobert
    Commented Jan 5, 2019 at 16:51
  • < X -89 Y -1989 Z -1096 >
    – Joe Lord
    Commented Jan 5, 2019 at 16:55
  • each of the reading im getting need to be 4 in length . im reading -2000 to +2000 but say when the voltage is -89 it shortens the string.
    – Joe Lord
    Commented Jan 5, 2019 at 16:56
  • So you want to print, e.g. "15" as "0015"? How about "-1500"? How can that take fewer than 5 printing characters?
    – JRobert
    Commented Jan 5, 2019 at 17:39
  • sorry i mean 5 characters. "___15" or " __-15" or "-1500" or "_1500" like i said im very new to this and im amazed i have got this far without asking for help . :)
    – Joe Lord
    Commented Jan 5, 2019 at 17:50

1 Answer 1


The printf() family of functions comes handy when you need this kind of control over the formatted output:

 * Format a number as 5 characters.
 * Returns the string in a statically allocated buffer.
char * format5(int x)
    static char buffer[6];  // 5 chars + terminating NUL
    snprintf(buffer, sizeof buffer, "%5d", x);
    return buffer;

void setup() {

void loop(){}


  • thanks for this. as been very new to this I have tried and cant get this working...
    – Joe Lord
    Commented Jan 6, 2019 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.