Using 5 sensors(luminance, motion, tempurature, humanity, air_quality) to control

Dependencies:   Air_Quality DHT mbed-src

main.cpp

Committer:
ysy00700
Date:
2015-07-21
Revision:
0:d0e594c3fb6c
Child:
1:f9339d95123d

File content as of revision 0:d0e594c3fb6c:

#include "mbed.h"
#include "DHT.h"

DHT sensor(A0, DHT11);
InterruptIn motion(D2);
AnalogIn luminance(A1);

DigitalOut led_R(LED_RED);
DigitalOut led_G(LED_GREEN);
DigitalOut led_B(LED_BLUE);

int motion_detected = 0;

void irq_handler(void)
{
    motion_detected = 1;
}

int main()
{
    int error = 0;
    float hum = 0.0f;
    float cel = 0.0f;
    
    int cnt = 0;
    motion.rise(&irq_handler);
    
    while(1)
    {
        wait(1);
        error = sensor.readData();
        
        if (0 == error) {
            hum = sensor.ReadHumidity();
            cel = sensor.ReadTemperature(CELCIUS);
            printf(" Humidity : %4.2f\r\n", hum);
            printf(" Temperature in Celcius : %2.2f\r\n", cel);
            
            {
                if (hum > 70) {  // if celcius is higher than 28, LED_RED on.
                    led_R = 0;   // LED_RED on
                    led_B = 1;   // LED_BLUE off
                }
                else{ 
                    led_R = 1;   // LED_RED off
                    led_B = 0;   // LED_BLUE on
                }
            }
            {
                if(cel > 28) {  // if celcius is higher than 28, LED_RED on.
                    led_R = 0;   // LED_RED on
                    led_B = 1;   // LED_BLUE off
                }
                else { 
                    led_R = 1;   // LED_RED off
                    led_B = 0;   // LED_BLUE on
                }
            }
        }
        else {
            printf("Error : %d\n", error);
        }
        
        if(motion_detected) {
            cnt++;
            motion_detected = 0;
            led_R = 1;
            led_G = 1;
            led_B = 1;
            
            printf("Something move%d\r\n", cnt);
            wait(1); 
        }
        
        if(luminance.read()){
            
            if(0.1<=luminance.read()&&luminance.read()<=0.3){
                RED=0; GREEN=0; BLUE=1;}   // yellow LED on
                
            else{
                RED=1; GREEN=1; BLUE=1;}   // led off
             
            printf("Luminance: %f\r\n", luminance.read());    
            wait(0.5f);
        }
        else {
            RED=0; GREEN=0; BLUE=0;    // white LED on 
            printf("Luminance: %f\r\n", luminance.read());
            wait(0.5f);         
        }
    }
}
/*
AnalogIn luminance(A0);
InterruptIn motion(D2);

DigitalOut RED(LED1, 1);
DigitalOut GREEN(LED2, 1);
DigitalOut BLUE(LED3, 1);

int motion_detected = 0;

void irq_handler(void)
{
    motion_detected = 1;
}

int main()
{
    int cnt = 0;
    motion.rise(&irq_handler);
    
    while (true) {
        
        //grove motion sensor
        if(motion_detected) {
            cnt++;
            motion_detected = 0;
            RED = 0;
            GREEN = 1;
            BLUE = 1;
            wait(1);
            printf("Something move%d\r\n", cnt);
        }
         
        //luminance sensor
        if(luminance.read()){
            
            if(0.1<=luminance.read()&&luminance.read()<=0.3){
                RED=0; GREEN=0; BLUE=1;}   // yellow LED on
                
            else{
                RED=1; GREEN=1; BLUE=1;}   // led off
             
            printf("Luminance: %f\r\n", luminance.read());    
            wait(0.5f);
        }
        else {
            RED=0; GREEN=0; BLUE=0;    // white LED on 
            printf("Luminance: %f\r\n", luminance.read());
            wait(0.5f);         
        }
                    
    }
}*/