Output values as decimals to the tenth place (0.01) instead of whole numbers

Hi! My code is reading data from an IR sensor and the log formula is meant to convert the raw data into distance values and it was able to run well. I tried using a smoothing code to make the readings more precise however the values outputted are whole numbers but I need it to output the values as decimals. I tried using the float command but I was unsure of where to put in the code. I placed float pinFloat = readings[readIndex] * 5.0 / 1023.0 after the line where the analog input is but I got the same output values. Right now I have that line commented out. The code I have below is still able to run without error and I am using an Arduino uno.

#include <SharpIR.h>
#include <math.h>
#define logf
#define IR A0 // define signal pin
#define model 1080 // used 1080 because model GP2Y0A21YK0F is used
// ir: the pin where your sensor is attached
// model: an int that determines your sensor:  1080 for GP2Y0A21Y
//                                            20150 for GP2Y0A02Y
//                                            430 for GP2Y0A41SK   
2 to 15 cm GP2Y0A51SK0F  use 1080
4 to 30 cm GP2Y0A41SK0F / GP2Y0AF30 series  use 430
10 to 80 cm GP2Y0A21YK0F  use 1080
10 to 150 cm GP2Y0A60SZLF use 10150
20 to 150 cm GP2Y0A02YK0F use 20150
100 to 550 cm GP2Y0A710K0F  use 100550


  Reads repeatedly from an analog input, calculating a running average and
  printing it to the computer. Keeps ten readings in an array and continually
  averages them.

  The circuit:
  - analog sensor (potentiometer will do) attached to analog input 0

  created 22 Apr 2007
  by David A. Mellis  <dam@mellis.org>
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.


// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;

void setup() {
  // initialize serial communication with computer:
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;

void loop() {
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
 readings[readIndex] = 12.57*log(analogRead(inputPin))-63.507;
 //float pinFloat = readings[readIndex] * 5.0 / 1023.0;
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;

  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
  delay(1);        // delay in between reads for stability

0.01 would be the second, not the tenth, decimal place.

You cut and pasted an example without examining the code. It uses int types, you are using float calculations.

int readings[numReadings]; Here

By all means, put raw analogRead values into this array, but not the processed ones you have.

Serial.println( average, 6 );

// initialize all the readings to 0:Hint: they’re already all zero. The for loop in setup is superfluous.


Serial.println( average, 6 );

average is declared as an int so no decimal places are available to be printed

average is declared as an int so no decimal places are available to be printed

Ah, yes. Thanks for the heads up.