0

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) {

  Serial.begin(9600);

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

  ads1.begin();
  ads2.begin();
}

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
  delay(500);
}

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

Joe

  • 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 Jan 5 at 16:51
  • < X -89 Y -1989 Z -1096 > – Joe Lord Jan 5 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 Jan 5 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 Jan 5 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 Jan 5 at 17:50
2

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() {
    Serial.begin(9600);
    Serial.println(format5(15));
    Serial.println(format5(-15));
    Serial.println(format5(-1500));
    Serial.println(format5(1500));
}

void loop(){}

output:

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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