how to control a servo with temp & humidity
Dependencies: DHT22 Servo mbed
main.cpp@5:86adb825bfd2, 2016-05-28 (annotated)
- Committer:
- mbedAustin
- Date:
- Sat May 28 09:33:39 2016 +0000
- Revision:
- 5:86adb825bfd2
- Parent:
- 4:c778b9ed6508
- Child:
- 6:14000499d474
Working Lab
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Maggie17 | 2:3d87a559769a | 1 | #include "mbed.h" // this tells us to load mbed related functions |
Maggie17 | 2:3d87a559769a | 2 | #include "Servo.h" // library for the Servo |
Maggie17 | 2:3d87a559769a | 3 | #include "DHT22.h" // library for the Temp&Humidity sensor |
nprobably | 0:671eadfdf703 | 4 | |
Maggie17 | 2:3d87a559769a | 5 | DigitalOut myled(LED1); // used as an output for the sensor |
Maggie17 | 4:c778b9ed6508 | 6 | Servo myservo(p10); // p10 works, using other pins is possible too |
Maggie17 | 2:3d87a559769a | 7 | DHT22 sensor(p6); // the pin of the connected grove port |
mbedAustin | 5:86adb825bfd2 | 8 | Ticker fan; |
mbedAustin | 5:86adb825bfd2 | 9 | |
mbedAustin | 5:86adb825bfd2 | 10 | void fanFunction() |
mbedAustin | 5:86adb825bfd2 | 11 | { |
mbedAustin | 5:86adb825bfd2 | 12 | // YOUR CODE HERE: read the sensor, set temp and hum variables |
mbedAustin | 5:86adb825bfd2 | 13 | myservo = !myservo; |
mbedAustin | 5:86adb825bfd2 | 14 | } |
nprobably | 0:671eadfdf703 | 15 | |
mbedAustin | 5:86adb825bfd2 | 16 | // this code runs when the microcontroller starts up |
mbedAustin | 5:86adb825bfd2 | 17 | int main() |
mbedAustin | 5:86adb825bfd2 | 18 | { |
nprobably | 0:671eadfdf703 | 19 | bool status; |
mbedAustin | 5:86adb825bfd2 | 20 | float temp = 0; |
mbedAustin | 5:86adb825bfd2 | 21 | float hum = 0; |
mbedAustin | 5:86adb825bfd2 | 22 | |
Maggie17 | 2:3d87a559769a | 23 | // spin a main loop all the time |
nprobably | 0:671eadfdf703 | 24 | while (1) { |
mbedAustin | 5:86adb825bfd2 | 25 | // led hearbeat |
mbedAustin | 5:86adb825bfd2 | 26 | myled = !myled; |
mbedAustin | 5:86adb825bfd2 | 27 | status = sensor.sample(); |
mbedAustin | 5:86adb825bfd2 | 28 | |
Maggie17 | 2:3d87a559769a | 29 | // sensor checksum successful |
nprobably | 0:671eadfdf703 | 30 | if (status) { |
Maggie17 | 3:98645a154332 | 31 | // printf the temperature and humidity from the sensor |
mbedAustin | 5:86adb825bfd2 | 32 | temp = sensor.getTemperature()/10.0f; |
mbedAustin | 5:86adb825bfd2 | 33 | hum = sensor.getHumidity()/10.0f; |
mbedAustin | 5:86adb825bfd2 | 34 | printf("Temperature is %f C \r\n", temp); |
mbedAustin | 5:86adb825bfd2 | 35 | printf("Humidity is %f \r\n", hum); |
mbedAustin | 5:86adb825bfd2 | 36 | |
mbedAustin | 5:86adb825bfd2 | 37 | // start fanning if temperature > 25C or humidity > 90% |
mbedAustin | 5:86adb825bfd2 | 38 | if(temp > 25 && hum > 80) { |
mbedAustin | 5:86adb825bfd2 | 39 | printf("Starting Fan! \r\n"); |
mbedAustin | 5:86adb825bfd2 | 40 | fan.attach(fanFunction,0.5); |
mbedAustin | 5:86adb825bfd2 | 41 | } else { |
mbedAustin | 5:86adb825bfd2 | 42 | printf("Stopping Fan! \r\n"); |
mbedAustin | 5:86adb825bfd2 | 43 | fan.detach(); |
mbedAustin | 5:86adb825bfd2 | 44 | } |
Maggie17 | 2:3d87a559769a | 45 | } else { // sensor checksum failed |
mbedAustin | 5:86adb825bfd2 | 46 | printf("Error reading sample \r\n"); |
nprobably | 0:671eadfdf703 | 47 | } |
mbedAustin | 5:86adb825bfd2 | 48 | wait(2.0f); |
nprobably | 0:671eadfdf703 | 49 | } |
nprobably | 0:671eadfdf703 | 50 | } |