-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrentManager.cpp
More file actions
156 lines (132 loc) · 4.51 KB
/
CurrentManager.cpp
File metadata and controls
156 lines (132 loc) · 4.51 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "esp32-hal.h"
#include "portmacro.h"
#include "CurrentManager.h"
#include <Preferences.h>
float dynamicThreshold = 0.0;
Preferences preferences;
CurrentManager::CurrentManager(int pinNumber) {
pin = pinNumber;
zeroPoint = 1950;
lastCheck = 0;
lastPumpState = false;
// Initialize debounce variables
pumpStartTime = 0;
pumpStopTime = 0;
pumpIsActuallyRunning = false;
}
void CurrentManager::init() {
pinMode(pin, INPUT);
Serial.println("Waiting for SCT circuit to stabilize...");
int lastReading = analogRead(pin);
int stableCount = 0;
unsigned long startWait = millis();
bool isStable = false;
while (millis() - startWait < 3000) {
delay(10);
int currentReading = analogRead(pin);
if (abs(currentReading - lastReading) < 50) {
stableCount++;
} else {
stableCount = 0;
}
if (stableCount >= 15) {
isStable = true;
Serial.print("Circuit stable. Took (ms): ");
Serial.println(millis() - startWait);
break;
}
lastReading = currentReading;
}
if (isStable) {
calibrate();
preferences.begin("espresso", false);
preferences.putInt("zero", zeroPoint);
preferences.putFloat("thresh", dynamicThreshold);
preferences.end();
Serial.println("Calibration saved to memory.");
} else {
Serial.println("WARNING: Circuit unstable! Loading historical calibration...");
preferences.begin("espresso", true);
zeroPoint = preferences.getInt("zero", 1950);
dynamicThreshold = preferences.getFloat("thresh", 16.0);
preferences.end();
Serial.print("Loaded Zero Point: "); Serial.println(zeroPoint);
Serial.print("Loaded Threshold: "); Serial.println(dynamicThreshold);
}
}
void CurrentManager::calibrate() {
Serial.println("--- CALIBRATING NOISE FLOOR ---");
Serial.println("Keep Pump OFF for 1 second...");
long total = 0;
int samples = 0;
unsigned long start = millis();
while (millis() - start < 500) {
total += analogRead(pin);
samples++;
delay(1);
}
if (samples > 0) zeroPoint = total / samples;
float maxNoise = 0.0;
start = millis();
while (millis() - start < 1000) {
float currentReading = readStrength();
if (currentReading > maxNoise) {
maxNoise = currentReading;
}
delay(10);
}
dynamicThreshold = maxNoise + 20.0;
Serial.print("Zero Point: "); Serial.println(zeroPoint);
Serial.print("Noise Floor: "); Serial.println(maxNoise);
Serial.print("Auto-Threshold Set To: "); Serial.println(dynamicThreshold);
Serial.println("-------------------------------");
}
float CurrentManager::readStrength() {
long sumSquared = 0;
int samples = 0;
unsigned long startSample = millis();
while (millis() - startSample < 30) {
int raw = analogRead(pin);
long shifted = raw - zeroPoint;
sumSquared += (shifted * shifted);
samples++;
delay(1);
}
float meanSquare = (float)sumSquared / samples;
float rms = sqrt(meanSquare);
return rms * 0.50;
}
bool CurrentManager::isPumpOn() {
// Get the raw bouncy state (checking hardware every 100ms)
if (millis() - lastCheck >= 100) {
lastCheck = millis();
float strength = readStrength();
Serial.println(strength);
if (lastPumpState) {
if (strength < (dynamicThreshold - 1.0)) lastPumpState = false;
} else {
if (strength > dynamicThreshold) lastPumpState = true;
}
}
// Apply Time-Based Debounce to smooth out EMI spikes
if (lastPumpState && !pumpIsActuallyRunning) {
if (pumpStartTime == 0) pumpStartTime = millis();
// Must be ON for 250ms
if (millis() - pumpStartTime > 250) {
pumpIsActuallyRunning = true;
pumpStopTime = 0;
}
} else if (!lastPumpState && pumpIsActuallyRunning) {
if (pumpStopTime == 0) pumpStopTime = millis();
// Must be OFF for 500ms
if (millis() - pumpStopTime > 500) {
pumpIsActuallyRunning = false;
pumpStartTime = 0;
}
} else {
// Reset timers if the state bounces back
if (!lastPumpState) pumpStartTime = 0;
if (lastPumpState) pumpStopTime = 0;
}
return pumpIsActuallyRunning;
}