-
Notifications
You must be signed in to change notification settings - Fork 4
/
trinket_m0_temp_sensor.ino
149 lines (130 loc) · 3.22 KB
/
trinket_m0_temp_sensor.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <Adafruit_DotStar.h>
#include <SPI.h>
#include <math.h>
// the value of the 'other' resistor
#define SERIESRESISTOR 10000
// What pin to connect the sensor to
#define THERMISTORPIN A4
#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 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
#define NUMPIXELS 1
#define DATAPIN 7
#define CLOCKPIN 8
#define RED_LED 13
#define SECOND 1000
#define AMBIENT_TEMP 23
#define MAX_TEMP 60
#define IDLE_TEMP 30
#define RGB_OFF 0x000000
#define BLUE 0x0000FF
#define GREEN 0x00FF00
#define RED 0xFF0000
#define PINK 0xFF0EF0
#define TEMP_OFFSET 20
int temp_to_colors[40][2] = {
//ambient
{ 0, 0x00D4FF },
{ 1, 0x00E4FF },
{ 2, 0x00fff4 },
{ 3, 0x00ffd0 },
{ 4, 0x00ffa8 },
{ 5, 0x00ff83 },
{ 6, 0x00ff5c },
{ 7, 0x00ff36 },
{ 8, 0x00ff10 },
{ 9, 0x17ff00 },
//idle
{ 10, 0x3eff00 },
{ 11, 0x65ff00 },
{ 12, 0x8aff00 },
{ 13, 0xb0ff00 },
{ 14, 0xd7ff00 },
{ 15, 0xfdff00 },
{ 16, 0xFFfa00 },
{ 17, 0xFFf000 },
{ 18, 0xFFe600 },
{ 19, 0xFFdc00 },
// load
{ 20, 0xFFd200 },
{ 21, 0xFFc800 },
{ 22, 0xFFbe00 },
{ 23, 0xFFb400 },
{ 24, 0xFFaa00 },
{ 25, 0xFFa000 },
{ 26, 0xFF9600 },
{ 27, 0xFF8c00 },
{ 28, 0xFF8200 },
{ 29, 0xFF7800 },
{ 30, 0xFF6e00 },
{ 31, 0xFF6400 },
{ 32, 0xFF5a00 },
{ 33, 0xFF5000 },
{ 34, 0xFF4600 },
{ 35, 0xFF3c00 },
{ 36, 0xFF3200 },
{ 37, 0xFF2800 },
{ 38, 0xFF1e00 },
{ 39, 0xFF1400 },
};
Adafruit_DotStar rgbLed(NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BGR);
void setup() {
pinMode(RED_LED, OUTPUT);
led_off();
Serial.begin(9600);
rgbLed.begin();
rgbLed.show();
rgbLed.setBrightness(60);
rgbLed.show();
}
void led_on() { digitalWrite(RED_LED, HIGH); }
void led_off() { digitalWrite(RED_LED, LOW); }
void set_rgb_color(int color) {
rgbLed.setPixelColor(0, color);
rgbLed.show();
}
float take_reading() {
float average = 0.0;
for(byte i = 0; i < NUMSAMPLES; i++) {
float currentRead = analogRead(THERMISTORPIN);
// convert the value to resistance
currentRead = 1023 / currentRead - 1;
currentRead = SERIESRESISTOR / currentRead;
average += currentRead;
}
return average / NUMSAMPLES;
}
float calculate_temp(float resistance) {
float steinhart = resistance / 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 absolute temp to C
return steinhart;
}
void send_temp(float temp) {
led_on();
Serial.println(temp);
delay(SECOND);
led_off();
}
int temp_to_rgb(float temp) {
int i = (int)round(temp) - TEMP_OFFSET;
if (i >= 40) { return PINK; }
if (i < 0) { return BLUE; }
return temp_to_colors[i][1];
}
void loop()
{
float reading = take_reading();
float temp = calculate_temp(reading);
int rgb = temp_to_rgb(temp);
set_rgb_color(rgb);
send_temp(temp);
}