-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththermometer.ino
110 lines (88 loc) · 2.36 KB
/
thermometer.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
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
const int tftCs = 7;
const int tftDc = 39;
const int tftRst = 40;
const int tftBacklight = 45;
const int tftI2cPower = 21;
const int sensorPin = 8;
const int ledPin = 13;
const unsigned int temperatureStringSize = 20;
const int samplePeriod = 5000;
const int lowTempLedPeriod = 1000;
const int highTempLedPeriod = 200;
const int loopPeriod = 200;
const int sampleCount = samplePeriod / loopPeriod;
const int lowTempLedCount = lowTempLedPeriod / loopPeriod;
const int highTempLedCount = highTempLedPeriod / loopPeriod;
const float lowTemp = 19.0f;
const float highTemp = 21.0f;
const char degreeSymbol = 248;
int counter = 0;
float temperature = 0.0f;
char temperatureString[temperatureStringSize];
int ledState = LOW;
Adafruit_ST7789 tft = Adafruit_ST7789(tftCs, tftDc, tftRst);
void setup(void) {
// turn on backlite
pinMode(tftBacklight, OUTPUT);
digitalWrite(tftBacklight, HIGH);
// turn on the TFT / I2C power supply
pinMode(tftI2cPower, OUTPUT);
digitalWrite(tftI2cPower, HIGH);
delay(10);
// initialize TFT
tft.init(135, 240); // Init ST7789 240x135
tft.setRotation(3);
// initialize LED
pinMode(ledPin, OUTPUT);
// get first temperature reading
updateTemperature();
}
void loop() {
counter++;
delay(loopPeriod);
if (temperature < lowTemp && counter % lowTempLedCount == 0) {
toggleLed();
}
if (temperature > highTemp && counter % highTempLedCount == 0) {
toggleLed();
}
if (counter == sampleCount) {
updateTemperature();
counter = 0;
}
}
void updateTemperature() {
temperature = getTemperature();
sprintf(temperatureString, "Temperature: %.1f%cC", temperature, degreeSymbol);
drawText(temperatureString, ST77XX_WHITE);
if (temperature >= lowTemp && temperature <= highTemp) {
ledOff();
}
}
float getTemperature() {
int sensorValue = analogRead(sensorPin);
float voltage = sensorValue / 8192.0 * 2.7;
return (voltage - 0.424) / 0.00625;
}
void toggleLed() {
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
void ledOff() {
ledState = LOW;
digitalWrite(ledPin, ledState);
}
void drawText(char *text, uint16_t color) {
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0, 60);
tft.setTextColor(color);
tft.setTextWrap(true);
tft.setTextSize(2);
tft.print(text);
}