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
|
#include <Wire.h> //library for i2c communication
#include <SPI.h> //library for spi communication
#include "ssd1306.h" //library for controlling ssd1306 oled display
void temperature() //loop for measuring temperature
{
pinMode(thermistorPin, INPUT); //sets thermistorPin as input
thermistorVoltage = analogRead(thermistorPin); //reads analog values from thermistorPin and writes them to thermistorVoltage
R2 = R1 * (1023.0 / (float)thermistorVoltage - 1.0); //equation for calculating temperature
logR2 = log(R2); //logarithmic function
currentTemperature = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2)); //equation for calculating temperature
currentTemperature = currentTemperature - 273.15; //equation for calculating temperature
//currentTemperature = (currentTemperature * 9.0) / 5.0 + 32.0; //for fanrenheit
}
void printFloatNumber() //loop for printing float numbers on oled display
{
char StrT[10];
sprintf(StrT, sizeof(StrT), "%f", currentTemperature);
//sprintf(StrT, sizeof(StrT), "%.2f", currentTemperature);
ssd1306_printFixed(85, 0, StrT, STYLE_NORMAL);
//ssd1306_printFixedN(85, 0, StrT, STYLE_NORMAL, FONT_SIZE_NORMAL);
}
void setup()
{
Serial.begin(115200); //starts serial communication at 115200 baud rate
ssd1306_128x64_i2c_init(); //initializer for display (ssd1306 library)
ssd1306_clearScreen(); //clears the screen
//ssd1306_fillScreen(0x00);
ssd1306_setFixedFont(ssd1306xled_font8x16); //sets font size
ssd1306_printFixed(0, 0, "U=", STYLE_BOLD);
ssd1306_printFixed(0, 16, "I=", STYLE_BOLD);
ssd1306_printFixed(0, 32, "P=", STYLE_BOLD);
ssd1306_printFixed(70, 0, "T=", STYLE_BOLD);
}
void loop()
{
currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
temperature();
printFloatNumber();
}
currentMillis1 = millis();
if (currentMillis1 - previousMillis1 >= interval1)
{
previousMillis1 = currentMillis1;
Serial.println(currentTemperature);
Serial.println(StrT);
}
}
|