Smoothing example with 1 digit after the comma

Hi there,

Need your help again. :confused:

I have succesfully implemented the smoothing piece of code for my temperture readings.
Only with the example code i found here: https://www.arduino.cc/en/tutorial/smoothing

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:
  Serial.begin(9600);
  // 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] = analogRead(inputPin);
  // 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
  Serial.println(average);
  delay(1);        // delay in between reads for stability
}

..my output average reading is 23 instead of 23.4.
Somewhere in the smoothing code the digits after the comma are ignored.

My code, on line 99 is the average that prints only 23:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp; // I2C

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "29bd3d4e947446a9966fe41797e26809";
//char ssid[] = "STYLEMATHOT_1";
//char pass[] = "LetMeNotIn29";

char ssid[] = "CapljeBB";
char pass[] = "Ubzkn3jumh3v";
BlynkTimer timer;


int ledState = LOW;
unsigned long previousMillis = 0;
const long intervalled = 100;
unsigned long currentMillisled;

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 8000;

#define PWM D8
#define relais D3
#define pirsensor D0
int pirsensorstate;

char tension12[5];
const int numReadings = 10;

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

void setup() {
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(2000L, myTimerEvent);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }

  lcd.init();
  lcd.clear();
  lcd.backlight();
  lcd.setCursor(0, 0);                 // 0 place on row 0
  lcd.print("Opstraten...");
  Serial.begin(115200);
  delay(1000);
  lcd.clear();
  pinMode(pirsensor, INPUT);
  pinMode(PWM, OUTPUT);
  pinMode(relais, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);


  if (!bmp.begin()) {
    lcd.print("Zoeken sensor..");
    while (1);
  }


}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer

total = total - readings[readIndex];
  readings[readIndex] = (bmp.readTemperature());
  total = total + readings[readIndex];
  readIndex = readIndex + 1;
  if (readIndex >= numReadings) {
    readIndex = 0;
  }

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

  lcd.setCursor(0, 0);                 // 0 place on row 0
  lcd.print(bmp.readTemperature()-2.1, 1);
  lcd.print((char)223);
  lcd.print("C");
  lcd.print(" ");

  lcd.setCursor(0, 1);
  lcd.print(average);

  analogWrite(PWM, 500);
  pirsensorstate = digitalRead(pirsensor);

  //Serial.println(digitalRead(D6));


  //***********************************************************
  //                        PIR SENSOR
  //***********************************************************
  if (pirsensorstate == 1) {
    digitalWrite(relais, 1);
  }
  if (pirsensorstate == 0) {
    currentMillis = millis();
    if (currentMillis - startMillis >= period) {
      startMillis = currentMillis;
      pirsensorstate = digitalRead(pirsensor);
      if (pirsensorstate == 0) {
        digitalWrite(relais, 0);
      }
    }
  }
  //***********************************************************
  //                         BLINK THE LED
  //***********************************************************
  currentMillisled = millis();
  if (currentMillisled - previousMillis >= intervalled) {
    previousMillis = currentMillisled;
    if (ledState == LOW)
      ledState = HIGH;  // Note that this switches the LED *off*
    else
      ledState = LOW;   // Note that this switches the LED *on*
    digitalWrite(LED_BUILTIN, ledState);
  }
  //**************************************************************
}

void myTimerEvent() {
  dtostrf(bmp.readTemperature()-2.1, 5, 1, tension12);
  Blynk.virtualWrite(V5, tension12);
  Blynk.virtualWrite(V6, 30);
  Blynk.virtualWrite(V8, 12);
}

Can any one help the get the readings with 1 digit after the comma?

By the way the raw readings from the temperature sensor are with 3 digits after the comma.

It looks like you're doing everything with int. You know, int is short for integer. Do you know what integer means?

No...

Delta_G:
Do you know what integer means?

Kumalix:
No...

Have you considered looking that up? After you do, go to the reference section of this site and look through the data types. One should stand out as the only one that is going to work.

But after reading this http://www.cplusplus.com/doc/tutorial/variables/ i now understand i think.

Need to use Float i.s.o. int. correct? :frowning:

Tnx for the help..

Bingo

Tnx a lot! :slight_smile:

If i don't want to get an error i have to leave the readindex to be an int. like so:
int readIndex = 0; // the index of the current reading
float total = 0; // the running total
float average = 0;
Now i do get 2 digits after the comma but they are always two zero's. :confused:

But this line..
readings[readIndex] = (bmp.readTemperature());
..reads the sensor so the readindex should also be a float. Only then i get this error:


Arduino: 1.8.5 (Mac OS X), TD: 1.41, Board:"WeMos D1 R2 & mini, 80 MHz, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 921600"

project/WerkTafel_ner_6/WerkTafel_ner_6.ino: In function 'void loop()':
WerkTafel_ner_6:85: error: invalid types 'int [10][float]' for array subscript
total = total - readings[readIndex];
^
WerkTafel_ner_6:86: error: invalid types 'int [10][float]' for array subscript
readings[readIndex] = (bmp.readTemperature());
^
WerkTafel_ner_6:87: error: invalid types 'int [10][float]' for array subscript
total = total + readings[readIndex];
^
exit status 1
invalid types 'int [10][float]' for array subscript

No readIndex is an index to the array. It should be an int. You don't have an element 1.5 of the array. The array slots are numbered 0, 1, 2, ...

Got it have to be like this:

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

Thanks a lot! You are the best! Have you ever considerd being a teacher? :wink:

I taught chemistry for a good while.