added active buzzer + refactoring

This commit is contained in:
Alexander Belov 2024-09-07 23:11:57 +07:00
parent 8f9584a818
commit 92adfc73d8

View File

@ -1,6 +1,7 @@
#define useEEPROM 1 #define useEEPROM 1
// #define TempSensorMax // #define TempSensorMax
#define TempSensorDallas #define TempSensorDallas
// #define PlotValues
#include <Wire.h> #include <Wire.h>
#include <GyverOLED.h> #include <GyverOLED.h>
@ -43,6 +44,7 @@ Encoder enc1(CLK, DT, SW, TYPE2);
// SSR pin configuration // SSR pin configuration
const int ssrPin = 2; const int ssrPin = 2;
const int activeBuzzerPin = 8;
// Profile structure definition // Profile structure definition
struct Phase { struct Phase {
@ -59,13 +61,14 @@ struct Profile {
// Profiles definition // Profiles definition
Profile profiles[] = { Profile profiles[] = {
{"Chickpeas", 2, {{30, 1}, {47, 120}, {55, 60}, {65, 150}, {70, 60}, {90, 105}}, 6}, {"Chickpeas", 2, {{47, 120}, {55, 60}, {65, 150}, {70, 60}, {90, 105}}, 5},
{"Lentils", 3, {{47, 120}, {53, 120}, {65, 180}, {72, 90}, {90, 15}}, 5}, {"Lentils", 3, {{47, 120}, {53, 120}, {65, 180}, {72, 90}, {90, 15}}, 5},
{"Lentils gradual", 10, {{48, 120}, {80, 60}}, 2}, {"Lentils gradual", 10, {{48, 120}, {80, 60}}, 2},
{"Soy gradual", 10, {{42, 1}, {48, 120}, {80, 60}, {90, 15}}, 4},
{"Wheat", 1, {{47, 60}, {53, 60}, {65, 20}, {72, 20}, {85, 20}}, 5}, {"Wheat", 1, {{47, 60}, {53, 60}, {65, 20}, {72, 20}, {85, 20}}, 5},
{"55-30&85-120", 1, {{55, 30}, {85, 120}}, 2}, {"55-30&85-120", 1, {{55, 30}, {85, 120}}, 2},
{"46-45", 0, {{46, 45}}, 1}, {"46-45", 0, {{46, 45}}, 1},
{"Test", 1, {{49, 1}, {51, 1}}, 2}, {"Test", 3, {{51, 1}}, 1},
}; };
// Global variables for profile selection and execution // Global variables for profile selection and execution
@ -84,7 +87,7 @@ bool inSelectionMode = true;
#include "GyverPID.h" #include "GyverPID.h"
double Setpoint, Input, Output; double Setpoint, Input, Output;
GyverPID regulator(1, 0.5, 0, 1000); GyverPID regulator(50, 0, 0, 1000);
// Timing variables // Timing variables
long phaseStartTime; long phaseStartTime;
@ -104,17 +107,22 @@ const int ssrSwitchInterval = 1000; // SSR switching interval in milliseconds
// Buffer for formatted time strings // Buffer for formatted time strings
char timeBuffer[10]; char timeBuffer[10];
uint32_t timer = 0; uint32_t timerExecution = 0;
#define T_PERIOD 1000 // period of Execution processing #define T_PERIOD_EXEC 1000 // period of Execution processing
uint32_t timerSSR = 0; uint32_t timerSSR = 0;
#define T_PERIOD_SSR 500 // period of heater handling #define T_PERIOD_SSR 500 // period of heater handling
uint32_t timerDallas = 0; uint32_t timerTemp = 0;
#define T_PERIOD_DALLAS 1000 // period of dallas sensor requesting #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;
#define PARTS 4 #define PARTS 4
void setup() { void setup() {
pinMode(ssrPin, OUTPUT); pinMode(ssrPin, OUTPUT);
pinMode(activeBuzzerPin, OUTPUT);
Serial.begin(9600); Serial.begin(9600);
oled.init(); // Initialize the OLED oled.init(); // Initialize the OLED
@ -131,8 +139,6 @@ void setup() {
sensors.setResolution(tempDeviceAddress, 12); sensors.setResolution(tempDeviceAddress, 12);
sensors.setWaitForConversion(false); sensors.setWaitForConversion(false);
#endif #endif
// Serial.println("Input, Output, Real Output");
} }
void loop() { void loop() {
@ -143,28 +149,54 @@ void loop() {
if (isLeft || isRight || isClick) { if (isLeft || isRight || isClick) {
handleEncoder(); handleEncoder();
} }
handleTemperatureSensor();
handleExecution(); handleExecution();
handleHeaterAdv(); handleHeaterAdv();
#ifdef TempSensorDallas handleAdditionalChecks();
handleDallasTemperatureSensor();
#endif
} }
#ifdef TempSensorDallas void handleAdditionalChecks() {
void handleDallasTemperatureSensor() { 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() {
long time = millis(); long time = millis();
static int currentPart = 0; static int currentPart = 0;
if (time - timerDallas < T_PERIOD_DALLAS) { if (time - timerTemp < T_PERIOD_TEMP) {
return; return;
} }
timerDallas = time; timerTemp = time;
#ifdef TempSensorDallas
Input = (double) sensors.getTempCByIndex(0); Input = (double) sensors.getTempCByIndex(0);
sensors.requestTemperatures(); // Send the command to get temperatures sensors.requestTemperatures(); // Send the command to get temperatures
}
#endif #endif
#ifdef TempSensorMax // to go into the read temp function, rename from dallas temp
Input = thermocouple.readCelsius();
#endif
if (isnan(Input)) {
Input = 100;
}
}
void handleHeaterSimple() { void handleHeaterSimple() {
long time = millis(); long time = millis();
@ -223,7 +255,7 @@ void handleHeaterAdv() {
} }
} }
bool next = current >= last ? true : false; bool next = current >= last ? true : false;
if (Output < 0.05) next = false; if (Output < 1) next = false;
states[PARTS-1] = next; states[PARTS-1] = next;
oled.setCursor(128-6*PARTS, 7); oled.setCursor(128-6*PARTS, 7);
@ -243,12 +275,14 @@ void handleHeaterAdv() {
currentPart = 0; currentPart = 0;
} }
#ifdef PlotValues
Serial.print(Setpoint); Serial.print(Setpoint);
Serial.print(","); Serial.print(",");
Serial.print(Input); Serial.print(Input);
Serial.print(","); Serial.print(",");
Serial.print(Output); Serial.print(Output);
Serial.println(",0,100"); Serial.println(",0,100");
#endif
} }
@ -263,7 +297,6 @@ void handleEncoder() {
void handleExecutionSelection() { void handleExecutionSelection() {
if (isClick) { if (isClick) {
Serial.println("clicked! resetting...");
activeProfileIndex = 100; activeProfileIndex = 100;
activeProfile = profiles[activeProfileIndex]; activeProfile = profiles[activeProfileIndex];
inSelectionMode = true; inSelectionMode = true;
@ -278,10 +311,10 @@ void handleExecutionSelection() {
void handleExecution() { void handleExecution() {
currentTime = millis(); currentTime = millis();
if (inSelectionMode || (currentTime - timer < T_PERIOD)) { // таймер на millis() if (inSelectionMode || (currentTime - timerExecution < T_PERIOD_EXEC)) { // таймер на millis()
return; return;
} }
timer = currentTime; timerExecution = currentTime;
totalElapsedTime = (currentTime - totalStartTime) / 1000; // Total elapsed time in seconds totalElapsedTime = (currentTime - totalStartTime) / 1000; // Total elapsed time in seconds
@ -291,16 +324,9 @@ void handleExecution() {
getPhaseAndTemperature(); getPhaseAndTemperature();
#ifdef TempSensorMax
Input = thermocouple.readCelsius();
#endif
if (isnan(Input)) {
Input = 0;
}
regulator.input = (float)Input; regulator.input = (float)Input;
regulator.setpoint = Setpoint; regulator.setpoint = Setpoint;
regulator.getResult(); regulator.getResultNow();
Output = regulator.output; Output = regulator.output;
if (isComplete && currentPhase >= activeProfile.numPhases && !finishTime) { if (isComplete && currentPhase >= activeProfile.numPhases && !finishTime) {
@ -323,7 +349,7 @@ void startExecution() {
getPhaseAndTemperature(); getPhaseAndTemperature();
writeEEPROM(); writeEEPROM();
oled.clear(); oled.clear();
isComplete = false; boolLastCompletedState = isComplete = false;
} }
void handleProfileSelection() { void handleProfileSelection() {
@ -523,6 +549,7 @@ void writeEEPROM() {
lastEEPROMWriteTime = millis(); lastEEPROMWriteTime = millis();
EEPROM.put(0, activeProfileIndex); // Store the active profile index EEPROM.put(0, activeProfileIndex); // Store the active profile index
EEPROM.put(4, totalElapsedTime); // Store the total elapsed time EEPROM.put(4, totalElapsedTime); // Store the total elapsed time
Serial.print("EEPROM written: "); Serial.print("EEPROM written: ");
Serial.print(activeProfileIndex); Serial.print(activeProfileIndex);
Serial.print(" "); Serial.print(" ");