Grove Temp sensor Hello world example
Dependencies: Grove_temp_sensor mbed
Grove_temp_sensor.h@2:772a59579086, 2016-03-10 (annotated)
- Committer:
- peipei123
- Date:
- Thu Mar 10 19:08:46 2016 +0000
- Revision:
- 2:772a59579086
- Parent:
- 0:3fa32ef93fdc
1
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 | 2:772a59579086 | 7 | /** Read the servo motors current position |
| peipei123 | 2:772a59579086 | 8 | * |
| peipei123 | 2:772a59579086 | 9 | * @param returns A normalised number 0.0-1.0 representing the full range. |
| peipei123 | 2:772a59579086 | 10 | */ |
| peipei123 | 0:3fa32ef93fdc | 11 | Grove_temp_sensor(PinName pin); |
| peipei123 | 2:772a59579086 | 12 | /** Read the servo motors current position |
| peipei123 | 2:772a59579086 | 13 | * |
| peipei123 | 2:772a59579086 | 14 | * @param returns A normalised number 0.0-1.0 representing the full range. |
| peipei123 | 2:772a59579086 | 15 | */ |
| peipei123 | 0:3fa32ef93fdc | 16 | Grove_temp_sensor(); |
| peipei123 | 2:772a59579086 | 17 | /** Read the servo motors current position |
| peipei123 | 2:772a59579086 | 18 | * |
| peipei123 | 2:772a59579086 | 19 | * @param returns A normalised number 0.0-1.0 representing the full range. |
| peipei123 | 2:772a59579086 | 20 | */ |
| peipei123 | 0:3fa32ef93fdc | 21 | float read(); |
| peipei123 | 0:3fa32ef93fdc | 22 | private: |
| peipei123 | 0:3fa32ef93fdc | 23 | //class sets up the AnalogIn pin |
| peipei123 | 0:3fa32ef93fdc | 24 | AnalogIn _pin; |
| peipei123 | 0:3fa32ef93fdc | 25 | }; |
| peipei123 | 0:3fa32ef93fdc | 26 | |
| peipei123 | 0:3fa32ef93fdc | 27 | Grove_temp_sensor::Grove_temp_sensor(PinName pin) : _pin(pin) |
| peipei123 | 0:3fa32ef93fdc | 28 | { |
| peipei123 | 0:3fa32ef93fdc | 29 | // _pin(pin) means pass pin to the AnalogIn constructor |
| peipei123 | 0:3fa32ef93fdc | 30 | } |
| peipei123 | 0:3fa32ef93fdc | 31 | |
| peipei123 | 0:3fa32ef93fdc | 32 | float Grove_temp_sensor::read() |
| peipei123 | 0:3fa32ef93fdc | 33 | { |
| peipei123 | 0:3fa32ef93fdc | 34 | |
| peipei123 | 0:3fa32ef93fdc | 35 | double a; |
| peipei123 | 0:3fa32ef93fdc | 36 | a=_pin.read()*1023; |
| peipei123 | 0:3fa32ef93fdc | 37 | double resistance=(float)(1023-a)*10000/a; |
| peipei123 | 0:3fa32ef93fdc | 38 | double temperature=1/(log(resistance/10000)/3975+1/298.15)-276.05; |
| peipei123 | 0:3fa32ef93fdc | 39 | //conver C to F; |
| peipei123 | 0:3fa32ef93fdc | 40 | temperature = (9.0*temperature)/5.0 + 32.0; |
| peipei123 | 0:3fa32ef93fdc | 41 | return temperature; |
| peipei123 | 0:3fa32ef93fdc | 42 | } |
| peipei123 | 0:3fa32ef93fdc | 43 | |
| peipei123 | 0:3fa32ef93fdc | 44 | |
| peipei123 | 0:3fa32ef93fdc | 45 |
Grove Temperature Sensor