update
Dependencies: Air_Quality DHT mbed-src
Fork of mbed_Wiznet_W7500 by
main.cpp@0:d0e594c3fb6c, 2015-07-21 (annotated)
- Committer:
- ysy00700
- Date:
- Tue Jul 21 08:17:32 2015 +0000
- Revision:
- 0:d0e594c3fb6c
- Child:
- 1:f9339d95123d
temp, motion, luminance sensor function combination
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ysy00700 | 0:d0e594c3fb6c | 1 | #include "mbed.h" |
ysy00700 | 0:d0e594c3fb6c | 2 | #include "DHT.h" |
ysy00700 | 0:d0e594c3fb6c | 3 | |
ysy00700 | 0:d0e594c3fb6c | 4 | DHT sensor(A0, DHT11); |
ysy00700 | 0:d0e594c3fb6c | 5 | InterruptIn motion(D2); |
ysy00700 | 0:d0e594c3fb6c | 6 | AnalogIn luminance(A1); |
ysy00700 | 0:d0e594c3fb6c | 7 | |
ysy00700 | 0:d0e594c3fb6c | 8 | DigitalOut led_R(LED_RED); |
ysy00700 | 0:d0e594c3fb6c | 9 | DigitalOut led_G(LED_GREEN); |
ysy00700 | 0:d0e594c3fb6c | 10 | DigitalOut led_B(LED_BLUE); |
ysy00700 | 0:d0e594c3fb6c | 11 | |
ysy00700 | 0:d0e594c3fb6c | 12 | int motion_detected = 0; |
ysy00700 | 0:d0e594c3fb6c | 13 | |
ysy00700 | 0:d0e594c3fb6c | 14 | void irq_handler(void) |
ysy00700 | 0:d0e594c3fb6c | 15 | { |
ysy00700 | 0:d0e594c3fb6c | 16 | motion_detected = 1; |
ysy00700 | 0:d0e594c3fb6c | 17 | } |
ysy00700 | 0:d0e594c3fb6c | 18 | |
ysy00700 | 0:d0e594c3fb6c | 19 | int main() |
ysy00700 | 0:d0e594c3fb6c | 20 | { |
ysy00700 | 0:d0e594c3fb6c | 21 | int error = 0; |
ysy00700 | 0:d0e594c3fb6c | 22 | float hum = 0.0f; |
ysy00700 | 0:d0e594c3fb6c | 23 | float cel = 0.0f; |
ysy00700 | 0:d0e594c3fb6c | 24 | |
ysy00700 | 0:d0e594c3fb6c | 25 | int cnt = 0; |
ysy00700 | 0:d0e594c3fb6c | 26 | motion.rise(&irq_handler); |
ysy00700 | 0:d0e594c3fb6c | 27 | |
ysy00700 | 0:d0e594c3fb6c | 28 | while(1) |
ysy00700 | 0:d0e594c3fb6c | 29 | { |
ysy00700 | 0:d0e594c3fb6c | 30 | wait(1); |
ysy00700 | 0:d0e594c3fb6c | 31 | error = sensor.readData(); |
ysy00700 | 0:d0e594c3fb6c | 32 | |
ysy00700 | 0:d0e594c3fb6c | 33 | if (0 == error) { |
ysy00700 | 0:d0e594c3fb6c | 34 | hum = sensor.ReadHumidity(); |
ysy00700 | 0:d0e594c3fb6c | 35 | cel = sensor.ReadTemperature(CELCIUS); |
ysy00700 | 0:d0e594c3fb6c | 36 | printf(" Humidity : %4.2f\r\n", hum); |
ysy00700 | 0:d0e594c3fb6c | 37 | printf(" Temperature in Celcius : %2.2f\r\n", cel); |
ysy00700 | 0:d0e594c3fb6c | 38 | |
ysy00700 | 0:d0e594c3fb6c | 39 | { |
ysy00700 | 0:d0e594c3fb6c | 40 | if (hum > 70) { // if celcius is higher than 28, LED_RED on. |
ysy00700 | 0:d0e594c3fb6c | 41 | led_R = 0; // LED_RED on |
ysy00700 | 0:d0e594c3fb6c | 42 | led_B = 1; // LED_BLUE off |
ysy00700 | 0:d0e594c3fb6c | 43 | } |
ysy00700 | 0:d0e594c3fb6c | 44 | else{ |
ysy00700 | 0:d0e594c3fb6c | 45 | led_R = 1; // LED_RED off |
ysy00700 | 0:d0e594c3fb6c | 46 | led_B = 0; // LED_BLUE on |
ysy00700 | 0:d0e594c3fb6c | 47 | } |
ysy00700 | 0:d0e594c3fb6c | 48 | } |
ysy00700 | 0:d0e594c3fb6c | 49 | { |
ysy00700 | 0:d0e594c3fb6c | 50 | if(cel > 28) { // if celcius is higher than 28, LED_RED on. |
ysy00700 | 0:d0e594c3fb6c | 51 | led_R = 0; // LED_RED on |
ysy00700 | 0:d0e594c3fb6c | 52 | led_B = 1; // LED_BLUE off |
ysy00700 | 0:d0e594c3fb6c | 53 | } |
ysy00700 | 0:d0e594c3fb6c | 54 | else { |
ysy00700 | 0:d0e594c3fb6c | 55 | led_R = 1; // LED_RED off |
ysy00700 | 0:d0e594c3fb6c | 56 | led_B = 0; // LED_BLUE on |
ysy00700 | 0:d0e594c3fb6c | 57 | } |
ysy00700 | 0:d0e594c3fb6c | 58 | } |
ysy00700 | 0:d0e594c3fb6c | 59 | } |
ysy00700 | 0:d0e594c3fb6c | 60 | else { |
ysy00700 | 0:d0e594c3fb6c | 61 | printf("Error : %d\n", error); |
ysy00700 | 0:d0e594c3fb6c | 62 | } |
ysy00700 | 0:d0e594c3fb6c | 63 | |
ysy00700 | 0:d0e594c3fb6c | 64 | if(motion_detected) { |
ysy00700 | 0:d0e594c3fb6c | 65 | cnt++; |
ysy00700 | 0:d0e594c3fb6c | 66 | motion_detected = 0; |
ysy00700 | 0:d0e594c3fb6c | 67 | led_R = 1; |
ysy00700 | 0:d0e594c3fb6c | 68 | led_G = 1; |
ysy00700 | 0:d0e594c3fb6c | 69 | led_B = 1; |
ysy00700 | 0:d0e594c3fb6c | 70 | |
ysy00700 | 0:d0e594c3fb6c | 71 | printf("Something move%d\r\n", cnt); |
ysy00700 | 0:d0e594c3fb6c | 72 | wait(1); |
ysy00700 | 0:d0e594c3fb6c | 73 | } |
ysy00700 | 0:d0e594c3fb6c | 74 | |
ysy00700 | 0:d0e594c3fb6c | 75 | if(luminance.read()){ |
ysy00700 | 0:d0e594c3fb6c | 76 | |
ysy00700 | 0:d0e594c3fb6c | 77 | if(0.1<=luminance.read()&&luminance.read()<=0.3){ |
ysy00700 | 0:d0e594c3fb6c | 78 | RED=0; GREEN=0; BLUE=1;} // yellow LED on |
ysy00700 | 0:d0e594c3fb6c | 79 | |
ysy00700 | 0:d0e594c3fb6c | 80 | else{ |
ysy00700 | 0:d0e594c3fb6c | 81 | RED=1; GREEN=1; BLUE=1;} // led off |
ysy00700 | 0:d0e594c3fb6c | 82 | |
ysy00700 | 0:d0e594c3fb6c | 83 | printf("Luminance: %f\r\n", luminance.read()); |
ysy00700 | 0:d0e594c3fb6c | 84 | wait(0.5f); |
ysy00700 | 0:d0e594c3fb6c | 85 | } |
ysy00700 | 0:d0e594c3fb6c | 86 | else { |
ysy00700 | 0:d0e594c3fb6c | 87 | RED=0; GREEN=0; BLUE=0; // white LED on |
ysy00700 | 0:d0e594c3fb6c | 88 | printf("Luminance: %f\r\n", luminance.read()); |
ysy00700 | 0:d0e594c3fb6c | 89 | wait(0.5f); |
ysy00700 | 0:d0e594c3fb6c | 90 | } |
ysy00700 | 0:d0e594c3fb6c | 91 | } |
ysy00700 | 0:d0e594c3fb6c | 92 | } |
ysy00700 | 0:d0e594c3fb6c | 93 | /* |
ysy00700 | 0:d0e594c3fb6c | 94 | AnalogIn luminance(A0); |
ysy00700 | 0:d0e594c3fb6c | 95 | InterruptIn motion(D2); |
ysy00700 | 0:d0e594c3fb6c | 96 | |
ysy00700 | 0:d0e594c3fb6c | 97 | DigitalOut RED(LED1, 1); |
ysy00700 | 0:d0e594c3fb6c | 98 | DigitalOut GREEN(LED2, 1); |
ysy00700 | 0:d0e594c3fb6c | 99 | DigitalOut BLUE(LED3, 1); |
ysy00700 | 0:d0e594c3fb6c | 100 | |
ysy00700 | 0:d0e594c3fb6c | 101 | int motion_detected = 0; |
ysy00700 | 0:d0e594c3fb6c | 102 | |
ysy00700 | 0:d0e594c3fb6c | 103 | void irq_handler(void) |
ysy00700 | 0:d0e594c3fb6c | 104 | { |
ysy00700 | 0:d0e594c3fb6c | 105 | motion_detected = 1; |
ysy00700 | 0:d0e594c3fb6c | 106 | } |
ysy00700 | 0:d0e594c3fb6c | 107 | |
ysy00700 | 0:d0e594c3fb6c | 108 | int main() |
ysy00700 | 0:d0e594c3fb6c | 109 | { |
ysy00700 | 0:d0e594c3fb6c | 110 | int cnt = 0; |
ysy00700 | 0:d0e594c3fb6c | 111 | motion.rise(&irq_handler); |
ysy00700 | 0:d0e594c3fb6c | 112 | |
ysy00700 | 0:d0e594c3fb6c | 113 | while (true) { |
ysy00700 | 0:d0e594c3fb6c | 114 | |
ysy00700 | 0:d0e594c3fb6c | 115 | //grove motion sensor |
ysy00700 | 0:d0e594c3fb6c | 116 | if(motion_detected) { |
ysy00700 | 0:d0e594c3fb6c | 117 | cnt++; |
ysy00700 | 0:d0e594c3fb6c | 118 | motion_detected = 0; |
ysy00700 | 0:d0e594c3fb6c | 119 | RED = 0; |
ysy00700 | 0:d0e594c3fb6c | 120 | GREEN = 1; |
ysy00700 | 0:d0e594c3fb6c | 121 | BLUE = 1; |
ysy00700 | 0:d0e594c3fb6c | 122 | wait(1); |
ysy00700 | 0:d0e594c3fb6c | 123 | printf("Something move%d\r\n", cnt); |
ysy00700 | 0:d0e594c3fb6c | 124 | } |
ysy00700 | 0:d0e594c3fb6c | 125 | |
ysy00700 | 0:d0e594c3fb6c | 126 | //luminance sensor |
ysy00700 | 0:d0e594c3fb6c | 127 | if(luminance.read()){ |
ysy00700 | 0:d0e594c3fb6c | 128 | |
ysy00700 | 0:d0e594c3fb6c | 129 | if(0.1<=luminance.read()&&luminance.read()<=0.3){ |
ysy00700 | 0:d0e594c3fb6c | 130 | RED=0; GREEN=0; BLUE=1;} // yellow LED on |
ysy00700 | 0:d0e594c3fb6c | 131 | |
ysy00700 | 0:d0e594c3fb6c | 132 | else{ |
ysy00700 | 0:d0e594c3fb6c | 133 | RED=1; GREEN=1; BLUE=1;} // led off |
ysy00700 | 0:d0e594c3fb6c | 134 | |
ysy00700 | 0:d0e594c3fb6c | 135 | printf("Luminance: %f\r\n", luminance.read()); |
ysy00700 | 0:d0e594c3fb6c | 136 | wait(0.5f); |
ysy00700 | 0:d0e594c3fb6c | 137 | } |
ysy00700 | 0:d0e594c3fb6c | 138 | else { |
ysy00700 | 0:d0e594c3fb6c | 139 | RED=0; GREEN=0; BLUE=0; // white LED on |
ysy00700 | 0:d0e594c3fb6c | 140 | printf("Luminance: %f\r\n", luminance.read()); |
ysy00700 | 0:d0e594c3fb6c | 141 | wait(0.5f); |
ysy00700 | 0:d0e594c3fb6c | 142 | } |
ysy00700 | 0:d0e594c3fb6c | 143 | |
ysy00700 | 0:d0e594c3fb6c | 144 | } |
ysy00700 | 0:d0e594c3fb6c | 145 | }*/ |