-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialtemp.ino
120 lines (114 loc) · 3.4 KB
/
serialtemp.ino
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// TmonN
// Arduino Thermistor reader
// Copyright (C) <2017> <Xander Jansen>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#define THERMISTORNOMINAL 10000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer but is more 'smooth'
#define NUMSAMPLES 41
#define MEDIAN 21
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 10000
int samples[NUMSAMPLES];
int led2 = 13;
boolean onoff=false;
byte inputByte_0;
byte inputByte_1;
void setup(void) {
Serial.begin(9600);
analogReference(EXTERNAL);
digitalWrite(led2, HIGH);
}
void insertionSort(int list[])
{
int temp;
for(long i = 1; i < NUMSAMPLES; i++)
{
temp = list[i];
long j;
for(j = i-1; j >= 0 && list[j] > temp; j--)
{
list[j+1] = list[j];
}
list[j+1] = temp;
}
}
float MeasureTemp(int pin) {
uint8_t i;
float average =0;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(pin);
delay(5);
}
// average all the samples out
//for (i=0; i< NUMSAMPLES; i++) {
// average += samples[i];
//}
//average /= NUMSAMPLES;
insertionSort(samples);
average = samples[MEDIAN];
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
return steinhart;
}
void loop(void) {
String Temperature="";
if (Serial.available() == 2)
{
//Read buffer
inputByte_0 = Serial.read();
delay(100);
inputByte_1 = Serial.read();
delay(100);
}
//Check for start of Message
if(inputByte_0 == 16)
{
//Detect Command type
switch (inputByte_1)
{
case 128:
//Say hello
Serial.print("HELLO FROM ARDUINO");
break;
}
//Clear Message bytes
inputByte_0 = 0;
inputByte_1 = 0;
}
Temperature += (String)MeasureTemp(A0)+",";
Temperature += (String)MeasureTemp(A1)+",";
Temperature += (String)MeasureTemp(A2); // you can change these to the amount of sensors you need or dynamically add them.
Serial.println(String(Temperature));
if(onoff){
digitalWrite(led2, LOW);
}
else{
digitalWrite(led2, HIGH);
}
onoff = !onoff;
}