Go Down

Topic: Basic sensor read question HDC1080 (Read 92 times) previous topic - next topic

ElHefe

Oct 10, 2017, 11:38 pm Last Edit: Oct 11, 2017, 06:35 am by ElHefe
I fairly new at this. I am reading an HDC1080 sensor and printing to serial port - works fine. I would like to be able to operate on this changing value (humidity) but since it is I2C, not sure how to do it.

The .h and .cpp files have variable "float readH()" in them so I thought I could just do an if statement "compare" to drive an output HIGH, but that didn't work. I am missing something really basic and need to learn this but I need a shove in the right direction.

Code: [Select]
#include "ClosedCube_HDC1080.h"
ClosedCube_HDC1080 hdc1080;
int fanOutputPin = 13;
float readHumidity;

void setup()
{
  Serial.begin(115200);
  hdc1080.begin(0x40);

}

void loop()
{
  {
    Serial.print("T=");
    Serial.print(hdc1080.readTemperature());
    Serial.print("C, RH=");
    Serial.print(hdc1080.readHumidity());
    Serial.println("%");
  }
  if (readHumidity < 40)
{
  digitalWrite, fanOutputPin, HIGH;      //why doesnt this work?

}

delay(3000);
}



pylon

You need to assign the value to the variable first:

Code: [Select]
readHumidity = hdc1080.readHumidity();

After that you can compare as you tried in the above sketch.


The syntax for function calls requires braces around the parameters:

Code: [Select]
digitalWrite(fanOutputPin, HIGH);

You might have to include some code to reset the fanOutputPin, otherwise it will be on always once it's activated.

ElHefe

The fan is always on, regardless of the humidity. The program does not see my if statement.

jremington

See reply #1. If you have changed the code, post the new code, using code tags.

pylon

You also should define you fan pin as an output:

Code: [Select]
pinMode(fanOutputPin, OUTPUT);

and preset it to a known value:

Code: [Select]
digitalWrite(fanOutputPin, LOW);

Both code parts should be in the setup() routine.

This assumes that you fan is controlled like an LED on that ouptut pin. You probably should post a wiring diagram of your setup to be sure that the hardware reacts as we expect.

ElHefe

Hey it works! Thanks for the direction. This is a good learning experience and will take it to heart. I attached the simple wiring diagram. The fan is an NMB 12vdc .72mA. Eventually will run on a PID loop. Thanks again.


Code: [Select]
#include <Wire.h>
#include "ClosedCube_HDC1080.h"
ClosedCube_HDC1080 hdc1080;
int fanOutputPin = 13;
float readHumidity;


void setup()///////////////////////////////////////
{
  Serial.begin(115200);
  hdc1080.begin(0x40);
  pinMode(fanOutputPin, OUTPUT);
  digitalWrite(fanOutputPin, LOW);

}

void loop()
{
    readHumidity = hdc1080.readHumidity();

  {
    Serial.print("T=");
    Serial.print(hdc1080.readTemperature());
    Serial.print("C, RH=");
    Serial.print(hdc1080.readHumidity());
    Serial.println("%");

  }
  if (readHumidity < 40)
  {
    digitalWrite (fanOutputPin, HIGH);
  }
  else
  {
    digitalWrite (fanOutputPin, LOW);
  }
  delay(3000);
}


Go Up