Tempaerature and Humidity sensor average code

Hello Everyone.

I am using Arduino Huzzah feather for SHT85 sensor. I have three such sensors connected to the Huzzah with the Adafruit TCA 9548A multiplexer.

The code that I have written records the temperature and humidity for all 3 sensors after every 3 seconds.

However I want each of the sensor to record temperature and humidity every second for 10 seconds and then print the average of the recorded values.

I am posting the code here. Any help is appreciated.

Tx.

// data and clock connections:
// TCA9548A SC2, SD2 ---- SHT85-1
// TCA9548A SC3, SD3 ---- SHT85-2
// TCA9548A SC4, SD4 ---- SHT85-3

#include <Wire.h>
#include “SHTSensor.h”

//creat one instance of the SHT85 sensor

SHTSensor sht1(SHTSensor::SHT3X);
SHTSensor sht2(SHTSensor::SHT3X);
SHTSensor sht3(SHTSensor::SHT3X);

#define TCAADDR 0x70
void tcaselect(uint8_t i) {
if (i > 7) return;

Wire.beginTransmission(TCAADDR);
Wire.write(1 << i);
Wire.endTransmission();
}

void setup(void)
{
Wire.begin();
Serial.begin(9600);
delay(1000);

// intialize SHT85-1
tcaselect(2);
sht1.init();

// intialize SHT85-2
tcaselect(3);
sht2.init();

// intialize SHT85-3
tcaselect(4);
sht3.init();
}

void loop(void){

//switch (tcaselect(x)) and read the sensor values

tcaselect(2);
sht1.readSample();
Serial.print("SHT85-1 : ");
Serial.print("RH: “);
Serial.print(sht1.getHumidity(), 2);
Serial.print(” ");
Serial.print(“T: “);
Serial.print(sht1.getTemperature(), 2);
//Serial.print(”\n”);

tcaselect(3);
sht2.readSample();
Serial.print(" SHT85-2: ");
Serial.print("RH: “);
Serial.print(sht2.getHumidity(), 2);
Serial.print(” ");
Serial.print(“T: “);
Serial.print(sht2.getTemperature(), 2);
//Serial.print(”\n”);

tcaselect(4);
sht3.readSample();
Serial.print(" SHT85-3: ");
Serial.print("RH: “);
Serial.print(sht3.getHumidity(), 2);
Serial.print(” ");
Serial.print(“T: “);
Serial.print(sht3.getTemperature(), 2);
Serial.print(”\n”);

delay(3000);
}

xAvg += some number

xAvg /= 2

a continuous running average

Would you edit my code and post it?

@OP

Check if the following codes come to your help:

// data and clock connections:
// TCA9548A SC2, SD2 ---- SHT85-1
// TCA9548A SC3, SD3 ---- SHT85-2
// TCA9548A SC4, SD4 ---- SHT85-3


#include <Wire.h>
#include "SHTSensor.h"

//creat one instance of the SHT85 sensor

SHTSensor sht1(SHTSensor::SHT3X);
SHTSensor sht2(SHTSensor::SHT3X);
SHTSensor sht3(SHTSensor::SHT3X);


float sht1Temp = 0;
float sht1Hum = 0;

float sht2Temp = 0;
float sht2Hum = 0;

float sht33Temp = 0;
float sht3Hum = 0;


#define TCAADDR 0x70
void tcaselect(uint8_t i) {
  if (i > 7) return;

  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();
}

void setup(void)
{
  Wire.begin();
  Serial.begin(9600);
  delay(1000);

  // intialize SHT85-1
  tcaselect(2);
  sht1.init();

  // intialize SHT85-2
  tcaselect(3);
  sht2.init();

  // intialize SHT85-3
  tcaselect(4);
  sht3.init();
}

void loop(void) 
{
  //switch (tcaselect(x)) and read the sensor values
  for (int i = 0; i < 10; i++)
  {
    tcaselect(2);
    sht1.readSample();
    sht1Hum += sht1.getHumidity();
    sht1Temp += sht1.getTemperature();

    tcaselect(3);
    sht2.readSample();
    sht2Hum += sht2.getHumidity();
    sht2Temp += sht2.getTemperature();

    tcaselect(4);
    sht3.readSample();
    sht3Temp += sht3.getHumidity();
    sht3Temp += sht3.getTemperature();

    delay(3000);
  }
  Serial.print("  SHT85-1:  ");
  Serial.print("RH1: ");
  Serial.print(sht1Hum / 10, 2);
  Serial.print("  ");
  Serial.print("T1:  ");
  Serial.print(sht1Temp / 10, 2);
  Serial.println();
  //-------------------------------
  Serial.print("  SHT85-2:  ");
  Serial.print("RH2: ");
  Serial.print(sht2Hum / 10, 2);
  Serial.print("  ");
  Serial.print("T2:  ");
  Serial.print(sht2Temp / 10, 2);
  Serial.println();
  //-------------------------------
  Serial.print("  SHT85-3:  ");
  Serial.print("RH3: ");
  Serial.print(sht3Hum / 10, 2);
  Serial.print("  ");
  Serial.print("T3:  ");
  Serial.print(sht3Temp / 10, 2);
  Serial.println();
  //-------------------------------
  sht1Hum = 0;
  sht1Temp = 0;

  sht2Hum = 0;
  sht2Temp = 0;

  sht3Hum = 0;
  sht3Temp = 0;
}