update

Dependencies:   Air_Quality DHT mbed-src

Fork of mbed_Wiznet_W7500 by Seo-young Yoon

Committer:
M_J
Date:
Wed Aug 12 04:52:58 2015 +0000
Revision:
2:37c5a8883978
Parent:
1:f9339d95123d
update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ysy00700 0:d0e594c3fb6c 1 #include "mbed.h"
ysy00700 0:d0e594c3fb6c 2 #include "DHT.h"
ysy00700 1:f9339d95123d 3 #include"Air_Quality.h"
ysy00700 0:d0e594c3fb6c 4
ysy00700 1:f9339d95123d 5 AirQuality airqualitysensor;
ysy00700 1:f9339d95123d 6 int current_quality = -1;
ysy00700 1:f9339d95123d 7 PinName analogPin = A0;
ysy00700 1:f9339d95123d 8
M_J 2:37c5a8883978 9 DHT sensor(D4, DHT11);
M_J 2:37c5a8883978 10 InterruptIn motion(D3);
ysy00700 0:d0e594c3fb6c 11 AnalogIn luminance(A1);
ysy00700 0:d0e594c3fb6c 12
ysy00700 0:d0e594c3fb6c 13 DigitalOut led_R(LED_RED);
ysy00700 0:d0e594c3fb6c 14 DigitalOut led_G(LED_GREEN);
ysy00700 0:d0e594c3fb6c 15 DigitalOut led_B(LED_BLUE);
ysy00700 0:d0e594c3fb6c 16
ysy00700 0:d0e594c3fb6c 17 int motion_detected = 0;
ysy00700 0:d0e594c3fb6c 18
ysy00700 1:f9339d95123d 19 // Interrupt Handler
ysy00700 1:f9339d95123d 20 void AirQualityInterrupt()
ysy00700 1:f9339d95123d 21 {
ysy00700 1:f9339d95123d 22 AnalogIn sensor(analogPin);
ysy00700 1:f9339d95123d 23 airqualitysensor.last_vol = airqualitysensor.first_vol;
ysy00700 1:f9339d95123d 24 airqualitysensor.first_vol = sensor.read()*1000;
ysy00700 1:f9339d95123d 25 airqualitysensor.timer_index = 1;
ysy00700 1:f9339d95123d 26 }
ysy00700 1:f9339d95123d 27
ysy00700 0:d0e594c3fb6c 28 void irq_handler(void)
ysy00700 0:d0e594c3fb6c 29 {
ysy00700 0:d0e594c3fb6c 30 motion_detected = 1;
ysy00700 0:d0e594c3fb6c 31 }
ysy00700 0:d0e594c3fb6c 32
ysy00700 0:d0e594c3fb6c 33 int main()
ysy00700 0:d0e594c3fb6c 34 {
ysy00700 0:d0e594c3fb6c 35 int error = 0;
ysy00700 0:d0e594c3fb6c 36 float hum = 0.0f;
ysy00700 0:d0e594c3fb6c 37 float cel = 0.0f;
ysy00700 0:d0e594c3fb6c 38
ysy00700 1:f9339d95123d 39 airqualitysensor.init(analogPin, AirQualityInterrupt);
ysy00700 1:f9339d95123d 40
ysy00700 0:d0e594c3fb6c 41 motion.rise(&irq_handler);
ysy00700 0:d0e594c3fb6c 42
ysy00700 0:d0e594c3fb6c 43 while(1)
ysy00700 0:d0e594c3fb6c 44 {
M_J 2:37c5a8883978 45 wait(0.3);
ysy00700 0:d0e594c3fb6c 46 error = sensor.readData();
ysy00700 0:d0e594c3fb6c 47
ysy00700 0:d0e594c3fb6c 48 if (0 == error) {
ysy00700 0:d0e594c3fb6c 49 hum = sensor.ReadHumidity();
ysy00700 0:d0e594c3fb6c 50 cel = sensor.ReadTemperature(CELCIUS);
M_J 2:37c5a8883978 51 printf("1. Humidity : %4.2f\r\n\n", hum);
M_J 2:37c5a8883978 52 wait(0.3);
M_J 2:37c5a8883978 53 printf("2. Temperature in Celcius : %2.2f\r\n\n", cel);
M_J 2:37c5a8883978 54 wait(0.3);
ysy00700 0:d0e594c3fb6c 55 }
ysy00700 0:d0e594c3fb6c 56 else {
M_J 2:37c5a8883978 57 //printf("Error : %d\n", error);
ysy00700 0:d0e594c3fb6c 58 }
ysy00700 0:d0e594c3fb6c 59
ysy00700 0:d0e594c3fb6c 60 if(motion_detected) {
ysy00700 0:d0e594c3fb6c 61 motion_detected = 0;
M_J 2:37c5a8883978 62
M_J 2:37c5a8883978 63 printf("3. Something move!\r\n\n");
M_J 2:37c5a8883978 64 wait(0.3);
ysy00700 0:d0e594c3fb6c 65 }
ysy00700 0:d0e594c3fb6c 66
ysy00700 0:d0e594c3fb6c 67 if(luminance.read()){
ysy00700 0:d0e594c3fb6c 68
ysy00700 0:d0e594c3fb6c 69 if(0.1<=luminance.read()&&luminance.read()<=0.3){
ysy00700 1:f9339d95123d 70 led_R=0; led_G=0; led_B=1;} // yellow LED on
ysy00700 0:d0e594c3fb6c 71
ysy00700 0:d0e594c3fb6c 72 else{
ysy00700 1:f9339d95123d 73 led_R=1; led_G=1; led_B=1;} // led off
ysy00700 0:d0e594c3fb6c 74
ysy00700 1:f9339d95123d 75 printf("4. Luminance: %f\r\n\n", luminance.read());
M_J 2:37c5a8883978 76 wait(0.3);
ysy00700 0:d0e594c3fb6c 77 }
ysy00700 0:d0e594c3fb6c 78 else {
ysy00700 1:f9339d95123d 79 led_R=0; led_G=0; led_B=0; // white LED on
ysy00700 1:f9339d95123d 80 printf("4. Luminance: %f\r\n\n", luminance.read());
M_J 2:37c5a8883978 81 wait(0.3);
ysy00700 0:d0e594c3fb6c 82 }
ysy00700 1:f9339d95123d 83 //air quality sensor
ysy00700 1:f9339d95123d 84 current_quality=airqualitysensor.slope();
ysy00700 1:f9339d95123d 85 if (current_quality >= 0) { // if a valid data returned.
M_J 2:37c5a8883978 86 if (current_quality == 0)
M_J 2:37c5a8883978 87 printf(" High pollution! Force signal active\n\r\n");
M_J 2:37c5a8883978 88 else if (current_quality == 1)
M_J 2:37c5a8883978 89 printf(" High pollution!\n\r\n");
M_J 2:37c5a8883978 90 else if (current_quality == 2)
M_J 2:37c5a8883978 91 printf(" Low pollution!\n\n\r");
M_J 2:37c5a8883978 92 else if (current_quality == 3)
M_J 2:37c5a8883978 93 printf(" Fresh air!\n\r\n");
ysy00700 1:f9339d95123d 94 }
ysy00700 0:d0e594c3fb6c 95 }
ysy00700 0:d0e594c3fb6c 96 }