conversion specifier

Hey everyone. I've managed to read 3 analog inputs from some sensors, and send them to VB6 and that's working fine. The problem I'm having is the varying number of digits in each variable.

What I was hoping to find out from this thread is what conversion specifier I would use to set these values(0-1023) to a constant set of digits(0000-1023). This way VB6 can pull four numbers off at a time and always have the correct value.

Thanks :smiley:

Another alternative is to add a termination character. E.g. for the three analog sensors you could send something like this:

1:12;2:458;3:9;

This way you have a free format that can easily be expanded to accomodate more sensors.

I'm not entirely sure how to use a termination character, or if it would help with the application that I'm working on. Will this make the sensors output a constant digit width no matter how small the number? I looked into conversion specifiers in C and I wasn't sure if employing them in the Arduino codeset was possible.

For example, if the sensor were to put out 4(out of a possible 1023), I would need that number to come out as 0004 or 4.000e0. On the other hand if the sensor were to put out 545, it would have to be 0545 or 5.450e-2.

I'm pretty stumped :-/

The most pragmatic solution I have seen in the past is to do something along the lines of

Untested

// some code reads an analog input
// and assign a value to an int 'val'

if (val < 1000) Serial.print("0");
if (val <  100) Serial.print("0");
if (val <   10) Serial.print("0");
Serial.print(val);

If you are going to do this for three sensors, you may want to put this into a function and call it three times.

If you use sprintf to do the conversion, you can use %4d to ensure a 4 character string from an integer. 1, 14, 189, and 4830 will all result in the same length string.

That sounds perfect guys. I'll give you a shout back when I give this a run in a day's time(school closed friday :frowning: ).

Thanks a lot. ;D

You can use sprintf with a "%04d" format specifier to get exactly what you asked for (numbers of length 4 prefixed with zeroes). Depending on your application you may have a hard time however separating one sensor value from the other.

  // read two sensor values
  int val1, val2;
  char str[7];

  val1 = analogRead(0); // assume val1 is 123
  val2 = analogRead(1); // assume val2 is 45

  // Alternative 1: Fixed length zero prefixed numbers
  sprintf(str,"%04d", val1);
  Serial.print(str);
  sprintf(str,"%04d", val2);
  Serial.print(str);
  // in your VB application you will then receive
  // the string "01230045"

  // Alternative 2: using separator and sensor prefix
  Serial.print("1:");
  Serial.print(val1);
  Serial.write(';');
  Serial.print("2:");
  Serial.print(val2);
  Serial.write(';');
  // in your VB application you will then receive
  // the string "1:123;2:45;"

Depending on your VB skills, you may find it easier to extract sensor values using the second alternative. There is also the issue to consider when the two apps (Arduino, VB) are out of sync. E.g. you start receiving in VB when Arduino is in the midst of transferring a sensor value. In that case it may be difficult to sync up again with a fixed length scheme.

school closed friday :frowning:

I like your attitude, CarRamRod! :wink:

This worked perfectly. Unfortunately I've been having trouble syncing the vb read function to the Arduino serial write function.

I have the Arduino serial write set on a 1000ms delay, so there's a big window for setting a corresponding delay on VB6 that would fall within that.

Since I have a digit length of 12 on every transmission into VB6, I think it would be best to set up an if loop to check for that as a condition.

The function would consist of delay(<1000), then serial read. Think this would work okay?

Thanks for the help :slight_smile:

Nvm I have the transmissions between VB6 and the Arduino sync'd.

Thanks for all your help guys ;D