-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLM35.cpp
More file actions
80 lines (66 loc) · 1.88 KB
/
LM35.cpp
File metadata and controls
80 lines (66 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
LM35.cpp - Temperature Sensor Library for use LM35 - implementation
2012 - Levi Mota
*/
// include core Wiring API
#if defined(ARDUINO) && ARDUINO < 100
#include "WProgram.h"
#else
#include "Arduino.h"
#endif
// include this library's description file
#include "LM35.h"
const double DIVIDER_HIGH_RES = 9.309;
const double DIVIDER_LOW_RES = 2.048;
// Constructors /////////////////////////////////////////////////////////////////
// Function that handles the creation and setup of instances
LM35Sensor::LM35Sensor(void) {
// initialize this instance's variables
setSamples(500);
setHighRes(false);
}
LM35Sensor::LM35Sensor(int pSamples) {
// initialize this instance's variables
setSamples(pSamples);
setHighRes(false);
}
LM35Sensor::LM35Sensor(int pSamples, bool pHighRes) {
// initialize this instance's variables
setSamples(pSamples);
setHighRes(pHighRes);
}
// Public Methods //////////////////////////////////////////////////////////////
// Functions available in Wiring sketches, this library, and other libraries
void LM35Sensor::setSamples(int pSamples) {
samples = pSamples;
}
void LM35Sensor::setHighRes(bool pHighRes) {
highRes = pHighRes;
if (highRes) {
divider = DIVIDER_HIGH_RES;
analogReference(HIGH_RES);
} else {
divider = DIVIDER_LOW_RES;
analogReference(LOW_RES);
}
}
void LM35Sensor::read(int port) {
double tempSum = 0.0;
for (int i = 0; i < samples; i++) {
tempSum = tempSum + (analogRead(port) / divider);
}
celsius = tempSum / samples;
fahrenheit = 1.8 * celsius + 32;
kelvin = celsius + 273.15;
}
double LM35Sensor::getCelsius(void) {
return celsius;
}
double LM35Sensor::getFahrenheit(void) {
return fahrenheit;
}
double LM35Sensor::getKelvin(void) {
return kelvin;
}
// Private Methods /////////////////////////////////////////////////////////////
// Functions only available to other functions in this library