void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
}
int minLight = 1023;
int maxLight = 0;
bool calibrating = false;
int calibrationStep = 0;
unsigned long buttonPressTime = 0;
void loop() {
int ph = analogRead(A0);
int button = digitalRead(2);
if (button == LOW) {
if (buttonPressTime == 0) {
buttonPressTime = millis();
}
if (millis() - buttonPressTime > 2000 && !calibrating) {
calibrating = true;
calibrationStep = 1;
minLight = 1023;
maxLight = 0;
}
} else {
if (buttonPressTime > 0 && millis() - buttonPressTime < 2000) {
if (calibrating) {
handleCalibrationStep();
}
}
buttonPressTime = 0;
}
if (calibrating) {
handleCalibration(ph);
} else {
int normalizedLight = map(ph, minLight, maxLight, 0, 100);
if (normalizedLight < 30) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
}
void handleCalibration(int ph) {
switch (calibrationStep) {
case 1:
digitalWrite(13, (millis() / 200) % 2);
maxLight = max(maxLight, ph);
break;
case 2: // Затемните датчик
digitalWrite(13, (millis() / 800) % 2);
minLight = min(minLight, ph);
break;
}
}
void handleCalibrationStep() {
calibrationStep++;
if (calibrationStep > 2) {
calibrating = false;
}
}