Robotics Kit Workshop / Mbed 2 deprecated Program2_TempAndHumidity

Dependencies:   DHT22 mbed

main.cpp

Committer:
Maggie17
Date:
2016-05-28
Revision:
3:f215a9bec026
Parent:
2:90b2eb3d14e6
Child:
4:9e198fc36ab5

File content as of revision 3:f215a9bec026:

#include "mbed.h"   // this tells us to load mbed related functions
#include "DHT.h"    // library for the Temp&Humidity sensor

DHT sensor(D4, DHT11); // used as an ouput for the sensor

// this code runs when the microcontroller starts up
int main()
{
    int error = 0;
    float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;

    // spin a main loop all the time
    while(1) {
        wait(2.0f);
        
        // read data from the sensor
        error = sensor.readData();
        
        // read successfully
        if (0 == error) {
            // read the temperature in CELCIUS
            c   = sensor.ReadTemperature(CELCIUS);
            
            // YOUR CODE HERE: read the temperature in FARENHEIT and KELVIN
            
            
            // read the humidity and do the calculation
            h   = sensor.ReadHumidity();
            dp  = sensor.CalcdewPoint(c, h);
            dpf = sensor.CalcdewPointFast(c, h);
            
            // printf the temperature and the humidity 
            printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f);
            printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n", h, dp, dpf);
            
        } else {  // read unseccessfully
            printf("Error: %d\n", error);
        }
    }
}