how to control a servo with temp & humidity

Dependencies:   DHT22 Servo mbed

Committer:
Maggie17
Date:
Sat May 28 04:38:45 2016 +0000
Revision:
2:3d87a559769a
Parent:
1:ccef6d6d9b62
Child:
3:98645a154332
add the comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nprobably 1:ccef6d6d9b62 1 /* mbed Seeed Archlink Temperature Humidity and Servo starter code
nprobably 1:ccef6d6d9b62 2 *
nprobably 1:ccef6d6d9b62 3 * This program take the temperature humidity reading and writes to the USB serial
nprobably 1:ccef6d6d9b62 4 * The servo is actuated for each loop-iteration
nprobably 1:ccef6d6d9b62 5 * Fow how to view the serial output, please refer to the mbed serial cookbook: https://developer.mbed.org/handbook/Serial
nprobably 1:ccef6d6d9b62 6 *
nprobably 1:ccef6d6d9b62 7 * Neil Tan
nprobably 1:ccef6d6d9b62 8 */
nprobably 1:ccef6d6d9b62 9
nprobably 1:ccef6d6d9b62 10
Maggie17 2:3d87a559769a 11 #include "mbed.h" // this tells us to load mbed related functions
Maggie17 2:3d87a559769a 12 #include "Servo.h" // library for the Servo
Maggie17 2:3d87a559769a 13 #include "DHT22.h" // library for the Temp&Humidity sensor
nprobably 0:671eadfdf703 14
Maggie17 2:3d87a559769a 15 DigitalOut myled(LED1); // used as an output for the sensor
Maggie17 2:3d87a559769a 16 Servo myservo(p4); // p4 works, using other pins is possible too
Maggie17 2:3d87a559769a 17 DHT22 sensor(p6); // the pin of the connected grove port
Maggie17 2:3d87a559769a 18 Serial pc(USBTX, USBRX); // serial TX & RX
nprobably 0:671eadfdf703 19
Maggie17 2:3d87a559769a 20 // this code runs when the microcontroller starts up
nprobably 0:671eadfdf703 21 int main() {
nprobably 0:671eadfdf703 22
nprobably 0:671eadfdf703 23 bool status;
nprobably 0:671eadfdf703 24 pc.printf("\r\nDHT Test program");
nprobably 0:671eadfdf703 25 pc.printf("\r\n******************\r\n");
Maggie17 2:3d87a559769a 26
Maggie17 2:3d87a559769a 27 // spin a main loop all the time
nprobably 0:671eadfdf703 28 while (1) {
Maggie17 2:3d87a559769a 29 // turn off the LED
nprobably 0:671eadfdf703 30 myled = 1;
Maggie17 2:3d87a559769a 31
Maggie17 2:3d87a559769a 32 // YOUR CODE HERE: get the status of the sensor checksum, sucessful or failed
Maggie17 2:3d87a559769a 33
Maggie17 2:3d87a559769a 34
Maggie17 2:3d87a559769a 35 // sensor checksum successful
nprobably 0:671eadfdf703 36 if (status) {
Maggie17 2:3d87a559769a 37 // printf the temperature from the sensor
nprobably 1:ccef6d6d9b62 38 pc.printf("Temperature is %f C \r\n", sensor.getTemperature()/10.0f); //the readings need to be divided by 10
Maggie17 2:3d87a559769a 39
Maggie17 2:3d87a559769a 40 // YOUR CODE HERE: printf the humidity from the sensor
Maggie17 2:3d87a559769a 41
Maggie17 2:3d87a559769a 42
Maggie17 2:3d87a559769a 43 } else { // sensor checksum failed
nprobably 0:671eadfdf703 44 pc.printf("Error reading sample \r\n");
nprobably 0:671eadfdf703 45 }
nprobably 0:671eadfdf703 46
nprobably 0:671eadfdf703 47 myservo = 0.0f;
nprobably 0:671eadfdf703 48 wait(5);
nprobably 0:671eadfdf703 49 myservo = 1.0f;
nprobably 0:671eadfdf703 50 wait(5);
nprobably 0:671eadfdf703 51
Maggie17 2:3d87a559769a 52 // turn on the LED
nprobably 0:671eadfdf703 53 myled = 0;
nprobably 0:671eadfdf703 54 }
nprobably 0:671eadfdf703 55 }