#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
const int analogPin = 0;
const int Heatersolidstaterelay = 5;
const int ledheater = 4;
const int indicatorpower = 6;
int analogval = 0;
int vin = 5;
float buff = 0;
float vout = 0;
float R1 = 0;
float R2 = 470;
const int sensorPin = A2; // Analog pin to which the thermistor is connected
const float BETA = 3950; // Beta coefficient of the thermistor
const float SERIES_RESISTOR = 10000; // Resistance of the series resistor (10kΩ)
OneWire bus(2); // SET UP ONE WIRE LIBRARY ON PIN 2
DallasTemperature sensors(&bus); // SETUP DALLAS TEMP TO WORK ON ONEWIRE BUS
U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
#define Thermo_width 41
#define Thermo_height 64
static const unsigned char Thermo_bits[] PROGMEM = {
// Your Thermo_bits data here
};
float temperature; // NOT SURE WHAT THESE ARE FOR YET
char temperatureString[6] = "-"; // NOT SURE WHAT THESE ARE FOR YET
unsigned long previousMillis = 0; // Stores the last time the heater state was changed
unsigned long intervalHigh = 10000; // Interval for HIGH state (10 seconds)
unsigned long intervalLow = 30000; // Interval for LOW state (30 seconds)
bool heaterState = LOW; // Current state of the heater
const char* correctPassword = "1234"; // Change "1234" to your desired password
String enteredPassword;
bool isAuthenticated = false;
void draw(void) {
u8g2.drawXBMP(10, 0, Thermo_width, Thermo_height, Thermo_bits); // These lines call upon the sizing at the top of the bitmap coding.
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(43, 30, "TEMP");
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(43, 55, temperatureString);
}
void setup(void) {
Serial.begin(9600);
pinMode(ledheater, OUTPUT);
pinMode(Heatersolidstaterelay, OUTPUT);
pinMode(indicatorpower, OUTPUT);
sensors.begin();
u8g2.begin();
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(13, 37, "VORTREX");
} while (u8g2.nextPage());
delay(3000);
}
void loop(void) {
digitalWrite(indicatorpower, HIGH);
analogval = analogRead(analogPin);
if (analogval) {
buff = analogval * vin;
vout = (buff) / 1024.0;
if (vout > 0.9) {
buff = (vin / vout) - 1;
R1 = R2 * buff;
u8g2.firstPage();
do {
draw();
} while (u8g2.nextPage());
sensors.requestTemperatures();
temperature = sensors.getTempCByIndex(0);
dtostrf(temperature, 2, 2, temperatureString);
int analogValue = analogRead(sensorPin);
// Convert the analog value to resistance
float resistance = SERIES_RESISTOR / (1023.0 / analogValue - 1);
// Calculate temperature using the Beta parameter equation
float temp_ntc = 1.0 / (log(resistance / 10000.0) / BETA + 1.0 / 298.15) - 273.15;
if(((R1 > 0) && (R1 < 200) && (temperature < 20)) || ((R1 > 200) && (R1 < 250) && (temperature < 30)) ||
((R1 > 250) && (R1 < 280) && (temperature < 35)) || ((R1 > 280) && (R1 < 320) && (temperature < 37)) ||
((R1 > 320) && (R1 < 370) && (temperature < 38)) || ((R1 > 370) && (R1 < 450) && (temperature < 40)) ||
((R1 > 450) && (R1 < 620) && (temperature < 45)) || ((R1 > 620) && (R1 < 800) && (temperature < 50)) ||
((R1 > 800) && (R1 < 993) && (temperature < 55)) || ((R1 > 993) && (R1 < 1290) && (temperature < 60)))
{digitalWrite(ledheater, HIGH);}
else {
digitalWrite(ledheater, LOW);
}
unsigned long currentMillis = millis();
if((((R1 > 0) && (R1 < 200) && (temperature < 20)) || ((R1 > 200) && (R1 < 250) && (temperature < 30)) ||
((R1 > 250) && (R1 < 280) && (temperature < 35)) || ((R1 > 280) && (R1 < 320) && (temperature < 37)) ||
((R1 > 320) && (R1 < 370) && (temperature < 38)) || ((R1 > 370) && (R1 < 450) && (temperature < 40)) ||
((R1 > 450) && (R1 < 620) && (temperature < 45)) || ((R1 > 620) && (R1 < 800) && (temperature < 50)) ||
((R1 > 800) && (R1 < 993) && (temperature < 55)) || ((R1 > 993) && (R1 < 1290) && (temperature < 60))) && (temp_ntc < 45))
{
if (heaterState == HIGH && (currentMillis - previousMillis >= intervalLow)) {
heaterState = LOW;
previousMillis = currentMillis;
digitalWrite(Heatersolidstaterelay, LOW);
}
if (heaterState == LOW && (currentMillis - previousMillis >= intervalHigh)) {
heaterState = HIGH;
previousMillis = currentMillis;
digitalWrite(Heatersolidstaterelay, HIGH);
}
} else {
digitalWrite(Heatersolidstaterelay, HIGH);
}
}
}
// Password handling for serial monitor
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
command.trim(); // Remove any leading/trailing whitespace
if (isAuthenticated) {
// Process command or show data
if (command == "show") {
Serial.print("Resistance: ");
Serial.println(R1);
Serial.print("Temperature: ");
Serial.println(temperature);
}
} else {
// Check for password
if (command == correctPassword) {
isAuthenticated = true;
Serial.println("Access granted.");
} else {
Serial.println("Access denied. Incorrect password.");
}
}
}
}