How to control a servo with temp & humidity
Dependencies: DHT22 Servo mbed
Fork of Program4_ServoWithTempAndHumidity by
Seeed_Arch_link_ServoWithTempAndHumidity
Setup
- Connect the Temperature sensor as well as the Servo to the Seeeduino-Arch-Link board, and then connect the board to you PC using the micro-USB cable.
- The board mounts as a mass-storage device (like a USB drive). Verify that you can see it (drive name will be MBED).
- Go to http://developer.mbed.org
- Create an ARM mbed account if you do not have one.
- On the top right corner, click the Compiler button.
An IDE should open. Congratulations!
On Windows: To see debug messages, install the serial driver.
Debug messages: We can talk to the board via a serial port, but you might need some software. Read this doc and install required software (like PuTTY or CoolTermon Windows).
Locally: If you like things locally, you can do so by exporting to a supported toolchain.
I very much recommend to just use the online IDE, as it makes it easier for us, but if you want to continue hacking in the future, this is a nice way.
Seeed_Arch_link_ServoWithTempAndHumidity
- Go back to the compiler browser window and click F5 to refresh the page.
- Click the Import button, then click "Click Here to import from URL"
- Paste the URL https://developer.mbed.org/teams/znrobotics/code/Seeed_Arch_link_ServoWithTempAndHumidity/
- Double click to open main.cpp
- Here we intend to control the servo with temp & humidity
- Try to finish the code under' YOUR CODE HERE: to reverse myservo between 0 and 1 '
- Now press Compile
- A file downloads (Seeed_Arch_link_ServoWithTempAndHumidity.hex)
- Drag the file to the 'MBED' disk
- After flashing, hit the 'Reset' button to start the program.
- Now the servo will move based on the temperature reading! Try putting your finger on the temperature sensor to see it move!
main.cpp@6:14000499d474, 2016-05-28 (annotated)
- Committer:
- Maggie17
- Date:
- Sat May 28 09:37:28 2016 +0000
- Revision:
- 6:14000499d474
- Parent:
- 5:86adb825bfd2
modify
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 | { |
Maggie17 | 6:14000499d474 | 12 | // YOUR CODE HERE: to reverse myservo between 0 and 1 |
Maggie17 | 6:14000499d474 | 13 | |
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 | } |