/*
Thermometer Thing
NTC temperature sensor with OLED and RGB LED output
AnonEngineering 9/24/24
*/
#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_1_HW_I2C oled(U8G2_R2, U8X8_PIN_NONE); // rotation, reset
#define oledWidth oled.getDisplayWidth()
const int NUM_RANGES = 8;
const char TEMP_RANGES[NUM_RANGES][16] = {
{"Very cold"}, {"Freezing"}, {"Cold"}, {"Cool"},
{"Nice!"}, {"Warm"}, {"Hot"}, {"Very hot"}
};
const int LED_COLORS[NUM_RANGES][3] = {
{0, 0, 255}, {153, 51, 255}, {255, 0, 255}, {0, 204, 255},
{0, 255, 0}, {255, 255, 0}, {255, 51, 0}, {255, 0, 0}
};
const int INTERVAL = 1000;
const int RED_PIN = 11;
const int GRN_PIN = 10;
const int BLU_PIN = 9;
const int MODE_PIN = 8;
const char HEADER[] = {"Temperature"};
const float BETA = 3950; // thermistor beta coefficient
unsigned long prevTime = 0;
int getComfort(int degree) {
int range = 0;
if (degree < -16) {
range = 0;
} else if (degree >= -16 && degree < 1) {
range = 1;
} else if (degree >= 1 && degree < 10) {
range = 2;
} else if (degree >= 10 && degree < 16) {
range = 3;
} else if (degree >= 16 && degree < 27) {
range = 4;
} else if (degree >= 27 && degree < 35) {
range = 5;
} else if (degree >= 35 && degree < 40) {
range = 6;
} else if (degree >= 40) {
range = 7;
}
return range;
}
void setup() {
Serial.begin(9600);
oled.begin();
pinMode(MODE_PIN, INPUT_PULLUP);
pinMode(RED_PIN, OUTPUT);
pinMode(GRN_PIN, OUTPUT);
pinMode(BLU_PIN, OUTPUT);
Serial.println("Ready");
}
void loop() {
char dispBuff[8];
char tempBuff[8];
char comfort[12];
char scale;
if (millis() - prevTime >= INTERVAL) { // once a second...
prevTime = millis();
// get temp and scale
int mode = digitalRead(MODE_PIN);
int analogValue = analogRead(A0);
float degC = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
float degF = (degC * (9.0 / 5.0)) + 32;
if (mode) {
dtostrf(degC, 1, 1, tempBuff);
scale = 'C';
} else {
dtostrf(degF, 1, 0, tempBuff);
scale = 'F';
}
snprintf(dispBuff, 24, "%s%c%c", tempBuff, char(176), scale);
// get "feels like"
int rangeIdx = getComfort((int)degC);
strncpy (comfort, TEMP_RANGES[rangeIdx], sizeof(comfort) / sizeof(comfort[0]));
analogWrite(RED_PIN, LED_COLORS[rangeIdx][0]);
analogWrite(GRN_PIN, LED_COLORS[rangeIdx][1]);
analogWrite(BLU_PIN, LED_COLORS[rangeIdx][2]);
Serial.print("Temperature: ");
Serial.print(dispBuff);
Serial.print("\tFeels: ");
Serial.println(comfort);
oled.firstPage();
do {
// display header
oled.setFont(u8g2_font_ncenB12_tr);
oled.drawStr((oledWidth - (oled.getUTF8Width(HEADER))) / 2, 15, HEADER);
// display temperature
oled.setFont(u8g2_font_inb16_mf);
oled.drawStr((oledWidth - (oled.getUTF8Width(dispBuff))) / 2, 40, dispBuff);
// display comfort
oled.setFont(u8g2_font_ncenB12_tr);
oled.drawStr((oledWidth - (oled.getUTF8Width(comfort))) / 2, 60, comfort);
} while ( oled.nextPage() );
}
}
C / F