Grove Temp sensor Hello world example
Dependencies: Grove_temp_sensor mbed
Grove_temp_sensor.h@0:3fa32ef93fdc, 2016-03-09 (annotated)
- Committer:
- peipei123
- Date:
- Wed Mar 09 00:00:46 2016 +0000
- Revision:
- 0:3fa32ef93fdc
- Child:
- 2:772a59579086
Hello world Example
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| peipei123 | 0:3fa32ef93fdc | 1 | #include "mbed.h" |
| peipei123 | 0:3fa32ef93fdc | 2 | |
| peipei123 | 0:3fa32ef93fdc | 3 | //Setup a new class for Grove_temp_sensor sensor |
| peipei123 | 0:3fa32ef93fdc | 4 | class Grove_temp_sensor |
| peipei123 | 0:3fa32ef93fdc | 5 | { |
| peipei123 | 0:3fa32ef93fdc | 6 | public: |
| peipei123 | 0:3fa32ef93fdc | 7 | Grove_temp_sensor(PinName pin); |
| peipei123 | 0:3fa32ef93fdc | 8 | Grove_temp_sensor(); |
| peipei123 | 0:3fa32ef93fdc | 9 | float read(); |
| peipei123 | 0:3fa32ef93fdc | 10 | private: |
| peipei123 | 0:3fa32ef93fdc | 11 | //class sets up the AnalogIn pin |
| peipei123 | 0:3fa32ef93fdc | 12 | AnalogIn _pin; |
| peipei123 | 0:3fa32ef93fdc | 13 | }; |
| peipei123 | 0:3fa32ef93fdc | 14 | |
| peipei123 | 0:3fa32ef93fdc | 15 | Grove_temp_sensor::Grove_temp_sensor(PinName pin) : _pin(pin) |
| peipei123 | 0:3fa32ef93fdc | 16 | { |
| peipei123 | 0:3fa32ef93fdc | 17 | // _pin(pin) means pass pin to the AnalogIn constructor |
| peipei123 | 0:3fa32ef93fdc | 18 | } |
| peipei123 | 0:3fa32ef93fdc | 19 | |
| peipei123 | 0:3fa32ef93fdc | 20 | float Grove_temp_sensor::read() |
| peipei123 | 0:3fa32ef93fdc | 21 | { |
| peipei123 | 0:3fa32ef93fdc | 22 | |
| peipei123 | 0:3fa32ef93fdc | 23 | double a; |
| peipei123 | 0:3fa32ef93fdc | 24 | a=_pin.read()*1023; |
| peipei123 | 0:3fa32ef93fdc | 25 | double resistance=(float)(1023-a)*10000/a; |
| peipei123 | 0:3fa32ef93fdc | 26 | double temperature=1/(log(resistance/10000)/3975+1/298.15)-276.05; |
| peipei123 | 0:3fa32ef93fdc | 27 | //conver C to F; |
| peipei123 | 0:3fa32ef93fdc | 28 | temperature = (9.0*temperature)/5.0 + 32.0; |
| peipei123 | 0:3fa32ef93fdc | 29 | return temperature; |
| peipei123 | 0:3fa32ef93fdc | 30 | } |
| peipei123 | 0:3fa32ef93fdc | 31 | |
| peipei123 | 0:3fa32ef93fdc | 32 | |
| peipei123 | 0:3fa32ef93fdc | 33 |
Grove Temperature Sensor