2024-09-01 11:18:03 +07:00
|
|
|
#define useEEPROM 1
|
2024-09-06 02:15:52 +07:00
|
|
|
// #define TempSensorMax
|
|
|
|
|
#define TempSensorDallas
|
2024-09-07 23:11:57 +07:00
|
|
|
// #define PlotValues
|
2024-09-01 11:18:03 +07:00
|
|
|
|
2024-08-29 12:37:31 +07:00
|
|
|
#include <Wire.h>
|
2024-08-31 01:38:49 +07:00
|
|
|
#include <GyverOLED.h>
|
2024-09-01 11:18:03 +07:00
|
|
|
#include "GyverEncoder.h"
|
|
|
|
|
|
|
|
|
|
#if useEEPROM
|
2024-08-31 13:28:50 +07:00
|
|
|
#include <EEPROM.h>
|
2024-09-01 11:18:03 +07:00
|
|
|
#endif
|
|
|
|
|
|
2024-09-06 02:15:52 +07:00
|
|
|
#ifdef TempSensorMax
|
|
|
|
|
#include <max6675.h>
|
|
|
|
|
// MAX6675 configuration
|
|
|
|
|
int max_SO = 12;
|
|
|
|
|
int max_CS = 10;
|
|
|
|
|
int max_SCK = 13;
|
|
|
|
|
MAX6675 thermocouple(max_SCK, max_CS, max_SO);
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef TempSensorDallas
|
|
|
|
|
#include <OneWire.h>
|
|
|
|
|
#include <DallasTemperature.h>
|
|
|
|
|
// Data wire is plugged into port 2 on the Arduino
|
|
|
|
|
#define ONE_WIRE_BUS 9
|
|
|
|
|
|
|
|
|
|
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
|
|
|
|
|
OneWire oneWire(ONE_WIRE_BUS);
|
|
|
|
|
|
|
|
|
|
// Pass our oneWire reference to Dallas Temperature.
|
|
|
|
|
DallasTemperature sensors(&oneWire);
|
|
|
|
|
#endif
|
2024-08-29 12:37:31 +07:00
|
|
|
|
2024-08-29 23:01:32 +07:00
|
|
|
// OLED configuration
|
2024-08-31 13:28:50 +07:00
|
|
|
// GyverOLED<SSD1306_128x64> oled;
|
|
|
|
|
GyverOLED<SSD1306_128x64, OLED_NO_BUFFER> oled;
|
2024-08-29 12:37:31 +07:00
|
|
|
|
2024-08-31 01:38:49 +07:00
|
|
|
// Encoder configuration
|
|
|
|
|
#define CLK 5
|
|
|
|
|
#define DT 6
|
|
|
|
|
#define SW 7
|
|
|
|
|
Encoder enc1(CLK, DT, SW, TYPE2);
|
|
|
|
|
|
2024-08-29 12:37:31 +07:00
|
|
|
// SSR pin configuration
|
2024-09-01 11:18:03 +07:00
|
|
|
const int ssrPin = 2;
|
2024-09-07 23:11:57 +07:00
|
|
|
const int activeBuzzerPin = 8;
|
2024-08-29 12:37:31 +07:00
|
|
|
|
2024-08-29 23:58:25 +07:00
|
|
|
// Profile structure definition
|
|
|
|
|
struct Phase {
|
|
|
|
|
int temperature;
|
|
|
|
|
int duration; // in minutes
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Profile {
|
|
|
|
|
const char* name;
|
2024-08-30 02:45:48 +07:00
|
|
|
int transitionMinutesPerDegree;
|
2024-08-30 00:07:12 +07:00
|
|
|
Phase phases[6]; // Maximum of 6 phases per profile
|
2024-08-29 23:58:25 +07:00
|
|
|
int numPhases;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Profiles definition
|
|
|
|
|
Profile profiles[] = {
|
2024-09-23 18:29:42 +07:00
|
|
|
{"Chickpeas", 1, {{46, 120}, {55, 60}, {65, 150}, {70, 60}, {90, 105}}, 5},
|
2024-10-01 23:02:50 +07:00
|
|
|
{"Chickpeas v2", 1, {{46, 120}, {55, 120}, {65, 120}, {72, 240}, {85, 300}}, 5},
|
2024-09-23 18:29:42 +07:00
|
|
|
{"Lentils", 3, {{46, 120}, {53, 120}, {65, 180}, {72, 90}, {90, 15}}, 5},
|
|
|
|
|
{"Gradual", 10, {{46, 120}, {80, 60}}, 2},
|
|
|
|
|
{"Long gradual", 10, {{46, 120}, {80, 60}, {90, 15}}, 3},
|
2024-10-06 21:36:43 +07:00
|
|
|
{"Wheat", 1, {{46, 120}, {53, 40}, {65, 40}, {72, 40}, {85, 40}}, 5},
|
2024-09-23 18:29:42 +07:00
|
|
|
{"SV1", 1, {{55, 30}, {85, 120}}, 2},
|
|
|
|
|
{"SV2", 0, {{46, 45}}, 1},
|
2024-10-01 23:02:50 +07:00
|
|
|
// {"Yoghurt maker", 0, {{40, 300}, {40, 300}, {30, 300}}, 3},
|
2024-08-29 23:58:25 +07:00
|
|
|
};
|
|
|
|
|
|
2024-08-31 01:38:49 +07:00
|
|
|
// Global variables for profile selection and execution
|
|
|
|
|
int activeProfileIndex = 100; // 100 indicates no profile selected yet
|
|
|
|
|
int selectedProfileIndex = 0; // Index of the currently selected profile in the selection mode
|
|
|
|
|
Profile activeProfile; // The active profile once selected
|
2024-08-29 12:37:31 +07:00
|
|
|
|
2024-08-30 02:45:48 +07:00
|
|
|
// Global variables for current phase, setpoint, and transition state
|
|
|
|
|
int currentPhase;
|
|
|
|
|
bool isInTransition = false;
|
2024-08-31 01:38:49 +07:00
|
|
|
bool inSelectionMode = true;
|
2024-08-30 02:45:48 +07:00
|
|
|
|
2024-08-29 12:37:31 +07:00
|
|
|
// PID Control variables
|
2024-09-06 11:27:26 +07:00
|
|
|
// double Kp = 0.5, Ki = 1, Kd = 0.05;
|
|
|
|
|
// PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
|
|
|
|
|
|
|
|
|
|
#include "GyverPID.h"
|
2024-08-29 12:37:31 +07:00
|
|
|
double Setpoint, Input, Output;
|
2024-09-23 18:29:42 +07:00
|
|
|
GyverPID regulator(25, 0, 0, 1000);
|
|
|
|
|
|
|
|
|
|
bool temperatureSensorError = false;
|
2024-08-29 12:37:31 +07:00
|
|
|
|
|
|
|
|
// Timing variables
|
2024-08-31 12:47:54 +07:00
|
|
|
long phaseStartTime;
|
|
|
|
|
long totalStartTime;
|
|
|
|
|
long ssrLastSwitchTime;
|
|
|
|
|
long totalElapsedTime;
|
|
|
|
|
long totalProcessTime;
|
|
|
|
|
long finishTime = 0;
|
|
|
|
|
long currentTime = 0;
|
2024-08-31 13:28:50 +07:00
|
|
|
long lastEEPROMWriteTime = 0;
|
2024-09-03 17:20:16 +07:00
|
|
|
bool isClick = false;
|
|
|
|
|
bool isLeft = false;
|
|
|
|
|
bool isRight = false;
|
2024-08-30 00:23:18 +07:00
|
|
|
bool isComplete = false;
|
2024-08-29 12:37:31 +07:00
|
|
|
const int ssrSwitchInterval = 1000; // SSR switching interval in milliseconds
|
|
|
|
|
|
2024-08-29 23:58:25 +07:00
|
|
|
// Buffer for formatted time strings
|
|
|
|
|
char timeBuffer[10];
|
|
|
|
|
|
2024-09-07 23:11:57 +07:00
|
|
|
uint32_t timerExecution = 0;
|
|
|
|
|
#define T_PERIOD_EXEC 1000 // period of Execution processing
|
2024-09-03 17:20:16 +07:00
|
|
|
uint32_t timerSSR = 0;
|
|
|
|
|
#define T_PERIOD_SSR 500 // period of heater handling
|
2024-09-07 23:11:57 +07:00
|
|
|
uint32_t timerTemp = 0;
|
|
|
|
|
#define T_PERIOD_TEMP 1000 // period of dallas sensor requesting
|
|
|
|
|
uint32_t timerChecks= 0;
|
|
|
|
|
#define T_PERIOD_Checks 1000 // period of additional checks
|
|
|
|
|
|
|
|
|
|
bool boolLastCompletedState = false;
|
2024-09-03 17:20:16 +07:00
|
|
|
|
2024-09-23 18:29:42 +07:00
|
|
|
float failedReadingLastValue = 0;
|
|
|
|
|
int failedReadingCount = 0;
|
|
|
|
|
|
|
|
|
|
#define PARTS 8
|
2024-09-03 17:20:16 +07:00
|
|
|
|
2024-08-29 12:37:31 +07:00
|
|
|
void setup() {
|
|
|
|
|
pinMode(ssrPin, OUTPUT);
|
2024-09-07 23:11:57 +07:00
|
|
|
pinMode(activeBuzzerPin, OUTPUT);
|
2024-08-29 12:37:31 +07:00
|
|
|
Serial.begin(9600);
|
2024-09-02 00:39:39 +07:00
|
|
|
|
2024-08-29 23:01:32 +07:00
|
|
|
oled.init(); // Initialize the OLED
|
|
|
|
|
oled.clear(); // Clear the display
|
2024-09-01 11:18:03 +07:00
|
|
|
readEEPROM();
|
2024-09-06 11:27:26 +07:00
|
|
|
regulator.setDirection(NORMAL); // направление регулирования (NORMAL/REVERSE). ПО УМОЛЧАНИЮ СТОИТ NORMAL
|
|
|
|
|
regulator.setLimits(0, 100); // пределы (ставим для 8 битного ШИМ). ПО УМОЛЧАНИЮ СТОЯТ 0 И 255
|
|
|
|
|
regulator.setpoint = 0;
|
2024-09-06 02:15:52 +07:00
|
|
|
|
|
|
|
|
#ifdef TempSensorDallas
|
|
|
|
|
sensors.begin();
|
|
|
|
|
DeviceAddress tempDeviceAddress;
|
|
|
|
|
sensors.getAddress(tempDeviceAddress, 0);
|
|
|
|
|
sensors.setResolution(tempDeviceAddress, 12);
|
|
|
|
|
sensors.setWaitForConversion(false);
|
|
|
|
|
#endif
|
2024-08-29 12:37:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loop() {
|
2024-08-31 12:47:54 +07:00
|
|
|
enc1.tick();
|
2024-09-03 17:20:16 +07:00
|
|
|
isLeft = enc1.isLeft();
|
|
|
|
|
isRight = enc1.isRight();
|
|
|
|
|
isClick = enc1.isClick();
|
|
|
|
|
if (isLeft || isRight || isClick) {
|
|
|
|
|
handleEncoder();
|
|
|
|
|
}
|
2024-09-07 23:11:57 +07:00
|
|
|
handleTemperatureSensor();
|
2024-09-03 17:20:16 +07:00
|
|
|
handleExecution();
|
2024-09-03 19:44:45 +07:00
|
|
|
handleHeaterAdv();
|
2024-09-07 23:11:57 +07:00
|
|
|
handleAdditionalChecks();
|
2024-09-06 02:15:52 +07:00
|
|
|
}
|
|
|
|
|
|
2024-09-07 23:11:57 +07:00
|
|
|
void handleAdditionalChecks() {
|
|
|
|
|
long time = millis();
|
|
|
|
|
|
|
|
|
|
if (time - timerChecks < T_PERIOD_Checks) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
timerChecks = time;
|
|
|
|
|
|
|
|
|
|
if (isComplete && !boolLastCompletedState) {
|
|
|
|
|
boolLastCompletedState = true;
|
|
|
|
|
for(int i=0; i<10; i++) {
|
|
|
|
|
digitalWrite(activeBuzzerPin, HIGH);
|
|
|
|
|
delay(100);
|
|
|
|
|
digitalWrite(activeBuzzerPin, LOW);
|
|
|
|
|
delay(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void handleTemperatureSensor() {
|
2024-09-06 02:15:52 +07:00
|
|
|
long time = millis();
|
|
|
|
|
static int currentPart = 0;
|
|
|
|
|
|
2024-09-07 23:11:57 +07:00
|
|
|
if (time - timerTemp < T_PERIOD_TEMP) {
|
2024-09-06 02:15:52 +07:00
|
|
|
return;
|
|
|
|
|
}
|
2024-09-07 23:11:57 +07:00
|
|
|
timerTemp = time;
|
2024-09-06 02:15:52 +07:00
|
|
|
|
2024-09-07 23:11:57 +07:00
|
|
|
#ifdef TempSensorDallas
|
2024-09-06 02:15:52 +07:00
|
|
|
Input = (double) sensors.getTempCByIndex(0);
|
|
|
|
|
|
|
|
|
|
sensors.requestTemperatures(); // Send the command to get temperatures
|
|
|
|
|
#endif
|
2024-09-07 23:11:57 +07:00
|
|
|
#ifdef TempSensorMax // to go into the read temp function, rename from dallas temp
|
|
|
|
|
Input = thermocouple.readCelsius();
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-09-23 18:29:42 +07:00
|
|
|
if (isnan(Input) || Input < 20 || Input > 95) {
|
|
|
|
|
failedReadingLastValue = (float) Input;
|
|
|
|
|
failedReadingCount++;
|
|
|
|
|
temperatureSensorError = true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
temperatureSensorError = false;
|
2024-09-07 23:11:57 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-09-03 17:20:16 +07:00
|
|
|
|
2024-09-03 19:44:45 +07:00
|
|
|
void handleHeaterSimple() {
|
2024-09-03 17:20:16 +07:00
|
|
|
long time = millis();
|
|
|
|
|
static int currentPart = 0;
|
|
|
|
|
|
|
|
|
|
if (time - timerSSR < T_PERIOD_SSR) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (inSelectionMode) {
|
|
|
|
|
digitalWrite(ssrPin, LOW);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
timerSSR = time;
|
|
|
|
|
|
|
|
|
|
int current = Output * PARTS;
|
2024-08-31 01:38:49 +07:00
|
|
|
|
2024-09-03 17:20:16 +07:00
|
|
|
oled.setCursor(104, 7);
|
|
|
|
|
char symbol;
|
|
|
|
|
for (int i = 0; i < PARTS; i++) {
|
|
|
|
|
symbol = current > i ? '=' : '-';
|
|
|
|
|
if (currentPart == i) {
|
|
|
|
|
oled.invertText(true);
|
|
|
|
|
}
|
|
|
|
|
oled.print(symbol);
|
|
|
|
|
oled.invertText(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
digitalWrite(ssrPin, currentPart < current ? HIGH : LOW);
|
|
|
|
|
currentPart++;
|
|
|
|
|
if (currentPart >= PARTS) {
|
|
|
|
|
currentPart = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-03 19:44:45 +07:00
|
|
|
void handleHeaterAdv() {
|
|
|
|
|
long time = millis();
|
|
|
|
|
static int currentPart = 0;
|
|
|
|
|
static bool states[PARTS];
|
|
|
|
|
|
|
|
|
|
if (time - timerSSR < T_PERIOD_SSR) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (inSelectionMode) {
|
|
|
|
|
digitalWrite(ssrPin, LOW);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
timerSSR = time;
|
|
|
|
|
|
2024-09-06 02:15:52 +07:00
|
|
|
int current = Output/100 * PARTS;
|
2024-09-03 19:44:45 +07:00
|
|
|
|
|
|
|
|
int last = 0;
|
|
|
|
|
for (int i = 1; i < PARTS; i++) {
|
|
|
|
|
last += states[i] ? 1 : 0;
|
|
|
|
|
if (i) {
|
|
|
|
|
states[i-1] = states[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-06 02:15:52 +07:00
|
|
|
bool next = current >= last ? true : false;
|
2024-09-07 23:11:57 +07:00
|
|
|
if (Output < 1) next = false;
|
2024-09-03 19:44:45 +07:00
|
|
|
states[PARTS-1] = next;
|
|
|
|
|
|
|
|
|
|
oled.setCursor(128-6*PARTS, 7);
|
2024-09-06 02:15:52 +07:00
|
|
|
sprintf(timeBuffer, "%3d", (int)(Output));
|
2024-09-03 19:44:45 +07:00
|
|
|
char symbol;
|
|
|
|
|
for (int i = 0; i < PARTS; i++) {
|
|
|
|
|
int index = i - (PARTS - 3);
|
|
|
|
|
symbol = index < 0 ? ' ' : timeBuffer[index];
|
|
|
|
|
oled.invertText(states[i]);
|
|
|
|
|
oled.print(symbol);
|
|
|
|
|
oled.invertText(false);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-23 18:29:42 +07:00
|
|
|
if (temperatureSensorError) {
|
|
|
|
|
next = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-03 19:44:45 +07:00
|
|
|
digitalWrite(ssrPin, next);
|
|
|
|
|
currentPart++;
|
|
|
|
|
if (currentPart >= PARTS) {
|
|
|
|
|
currentPart = 0;
|
|
|
|
|
}
|
2024-09-06 02:15:52 +07:00
|
|
|
|
2024-09-07 23:11:57 +07:00
|
|
|
#ifdef PlotValues
|
2024-09-06 02:15:52 +07:00
|
|
|
Serial.print(Setpoint);
|
|
|
|
|
Serial.print(",");
|
|
|
|
|
Serial.print(Input);
|
|
|
|
|
Serial.print(",");
|
|
|
|
|
Serial.print(Output);
|
|
|
|
|
Serial.println(",0,100");
|
2024-09-07 23:11:57 +07:00
|
|
|
#endif
|
2024-09-06 02:15:52 +07:00
|
|
|
|
2024-09-03 19:44:45 +07:00
|
|
|
}
|
|
|
|
|
|
2024-09-03 17:20:16 +07:00
|
|
|
void handleEncoder() {
|
2024-08-31 01:38:49 +07:00
|
|
|
if (inSelectionMode) {
|
2024-08-31 12:47:54 +07:00
|
|
|
handleProfileSelection();
|
2024-09-01 11:18:03 +07:00
|
|
|
}
|
|
|
|
|
else {
|
2024-09-03 17:20:16 +07:00
|
|
|
handleExecutionSelection();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void handleExecutionSelection() {
|
|
|
|
|
if (isClick) {
|
|
|
|
|
activeProfileIndex = 100;
|
|
|
|
|
activeProfile = profiles[activeProfileIndex];
|
|
|
|
|
inSelectionMode = true;
|
|
|
|
|
writeEEPROM();
|
|
|
|
|
oled.clear();
|
|
|
|
|
displaySelection();
|
|
|
|
|
return;
|
2024-08-31 12:47:54 +07:00
|
|
|
}
|
2024-09-03 17:20:16 +07:00
|
|
|
|
2024-08-31 12:47:54 +07:00
|
|
|
}
|
2024-08-31 01:38:49 +07:00
|
|
|
|
2024-08-31 12:47:54 +07:00
|
|
|
void handleExecution() {
|
|
|
|
|
currentTime = millis();
|
2024-09-03 17:20:16 +07:00
|
|
|
|
2024-09-07 23:11:57 +07:00
|
|
|
if (inSelectionMode || (currentTime - timerExecution < T_PERIOD_EXEC)) { // таймер на millis()
|
2024-09-03 17:20:16 +07:00
|
|
|
return;
|
|
|
|
|
}
|
2024-09-07 23:11:57 +07:00
|
|
|
timerExecution = currentTime;
|
2024-09-03 17:20:16 +07:00
|
|
|
|
2024-08-31 12:47:54 +07:00
|
|
|
totalElapsedTime = (currentTime - totalStartTime) / 1000; // Total elapsed time in seconds
|
2024-08-31 01:38:49 +07:00
|
|
|
|
2024-09-01 11:18:03 +07:00
|
|
|
if ((currentTime - lastEEPROMWriteTime) >= (long)10*60*1000) {
|
2024-08-31 13:28:50 +07:00
|
|
|
writeEEPROM();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-31 12:47:54 +07:00
|
|
|
getPhaseAndTemperature();
|
2024-08-31 01:38:49 +07:00
|
|
|
|
2024-09-06 11:27:26 +07:00
|
|
|
regulator.input = (float)Input;
|
|
|
|
|
regulator.setpoint = Setpoint;
|
2024-09-07 23:11:57 +07:00
|
|
|
regulator.getResultNow();
|
2024-09-06 11:27:26 +07:00
|
|
|
Output = regulator.output;
|
2024-08-31 01:38:49 +07:00
|
|
|
|
2024-08-31 12:47:54 +07:00
|
|
|
if (isComplete && currentPhase >= activeProfile.numPhases && !finishTime) {
|
|
|
|
|
finishTime = currentTime;
|
|
|
|
|
}
|
2024-08-29 12:37:31 +07:00
|
|
|
|
2024-08-31 12:47:54 +07:00
|
|
|
// Display all phases and highlight the current one
|
|
|
|
|
printPhases();
|
2024-08-31 01:38:49 +07:00
|
|
|
}
|
|
|
|
|
|
2024-09-03 19:44:45 +07:00
|
|
|
void startExecution() {
|
|
|
|
|
activeProfile = profiles[activeProfileIndex];
|
|
|
|
|
inSelectionMode = false; // Switch to execution mode
|
|
|
|
|
phaseStartTime = totalStartTime = millis(); // Start the timer
|
|
|
|
|
totalElapsedTime = 0;
|
2024-09-06 11:27:26 +07:00
|
|
|
regulator.setpoint = 0;
|
2024-09-03 19:44:45 +07:00
|
|
|
// digitalWrite(ssrPin, HIGH); // Start with heater on
|
|
|
|
|
ssrLastSwitchTime = millis();
|
|
|
|
|
calculateTotalTime();
|
|
|
|
|
getPhaseAndTemperature();
|
|
|
|
|
writeEEPROM();
|
|
|
|
|
oled.clear();
|
2024-09-07 23:11:57 +07:00
|
|
|
boolLastCompletedState = isComplete = false;
|
2024-09-03 19:44:45 +07:00
|
|
|
}
|
|
|
|
|
|
2024-08-31 01:38:49 +07:00
|
|
|
void handleProfileSelection() {
|
2024-09-03 17:20:16 +07:00
|
|
|
if (!isClick && !isLeft && !isRight) {
|
2024-08-31 01:38:49 +07:00
|
|
|
return;
|
2024-08-30 02:45:48 +07:00
|
|
|
}
|
|
|
|
|
|
2024-08-31 01:38:49 +07:00
|
|
|
// Handle encoder input for selecting the profile
|
2024-09-03 17:20:16 +07:00
|
|
|
if (isRight) {
|
2024-08-31 01:38:49 +07:00
|
|
|
selectedProfileIndex = (selectedProfileIndex + 1) % (sizeof(profiles) / sizeof(profiles[0]));
|
2024-09-03 17:20:16 +07:00
|
|
|
} else if (isLeft) {
|
2024-08-31 01:38:49 +07:00
|
|
|
selectedProfileIndex = (selectedProfileIndex - 1 + (sizeof(profiles) / sizeof(profiles[0]))) % (sizeof(profiles) / sizeof(profiles[0]));
|
|
|
|
|
}
|
2024-08-30 02:45:48 +07:00
|
|
|
|
2024-08-31 01:38:49 +07:00
|
|
|
displaySelection();
|
|
|
|
|
|
|
|
|
|
// Start the selected profile on button press
|
2024-09-03 17:20:16 +07:00
|
|
|
if (isClick) {
|
2024-08-31 01:38:49 +07:00
|
|
|
activeProfileIndex = selectedProfileIndex;
|
2024-09-03 19:44:45 +07:00
|
|
|
startExecution();
|
2024-08-31 01:38:49 +07:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-30 02:45:48 +07:00
|
|
|
|
2024-08-31 01:38:49 +07:00
|
|
|
void displaySelection() {
|
2024-08-31 13:36:34 +07:00
|
|
|
// oled.clear();
|
2024-08-31 01:38:49 +07:00
|
|
|
// Display all profiles with the selected one highlighted
|
|
|
|
|
for (int i = 0; i < (int) (sizeof(profiles) / sizeof(profiles[0])); i++) {
|
|
|
|
|
if (i == selectedProfileIndex) {
|
|
|
|
|
oled.invertText(true); // Highlight the selected profile
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
oled.setCursor(0, i); // Set cursor to the correct row
|
|
|
|
|
oled.print(profiles[i].name);
|
|
|
|
|
|
|
|
|
|
oled.invertText(false); // Reset text inversion for other profiles
|
|
|
|
|
}
|
|
|
|
|
oled.update();
|
2024-08-30 02:45:48 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void calculateTotalTime() {
|
|
|
|
|
// Calculate total process time, including transitions
|
2024-09-01 11:18:03 +07:00
|
|
|
activeProfile = profiles[activeProfileIndex];
|
2024-08-29 23:58:25 +07:00
|
|
|
totalProcessTime = 0;
|
|
|
|
|
for (int i = 0; i < activeProfile.numPhases; i++) {
|
|
|
|
|
totalProcessTime += activeProfile.phases[i].duration * 60;
|
2024-08-30 02:45:48 +07:00
|
|
|
if (i < activeProfile.numPhases - 1) {
|
|
|
|
|
int tempDiff = abs(activeProfile.phases[i + 1].temperature - activeProfile.phases[i].temperature);
|
|
|
|
|
totalProcessTime += tempDiff * 60 * activeProfile.transitionMinutesPerDegree;
|
|
|
|
|
}
|
2024-08-29 12:37:31 +07:00
|
|
|
}
|
2024-08-30 02:45:48 +07:00
|
|
|
}
|
2024-08-29 12:37:31 +07:00
|
|
|
|
2024-08-31 12:47:54 +07:00
|
|
|
void getPhaseAndTemperature() {
|
|
|
|
|
long accumulatedTime = 0;
|
2024-08-30 02:45:48 +07:00
|
|
|
|
|
|
|
|
for (int i = 0; i < activeProfile.numPhases; i++) {
|
|
|
|
|
int phaseDuration = activeProfile.phases[i].duration * 60;
|
|
|
|
|
|
|
|
|
|
// Check for transition
|
|
|
|
|
if (i > 0) {
|
|
|
|
|
int previousTemp = activeProfile.phases[i - 1].temperature;
|
|
|
|
|
int targetTemp = activeProfile.phases[i].temperature;
|
|
|
|
|
int tempDiff = abs(targetTemp - previousTemp);
|
|
|
|
|
int transitionDuration = tempDiff * 60 * activeProfile.transitionMinutesPerDegree;
|
|
|
|
|
|
2024-08-31 12:47:54 +07:00
|
|
|
if (totalElapsedTime < accumulatedTime + transitionDuration) {
|
2024-08-30 02:45:48 +07:00
|
|
|
isInTransition = true;
|
2024-08-31 01:38:49 +07:00
|
|
|
currentPhase = i - 1; // Keep currentPhase as the previous phase
|
2024-08-31 12:47:54 +07:00
|
|
|
int timeInTransition = totalElapsedTime - accumulatedTime;
|
2024-08-31 01:38:49 +07:00
|
|
|
Setpoint = previousTemp + (double)timeInTransition / (60 * activeProfile.transitionMinutesPerDegree) * (targetTemp > previousTemp ? 1 : -1);
|
2024-08-31 12:47:54 +07:00
|
|
|
phaseStartTime = totalStartTime + accumulatedTime * 1000; // Set phase start time
|
2024-08-30 02:45:48 +07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
accumulatedTime += transitionDuration;
|
2024-08-29 12:37:31 +07:00
|
|
|
}
|
|
|
|
|
|
2024-08-30 02:45:48 +07:00
|
|
|
// Check if we're within the current phase
|
2024-08-31 12:47:54 +07:00
|
|
|
if (totalElapsedTime < accumulatedTime + phaseDuration) {
|
2024-08-30 02:45:48 +07:00
|
|
|
isInTransition = false;
|
|
|
|
|
currentPhase = i;
|
|
|
|
|
Setpoint = activeProfile.phases[i].temperature;
|
2024-08-31 12:47:54 +07:00
|
|
|
phaseStartTime = totalStartTime + accumulatedTime * 1000; // Set phase start time
|
2024-08-30 02:45:48 +07:00
|
|
|
return;
|
|
|
|
|
}
|
2024-08-29 23:58:25 +07:00
|
|
|
|
2024-08-30 02:45:48 +07:00
|
|
|
accumulatedTime += phaseDuration;
|
|
|
|
|
}
|
2024-08-29 12:37:31 +07:00
|
|
|
|
2024-08-30 02:45:48 +07:00
|
|
|
// If the elapsed time exceeds the total duration, indicate completion
|
|
|
|
|
currentPhase = activeProfile.numPhases; // Indicate completion
|
|
|
|
|
Setpoint = 45; // Default to 45°C after completion
|
|
|
|
|
isInTransition = false;
|
|
|
|
|
isComplete = true; // Mark the process as complete
|
2024-08-31 12:47:54 +07:00
|
|
|
phaseStartTime = totalStartTime + accumulatedTime * 1000; // Set phase start time to the end
|
2024-08-29 12:37:31 +07:00
|
|
|
}
|
|
|
|
|
|
2024-08-31 12:47:54 +07:00
|
|
|
|
2024-08-31 01:46:02 +07:00
|
|
|
void printPhases() {
|
2024-08-31 13:36:34 +07:00
|
|
|
// oled.clear();
|
2024-08-29 23:58:25 +07:00
|
|
|
|
2024-08-30 00:23:18 +07:00
|
|
|
if (isComplete) {
|
|
|
|
|
// Show completion time and current temperature instead of the title
|
|
|
|
|
oled.setCursor(0, 0);
|
|
|
|
|
oled.invertText(true);
|
2024-08-30 02:45:48 +07:00
|
|
|
oled.print("Done!");
|
|
|
|
|
oled.invertText(false);
|
|
|
|
|
oled.print(" ");
|
2024-08-30 00:23:18 +07:00
|
|
|
formatTime((currentTime - finishTime) / 1000, timeBuffer); // Time since completion
|
|
|
|
|
oled.print(timeBuffer);
|
|
|
|
|
oled.print(" ");
|
2024-09-02 00:39:39 +07:00
|
|
|
oled.print(Input,0);
|
2024-08-31 13:28:50 +07:00
|
|
|
oled.print("c");
|
|
|
|
|
oled.print("->");
|
|
|
|
|
oled.print((int)Setpoint);
|
2024-08-31 13:36:34 +07:00
|
|
|
oled.print("c ");
|
2024-08-30 00:23:18 +07:00
|
|
|
} else {
|
|
|
|
|
oled.setCursor(0, 0);
|
|
|
|
|
oled.print(activeProfile.name);
|
2024-08-31 13:36:34 +07:00
|
|
|
oled.print(" ");
|
2024-08-30 00:23:18 +07:00
|
|
|
}
|
|
|
|
|
|
2024-08-29 23:58:25 +07:00
|
|
|
// Display the totals and current state on the OLED
|
2024-08-31 13:36:34 +07:00
|
|
|
oled.setCursor(0, 1);
|
|
|
|
|
oled.print(" ");
|
2024-08-29 23:58:25 +07:00
|
|
|
oled.setCursor(18, 1);
|
|
|
|
|
formatTime(totalElapsedTime, timeBuffer);
|
|
|
|
|
oled.print(timeBuffer);
|
|
|
|
|
oled.print(" / ");
|
|
|
|
|
formatTime(totalProcessTime, timeBuffer);
|
|
|
|
|
oled.print(timeBuffer);
|
2024-08-31 13:36:34 +07:00
|
|
|
oled.print(" ");
|
2024-08-31 01:46:02 +07:00
|
|
|
|
2024-08-29 23:58:25 +07:00
|
|
|
for (int i = 0; i < activeProfile.numPhases; i++) {
|
2024-09-01 11:18:03 +07:00
|
|
|
if (i == currentPhase) {
|
2024-08-29 23:58:25 +07:00
|
|
|
oled.invertText(true); // Invert text for the current phase
|
|
|
|
|
|
|
|
|
|
oled.setCursor(0, i + 2); // Set cursor to the row corresponding to the phase
|
2024-08-31 12:47:54 +07:00
|
|
|
long timeRemaining = (activeProfile.phases[i].duration * 60) - ((currentTime - phaseStartTime) / 1000);
|
2024-08-29 23:58:25 +07:00
|
|
|
formatTime(timeRemaining, timeBuffer);
|
|
|
|
|
oled.print(i + 1);
|
|
|
|
|
oled.print(". ");
|
2024-08-31 01:46:02 +07:00
|
|
|
oled.print(Input, 1);
|
2024-08-30 00:07:12 +07:00
|
|
|
oled.print("c ");
|
2024-08-30 02:45:48 +07:00
|
|
|
if (fabs(Setpoint - round(Setpoint)) < 0.05) {
|
2024-09-01 11:18:03 +07:00
|
|
|
oled.print(Setpoint, 0); // Print without decimals
|
2024-08-30 02:45:48 +07:00
|
|
|
} else {
|
2024-09-01 11:18:03 +07:00
|
|
|
oled.print(Setpoint, 1); // Print with 1 decimal place
|
2024-08-30 02:45:48 +07:00
|
|
|
}
|
2024-08-30 00:07:12 +07:00
|
|
|
oled.print("c ");
|
2024-09-01 11:18:03 +07:00
|
|
|
if (!isInTransition) {
|
|
|
|
|
oled.print(timeBuffer);
|
|
|
|
|
}
|
2024-08-31 13:36:34 +07:00
|
|
|
oled.print(" ");
|
2024-08-29 23:58:25 +07:00
|
|
|
} else {
|
|
|
|
|
oled.invertText(false); // Normal text for other phases
|
|
|
|
|
|
|
|
|
|
oled.setCursor(0, i + 2); // Set cursor to the row corresponding to the phase
|
|
|
|
|
formatTime(activeProfile.phases[i].duration * 60, timeBuffer);
|
|
|
|
|
oled.print(i + 1);
|
|
|
|
|
oled.print(". ");
|
|
|
|
|
oled.print(activeProfile.phases[i].temperature);
|
2024-08-30 00:07:12 +07:00
|
|
|
oled.print("c ");
|
2024-08-29 23:58:25 +07:00
|
|
|
oled.print(timeBuffer);
|
2024-08-31 13:36:34 +07:00
|
|
|
oled.print(" ");
|
2024-08-29 23:58:25 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-23 18:29:42 +07:00
|
|
|
if (temperatureSensorError) {
|
|
|
|
|
oled.invertText(true);
|
|
|
|
|
oled.setCursor(0, 0);
|
|
|
|
|
oled.setScale(2);
|
|
|
|
|
oled.print("T = ");
|
|
|
|
|
oled.print(Input);
|
|
|
|
|
oled.print(" ");
|
|
|
|
|
oled.setScale(1);
|
|
|
|
|
digitalWrite(activeBuzzerPin, HIGH);
|
|
|
|
|
delay(5);
|
|
|
|
|
digitalWrite(activeBuzzerPin, LOW);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (failedReadingCount) {
|
|
|
|
|
oled.setCursor(0, 7);
|
|
|
|
|
oled.print(failedReadingCount);
|
|
|
|
|
oled.print(" (");
|
|
|
|
|
oled.print(failedReadingLastValue);
|
|
|
|
|
oled.print(") ");
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-29 23:58:25 +07:00
|
|
|
oled.invertText(false); // Ensure text inversion is off after the loop
|
2024-09-01 11:18:03 +07:00
|
|
|
|
2024-09-03 17:20:16 +07:00
|
|
|
// oled.setCursor(110,7);
|
|
|
|
|
// sprintf(timeBuffer, "%3d", (int)(Output*100));
|
|
|
|
|
// oled.print(timeBuffer);
|
|
|
|
|
// oled.update();
|
2024-08-29 23:58:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void formatTime(long seconds, char* buffer) {
|
2024-08-30 00:07:12 +07:00
|
|
|
long hours = seconds / 3600;
|
|
|
|
|
long mins = (seconds % 3600) / 60;
|
|
|
|
|
int secs = seconds % 60;
|
|
|
|
|
|
|
|
|
|
buffer[0] = '\0'; // Ensure the buffer is empty
|
|
|
|
|
|
|
|
|
|
if (hours > 0) {
|
|
|
|
|
sprintf(buffer + strlen(buffer), "%ldh", hours);
|
|
|
|
|
}
|
|
|
|
|
if (mins > 0) {
|
|
|
|
|
sprintf(buffer + strlen(buffer), "%ldm", mins);
|
|
|
|
|
}
|
|
|
|
|
if (secs > 0) {
|
|
|
|
|
sprintf(buffer + strlen(buffer), "%ds", secs);
|
|
|
|
|
}
|
2024-08-29 23:01:32 +07:00
|
|
|
}
|
2024-08-31 13:28:50 +07:00
|
|
|
|
|
|
|
|
void writeEEPROM() {
|
2024-09-01 11:18:03 +07:00
|
|
|
#if useEEPROM
|
2024-08-31 13:28:50 +07:00
|
|
|
lastEEPROMWriteTime = millis();
|
|
|
|
|
EEPROM.put(0, activeProfileIndex); // Store the active profile index
|
|
|
|
|
EEPROM.put(4, totalElapsedTime); // Store the total elapsed time
|
2024-09-07 23:11:57 +07:00
|
|
|
|
2024-08-31 13:28:50 +07:00
|
|
|
Serial.print("EEPROM written: ");
|
|
|
|
|
Serial.print(activeProfileIndex);
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.println(totalElapsedTime);
|
2024-09-01 11:18:03 +07:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void readEEPROM() {
|
|
|
|
|
#if useEEPROM
|
|
|
|
|
long time = 0;
|
|
|
|
|
EEPROM.get(0, activeProfileIndex); // Store the active profile index
|
|
|
|
|
EEPROM.get(4, time); // Store the total elapsed time
|
|
|
|
|
if (activeProfileIndex != 100) {
|
|
|
|
|
totalStartTime = millis() - time*1000;
|
|
|
|
|
calculateTotalTime();
|
|
|
|
|
getPhaseAndTemperature();
|
|
|
|
|
inSelectionMode = false;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
oled.clear();
|
|
|
|
|
displaySelection();
|
|
|
|
|
}
|
|
|
|
|
Serial.print("EEPROM read: ");
|
|
|
|
|
Serial.print(activeProfileIndex);
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.println(time);
|
|
|
|
|
#endif
|
2024-09-07 23:11:57 +07:00
|
|
|
}
|