Analog In Number Format

Hi Folks,

I have the following code gathering the Ain data and appending a identifier character before writing to the serial port:

{
      // read and print the analog input on pins 0 -> 5:
      int i;
      char b[] = "@*$%^!";

      for (i = 0; b[i] != '\0'; i++)
      {
        Serial.print(b[i]);
        Serial.println(analogRead(i));
      }
      break;
    }

However, Occasionally the first and last lines skip the last characters and I get rogue readings.

Is there a way to format the output to 4 decimal places so that I get readings something like this:

@0001 *0024 $1024 %0011

etc? This way I can filter out the rogue readings afterwards.

Any advice greatly appreciated.

Best

Jim H

I do this on my setup by outputing all the values on one line and then reading the line twice at the processing end and just using the last line.

Hmmm, that's an interesting option but it would be much easier in terms of filtering if I could get the Arduino always to output a 4digit reading after the identifier character.

Jim

I think you could try this:

{
      // read and print the analog input on pins 0 -> 5:
      int i;
      char b[] = "@*$%^!";
                char buffer[6];

      for (i = 0; b[i] != '\0'; i++)
      {
        Serial.print(b[i]);
                  sprintf(buffer, "%4.04d", analogRead(i));
        Serial.println(buffer);      
                 }
      break;
    }

that should do it.

Thanks Spinlock.

I'm getting an error though:

"In function 'void do_command(char)': error: 'sprintf' was not declared in this scope"

Any ideas?

Jim H

Here is another way that doesn’t need sprintf

  int val = analogRead(i);
  for(int pad = 1000; val < pad; pad /= 10)
    Serial.print('0');
  Serial.print(val);

Its been running for 6 months without so much as a hiccup. Taking the second line means the often corrupted first line never mangles things. The order on the line can't change so the readings are always correct. The arduino outputs smoothed readings so I don't need to do any filtering.

This is how I get the stuff out of my arduino. Twenty odd variables all on one line. Reading it twice and discarding the first never fails. Several of the values are from analogue pins.

     Serial.print(voltm);
     Serial.print(" ");
     Serial.print(voltg);
     Serial.print(" ");
     Serial.print(tempin);
     Serial.print(" ");
     Serial.print(tempout);
     Serial.print(" ");
     Serial.print(light);
     Serial.print(" ");
     Serial.print(vmin);
     Serial.print(" ");
     Serial.print(vmax);
     Serial.print(" ");
     Serial.print(rev1);
     Serial.print(" ");
     Serial.print(rev2);
     Serial.print(" ");
     Serial.print(time);
     Serial.print(" ");
     Serial.print(timeon);
     Serial.print(" ");
     Serial.print(tempch);
     Serial.print(" ");
     Serial.print(min5);
     Serial.print(" ");
     Serial.print(light2);
     Serial.print(" ");
     Serial.print(dallas1);
     Serial.print(" ");
     Serial.print(dallas2);
     Serial.print(" ");
     Serial.print(dallas3);
     Serial.print(" ");
     Serial.print(dallas4);
     Serial.print(" ");
     Serial.print(dallas5);
     Serial.print(" "); 
     Serial.print(tempcount1);
     Serial.print(" "); 
     Serial.print(voltmramin);
     Serial.print(" "); 
     Serial.print(voltmramax);
     Serial.print(" ");
     Serial.print(wattage);
     Serial.print(" ");
     Serial.print(avwattage);
     Serial.print(" ");
     Serial.println("BITS");

You can see the end result here, it also has links to a how its done page :

http://pluggy.is-a-geek.com/index.html

I wanted a simple "print right justified" routine for analog readings, without the overhead of sprintf, the other day, so I wrote this:

// print an int, right-justified,  4 characters wide.
// Assumes i is in the range of 0-9999
void print4(int i)
{
  if (i > 999)
      Serial.print(i);
    else if (i > 99)
      {
        Serial.print(" ");
        Serial.print(i);
      }
    else if (i > 9)
      {
        Serial.print("  ");
        Serial.print(i);
      }
    else
      {
        Serial.print("   ");
        Serial.print(i);
      }
}

You can substitute "0"s for the spaces, and get the effect you want.

Ran

Mem - Brilliant, brilliant, brilliant!

A few tweaks and it works perfectly!

    // read and print the analog input on pins 0 -> 5:
    int i;
    char b[] = "@*$%^!";
    for (i = 0; b[i] != '\0'; i++)
    {
      Serial.print(b[i]);   
      int val = analogRead(i);
      for(int pad = 1000; val < pad; pad /= 10)
        Serial.print('0');
      Serial.println(val);
    }

Thanks a million.

Jim H