Help with pointers and references

So I’ve been working with a DHT22 sensor on my Arduino UNO 3 and for the most part it’s going pretty well. However, when I reworked the basic DHT22 example sketch included in the IDE to use an array to hold the sensor values, it now is just printing 0.00.

/*
 * File:    dht22_test_003.ino
 * Author:  Uncle RedBeard
 * Date:    22-DEC-2014
 * Desc:    reading temp and humidity from dht22 
 */
#include <DHT.h>

#define DHTPIN 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

unsigned long interval = 15000;
unsigned long prevMillis = 0;

void setup(){
  Serial.begin(9600);

  Serial.println("System starting");
  Serial.println("Please wait.");

  dht.begin();
}

void loop(){

  if(((unsigned long)(millis() - prevMillis)) >= interval){
    prevMillis = millis();

    float tempData[3];
    float humidity = get_temp_data(&tempData[0]);
    float tempC = get_temp_data(&tempData[1]);
    float tempF = get_temp_data(&tempData[2]);

    Serial.println("*****************************");
    Serial.print("RH: ");
    Serial.print(humidity);
    Serial.println("%");
    Serial.print("Temperature: ");
    Serial.print(tempC);
    Serial.println("*C");
    Serial.print("Temperature: ");
    Serial.print(tempF);
    Serial.println("*F");  
  }
}

float get_temp_data(float *tData){
  tData[0] = dht.readHumidity();
  tData[1] = dht.readTemperature();
  tData[2] = dht.readTemperature(true);

  if(isnan(tData[0]) || isnan(tData[1]) || isnan(tData[2])){
    Serial.println("failed to read sensor");
  }
}

The code above prints the following:

System starting
Please wait.


RH: 0.00%
Temperature: 0.00C
Temperature: 0.00
F


RH: 0.00%
Temperature: 0.00C
Temperature: 0.00
F

Yes, I know this is probably way more complex than it needs to be, but I’d really like to figure out what I’m doing wrong. My gut tells me it has to be something with how I’m (obviously) incorrectly using pointers and references. Any assistance is greatly appreciated!

Thanks in advance!

You aren't returning from get_temp_data. However it looks like it should return void (nothing) anyway.

Maybe it should look like this:

void get_temp_data(float *tData){
  tData[0] = dht.readHumidity();
  tData[1] = dht.readTemperature();
  tData[2] = dht.readTemperature(true);

  if(isnan(tData[0]) || isnan(tData[1]) || isnan(tData[2])){
    Serial.println("failed to read sensor");
  }
}

Then your code that uses it fails. As the function sets three elements you do not call it three times, just once to populate the array, then use the array in your code:

    float tempData[3];

    get_temp_data( tempData );

    float humidity = tempData[0];
    float tempC = tempData[1];
    float tempF = tempData[2];

You aren’t returning from get_temp_data.

Actually, the function does return. It doesn’t actually return a value, so the statement should be “You aren’t returning anything from get_temp_data.”

Other than that, spot on!

Well, of course the function returns, and it also returns a value which was a default initialized float (0), this is a consequence of the OP not supplying a return statement and why the readings all said zero.