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