how to control a servo with temp & humidity
Dependencies: DHT22 Servo mbed
main.cpp@4:c778b9ed6508, 2016-05-28 (annotated)
- Committer:
- Maggie17
- Date:
- Sat May 28 06:45:17 2016 +0000
- Revision:
- 4:c778b9ed6508
- Parent:
- 3:98645a154332
- Child:
- 5:86adb825bfd2
modify
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:c778b9ed6508 | 16 | Servo myservo(p10); // p10 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 | 3:98645a154332 | 32 | // YOUR CODE HERE: get the status of the sensor checksum, successful or failed |
Maggie17 | 2:3d87a559769a | 33 | |
Maggie17 | 2:3d87a559769a | 34 | |
Maggie17 | 2:3d87a559769a | 35 | // sensor checksum successful |
nprobably | 0:671eadfdf703 | 36 | if (status) { |
Maggie17 | 3:98645a154332 | 37 | // printf the temperature and humidity 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 | 3:98645a154332 | 39 | pc.printf("Humidity is %f \r\n", sensor.getHumidity()/10.0f); |
Maggie17 | 2:3d87a559769a | 40 | |
Maggie17 | 2:3d87a559769a | 41 | |
Maggie17 | 2:3d87a559769a | 42 | } else { // sensor checksum failed |
nprobably | 0:671eadfdf703 | 43 | pc.printf("Error reading sample \r\n"); |
nprobably | 0:671eadfdf703 | 44 | } |
nprobably | 0:671eadfdf703 | 45 | |
nprobably | 0:671eadfdf703 | 46 | myservo = 0.0f; |
nprobably | 0:671eadfdf703 | 47 | wait(5); |
nprobably | 0:671eadfdf703 | 48 | myservo = 1.0f; |
nprobably | 0:671eadfdf703 | 49 | wait(5); |
nprobably | 0:671eadfdf703 | 50 | |
Maggie17 | 2:3d87a559769a | 51 | // turn on the LED |
nprobably | 0:671eadfdf703 | 52 | myled = 0; |
nprobably | 0:671eadfdf703 | 53 | } |
nprobably | 0:671eadfdf703 | 54 | } |