how to control a servo with temp & humidity
Dependencies: DHT22 Servo mbed
Diff: main.cpp
- Revision:
- 5:86adb825bfd2
- Parent:
- 4:c778b9ed6508
- Child:
- 6:14000499d474
--- a/main.cpp Sat May 28 06:45:17 2016 +0000 +++ b/main.cpp Sat May 28 09:33:39 2016 +0000 @@ -1,13 +1,3 @@ -/* mbed Seeed Archlink Temperature Humidity and Servo starter code - * - * This program take the temperature humidity reading and writes to the USB serial - * The servo is actuated for each loop-iteration - * Fow how to view the serial output, please refer to the mbed serial cookbook: https://developer.mbed.org/handbook/Serial - * - * Neil Tan - */ - - #include "mbed.h" // this tells us to load mbed related functions #include "Servo.h" // library for the Servo #include "DHT22.h" // library for the Temp&Humidity sensor @@ -15,40 +5,46 @@ DigitalOut myled(LED1); // used as an output for the sensor Servo myservo(p10); // p10 works, using other pins is possible too DHT22 sensor(p6); // the pin of the connected grove port -Serial pc(USBTX, USBRX); // serial TX & RX - -// this code runs when the microcontroller starts up -int main() { +Ticker fan; + +void fanFunction() +{ + // YOUR CODE HERE: read the sensor, set temp and hum variables + myservo = !myservo; +} +// this code runs when the microcontroller starts up +int main() +{ bool status; - pc.printf("\r\nDHT Test program"); - pc.printf("\r\n******************\r\n"); - + float temp = 0; + float hum = 0; + // spin a main loop all the time while (1) { - // turn off the LED - myled = 1; - - // YOUR CODE HERE: get the status of the sensor checksum, successful or failed - - + // led hearbeat + myled = !myled; + status = sensor.sample(); + // sensor checksum successful if (status) { // printf the temperature and humidity from the sensor - pc.printf("Temperature is %f C \r\n", sensor.getTemperature()/10.0f); //the readings need to be divided by 10 - pc.printf("Humidity is %f \r\n", sensor.getHumidity()/10.0f); - - + temp = sensor.getTemperature()/10.0f; + hum = sensor.getHumidity()/10.0f; + printf("Temperature is %f C \r\n", temp); + printf("Humidity is %f \r\n", hum); + + // start fanning if temperature > 25C or humidity > 90% + if(temp > 25 && hum > 80) { + printf("Starting Fan! \r\n"); + fan.attach(fanFunction,0.5); + } else { + printf("Stopping Fan! \r\n"); + fan.detach(); + } } else { // sensor checksum failed - pc.printf("Error reading sample \r\n"); + printf("Error reading sample \r\n"); } - - myservo = 0.0f; - wait(5); - myservo = 1.0f; - wait(5); - - // turn on the LED - myled = 0; + wait(2.0f); } } \ No newline at end of file