-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemperature_display.ino
142 lines (116 loc) · 3.74 KB
/
temperature_display.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
/*
Measure and display 24 hours of air temperature measurments
This sketch programs a Trinket M0 to measure the air temperature
every 15 minutes using a TMP36 temperature sensor.
A 128x32 OLED displays the current temperature along with a graph
of the previous 24 hours of measurements.
Components:
* Adafruit Trinket M0
* TMP36 temperature sensor
* Monochrome OLED 128x32 display
Circuit:
* TMP36 VIN to Trinket M0 3V
* TMP36 VOUT to Trinket M0 pin 4 (A4)
* TMP36 GND to Trinket M0 GND
* OLED SDA to Trinket M0 pin 0 (SDA)
* OLED SLC to Trinket M0 pin 2 (SLC)
* OLED VIN to Trinket M0 3V
* OLED GND to Trinket M0 GND
*/
#include <TMP36.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_DotStar.h>
// Onboard Dotstar object
Adafruit_DotStar strip = Adafruit_DotStar(1, INTERNAL_DS_DATA, INTERNAL_DS_CLK, DOTSTAR_BGR);
// Adafruit OLED object
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// OLED character params
#define CHAR_WIDTH 6
#define CHAR_HEIGHT 8
// TMP36 object
#define tempPin A4
TMP36 myTMP36(tempPin, 3.3);
// Define temperature array
const int nPoints = 96;
int tempArr[nPoints];
void setup()
{
Serial.begin(9600);
// Turn off dotstar
strip.setPixelColor(0, 0, 0, 0);
strip.show();
// Initialize display
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.setTextColor(SSD1306_WHITE);
}
void loop()
{
// Read current temperature value
int temp = myTMP36.getTempF();
Serial.print(temp); Serial.println(" degrees F");
// Update teperature array
for (int i=0; i<nPoints-1; i++) {
tempArr[i] = tempArr[i+1];
}
tempArr[nPoints-1] = temp;
// Find min and max temps
int minTemp = 1000;
int maxTemp = -1000;
for (int i=0; i<nPoints; i++) {
maxTemp = max(maxTemp, tempArr[i]);
minTemp = min(minTemp, tempArr[i]);
}
// Reset display
display.clearDisplay();
// Display current temp
int textSize = 2;
int textX = 0;
int textY = SCREEN_HEIGHT / 2 - textSize * CHAR_HEIGHT / 2;
int nChars = String(temp).length();
int circleX = textX + textSize * nChars * CHAR_WIDTH + textSize;
int circleY = textY + textSize;
display.setTextSize(textSize);
display.setCursor(textX, textY);
display.print(temp);
display.drawCircle(circleX, circleY, textSize, WHITE);
// Draw graph labels
int maxChars = String(maxTemp).length();
int minChars = String(minTemp).length();
int labelChars = max(maxChars, minChars);
int labelsX = circleX + textSize * 5;
display.setTextSize(1);
display.setCursor(labelsX + (labelChars - maxChars) * CHAR_WIDTH, 0);
display.print(maxTemp);
display.setCursor(labelsX + (labelChars - minChars) * CHAR_WIDTH, SCREEN_HEIGHT - CHAR_HEIGHT);
display.print(minTemp);
// Define graph bounds
int graphStartX = labelsX + labelChars * CHAR_WIDTH + 2;
int graphEndX = SCREEN_WIDTH;
int graphStartY = 0;
int graphEndY = SCREEN_HEIGHT-1;
// Draw graph axis
display.drawLine(graphStartX, 0, graphStartX, SCREEN_HEIGHT, SSD1306_WHITE);
// Plot graph lines
for (int i=0; i<nPoints-1; i++) {
int x0 = map(i, 0, nPoints-1, graphStartX, graphEndX);
int y0 = map(tempArr[i], minTemp, maxTemp, graphEndY, graphStartY);
int x1 = map(i+1, 0, nPoints-1, graphStartX, graphEndX);
int y1 = map(tempArr[i+1], minTemp, maxTemp, graphEndY, graphStartY);
display.drawLine(x0, y0, x1, y1, SSD1306_WHITE);
}
// Update display
display.display();
// Delay 15 mins
int minutes = 15;
delay(1000L * 60 * minutes);
}