From de6a48a730e59a4dba4e64c39c01ac8a6d86e46d Mon Sep 17 00:00:00 2001 From: andrea Date: Fri, 28 Jun 2024 01:01:44 +0800 Subject: [PATCH] mosftes --- opncure.ino | 123 +++++++++++++++++++--------------------------------- 1 file changed, 45 insertions(+), 78 deletions(-) diff --git a/opncure.ino b/opncure.ino index bf3ab41..22b646d 100644 --- a/opncure.ino +++ b/opncure.ino @@ -5,15 +5,13 @@ #include #include -#define DHT1_PIN 11 -#define DHT2_PIN 12 -#define RELAY_COOLING_PIN 13 -#define RELAY_HEATING_PIN 14 +#define DHT_PIN 11 +#define MOSFET_HEATING_PIN 13 +#define MOSFET_COOLING_PIN 14 #define DHT_TYPE DHT11 -DHT dht1(DHT1_PIN, DHT_TYPE); -DHT dht2(DHT2_PIN, DHT_TYPE); +DHT dht(DHT_PIN, DHT_TYPE); LiquidCrystal_I2C lcd(0x27, 20, 4); ESP8266WebServer server(80); @@ -28,21 +26,20 @@ float targetTemperature = 25.0; unsigned long startTime = 0; unsigned long duration = 0; -float surfaceTemp = 0.0; -float airTemp = 0.0; -float surfaceHum = 0.0; -float airHum = 0.0; +float fridgeTemp = 0.0; + +bool heatingActive = false; +bool coolingActive = false; void setup() { Serial.begin(115200); - pinMode(RELAY_COOLING_PIN, OUTPUT); - pinMode(RELAY_HEATING_PIN, OUTPUT); - digitalWrite(RELAY_COOLING_PIN, LOW); - digitalWrite(RELAY_HEATING_PIN, LOW); + pinMode(MOSFET_HEATING_PIN, OUTPUT); + pinMode(MOSFET_COOLING_PIN, OUTPUT); + analogWriteRange(255); // Set PWM range to 0-255 + analogWriteFreq(20000); // Set PWM frequency to 20 kHz - dht1.begin(); - dht2.begin(); + dht.begin(); lcd.begin(20,4); lcd.backlight(); @@ -75,7 +72,7 @@ void setup() { void loop() { server.handleClient(); updateSensors(); - controlRelays(); + controlPeltier(); updateLCD(); switch (currentMode) { @@ -89,56 +86,59 @@ void loop() { curveMode(); break; case OFF: - disableRelays(); + disablePeltier(); break; } } void updateSensors() { - surfaceTemp = dht1.readTemperature(); - surfaceHum = dht1.readHumidity(); - airTemp = dht2.readTemperature(); - airHum = dht2.readHumidity(); - Serial.printf("Surface Temp: %.1f C, Air Temp: %.1f C\n", surfaceTemp, airTemp); + fridgeTemp = dht.readTemperature(); + Serial.printf("Fridge Temp: %.1f C\n", fridgeTemp); } -void enableCooling() { - digitalWrite(RELAY_HEATING_PIN, LOW); - delay(100); - digitalWrite(RELAY_COOLING_PIN, HIGH); +void controlHeating(int dutyCycle) { + analogWrite(MOSFET_COOLING_PIN, 0); // Ensure cooling is off + analogWrite(MOSFET_HEATING_PIN, dutyCycle); + heatingActive = true; + coolingActive = false; } -void enableHeating() { - digitalWrite(RELAY_COOLING_PIN, LOW); - delay(100); - digitalWrite(RELAY_HEATING_PIN, HIGH); +void controlCooling(int dutyCycle) { + analogWrite(MOSFET_HEATING_PIN, 0); // Ensure heating is off + analogWrite(MOSFET_COOLING_PIN, dutyCycle); + coolingActive = true; + heatingActive = false; } -void disableRelays() { - digitalWrite(RELAY_COOLING_PIN, LOW); - digitalWrite(RELAY_HEATING_PIN, LOW); +void disablePeltier() { + analogWrite(MOSFET_HEATING_PIN, 0); + analogWrite(MOSFET_COOLING_PIN, 0); + heatingActive = false; + coolingActive = false; } -void controlRelays() { +void controlPeltier() { if (currentMode == OFF) { - disableRelays(); + disablePeltier(); return; } if (targetTemperature < 25) { - if (airTemp > targetTemperature) { - enableCooling(); + if (fridgeTemp > targetTemperature) { + int dutyCycle = map(fridgeTemp - targetTemperature, 0, 10, 0, 255); + controlCooling(dutyCycle); } else { - disableRelays(); + disablePeltier(); } } else if (targetTemperature > 25) { - if (airTemp < targetTemperature) { - enableHeating(); + if (fridgeTemp < targetTemperature) { + int dutyCycle = map(targetTemperature - fridgeTemp, 0, 10, 0, 255); + controlHeating(dutyCycle); } else { - disableRelays(); + disablePeltier(); } } else { - disableRelays(); + disablePeltier(); } } @@ -150,16 +150,11 @@ void updateLCD() { lcd.print(" C"); lcd.setCursor(0, 1); - lcd.print("Surface Temp: "); - lcd.print(surfaceTemp, 1); + lcd.print("Fridge Temp: "); + lcd.print(fridgeTemp, 1); lcd.print(" C"); lcd.setCursor(0, 2); - lcd.print("Air Temp: "); - lcd.print(airTemp, 1); - lcd.print(" C"); - - lcd.setCursor(0, 3); lcd.print("Mode: "); switch (currentMode) { case TIME_MODE: @@ -170,41 +165,13 @@ void updateLCD() { break; case CURVE_MODE: lcd.print("Curve Mode"); - lcd.setCursor(0, 3); - lcd.print("Elapsed: "); - lcd.print((millis() - startTime) / 1000, 1); - lcd.print(" s"); break; case OFF: - default: lcd.print("Off"); break; } } -void timeMode() { - if (millis() - startTime >= duration * 3600 * 1000) { - currentMode = OFF; - } - controlRelays(); -} - -void holdMode() { - controlRelays(); -} - -void curveMode() { - float elapsedTime = (millis() - startTime) / 1000.0; - float gaussValue = exp(-pow((elapsedTime - duration / 2.0), 2) / (2 * pow(duration / 6.0, 2))); - targetTemperature = targetTemperature * gaussValue; - - if (elapsedTime >= duration) { - currentMode = OFF; - } - - controlRelays(); -} - void handleRoot() { String html = R"rawliteral(