Electronic Expansion Valf Sürücü Kodlaması V1.0

Kodlama iyileştirildikçe bu başlık altında güncellemeler yapmaya çalışacağım.

//Author : Baris Ozbek
// V1.0 - 2020.05.25
// Device : Arduino UNO
// IDE : VSCODE C++

#include <Stepper.h>

//pinler
#define calispin 4
#define pressure A0 //Transmitter
#define ntc A1      // NTC Sensor

//degiskenler
double tevap = 2;    // Evap Sıcaklığı
double tsh = 5;      // Superheat
double sicaklik;     // NTC Değişkeni
int reftype = 1;     // 1: R404A , 2:R407C , 3:R410A , 4:R134A
double bar;          // Transmitter'den gelen değer
double z;       // Akışkan Termodinamik Özelliği
double tdiff;        // Sistem ile Setpoint arasındaki Fark Sıcaklık
double pmin = 0.00;  // Transmitter En düşük basınç değeri
double pmax = 30.00; // Transmitter En yüksek basınç değeri

// Number of steps per output rotation
const int stepsPerRevolution = 2500; // Valfin Adım Sayısı
const int stepsPerCycle = 45;        //Çevrimiçi hareket sayısı
const int stepsPerSmall = 5;         // Çevrimiçi +1/-1 bar arası hareket sayısı

// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 12, 11, 10, 9);

void setup()
{
  pinMode(calispin, INPUT);

  myStepper.setSpeed(20); // Step Motor Dönme Hızı

  Serial.begin(9600); // Seri Bağlantı
}

double Termistor(int analogOkuma) // NTC
{
  sicaklik = log(((10240000 / analogOkuma) - 10000));
  sicaklik = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * sicaklik * sicaklik)) * sicaklik);
  sicaklik = sicaklik - 273.15;
  return sicaklik;
}

void steppon() //Valfi Tam Açıyoruz
{
  // saat yönünde dön
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);
}

void steppoff() // Valfi Tam kapatıyoruz
{
  // Saat yönünün tersine dön
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

void eevplus() // 50 adım açma hareketi
{
  myStepper.step(stepsPerCycle);
  delay(200);
}

void eevminus() // 50 adım kapama hareketi
{
  myStepper.step(-stepsPerCycle);
  delay(200);
}

void eevplussmall() // 5 adım açma hareketi
{
  myStepper.step(stepsPerSmall);
  delay(200);
}

void eevminussmall() // 5 adım kapama hareketi
{
  myStepper.step(-stepsPerSmall);
  delay(200);
}

void measurePressure() //0.5 to 4.5V Pressure Transmitter Read
{
  int rawReading = analogRead(pressure); // Range : 0..1024
  //float rawVoltage = (rawReading * (4.5 / 1023.0)) + 0.5;
  bar = map(rawReading, 0, 1023.00, pmin, pmax);
  Serial.print("Raw bar: ");
  Serial.println(bar);
}

// eslestirme +15 / -40 arası calisacaktir.
void eslestir() // 1: R404A , 2:R407C , 3:R410A , 4:R134A
{
  if (reftype == 1 && bar < 1.01) //  R404A
  {
    double pa = 0.30;  //lover limit pressure
    double pb = 1.01;  //upper limit pressure
    double td = -40.0; // lover temp
    double te = -30.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 1 && bar < 1.99 && bar > 1.01) //  R404A
  {
    double pa = 1.01;  //lover limit pressure
    double pb = 1.99;  //upper limit pressure
    double td = -30.0; // lover temp
    double te = -20.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 1 && bar < 3.29 && bar > 1.99) //  R404A
  {
    double pa = 1.99;  //lover limit pressure
    double pb = 3.29;  //upper limit pressure
    double td = -20.0; // lover temp
    double te = -10.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 1 && bar < 4.99 && bar > 3.29) //  R404A
  {
    double pa = 3.29;  //lover limit pressure
    double pb = 4.99;  //upper limit pressure
    double td = -10.0; // lover temp
    double te = 0.0    // upper temp

        double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 1 && bar < 7.14 && bar > 4.99) //  R404A
  {
    double pa = 4.99; //lover limit pressure
    double pb = 7.14; //upper limit pressure
    double td = 0.0;  // lover temp
    double te = 10.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 1 && bar < 8.42 && bar > 7.14) //  R404A
  {
    double pa = 7.14; //lover limit pressure
    double pb = 8.42; //upper limit pressure
    double td = 10.0; // lover temp
    double te = 15.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }

  else if (reftype == 2 && bar < 0.37) //  R407C
  {
    double pa = -0.16; //lover limit pressure
    double pb = 0.37;  //upper limit pressure
    double td = -40.0; // lover temp
    double te = -30.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 2 && bar < 1.13 && bar > 0.37) //  R407C
  {
    double pa = 0.37;  //lover limit pressure
    double pb = 1.13;  //upper limit pressure
    double td = -30.0; // lover temp
    double te = -20.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 2 && bar < 2.18 && bar > 1.13) //  R407C

  {
    double pa = 1.13;  //lover limit pressure
    double pb = 2.18;  //upper limit pressure
    double td = -20.0; // lover temp
    double te = -10.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 2 && bar < 3.59 && bar > 2.18) //  R407C
  {
    double pa = 2.18;  //lover limit pressure
    double pb = 3.59;  //upper limit pressure
    double td = -10.0; // lover temp
    double te = 0.0;   // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 2 && bar < 5.44 && bar > 3.59) //  R407C
  {
    double pa = 3.59; //lover limit pressure
    double pb = 5.44; //upper limit pressure
    double td = 0.0;  // lover temp
    double te = 10.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 2 && bar < 6.54 && bar > 5.44) //  R407C
  {
    double pa = 5.44; //lover limit pressure
    double pb = 6.54; //upper limit pressure
    double td = 10.0; // lover temp
    double te = 15.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }

  else if (reftype == 3 && bar < 1.68) //  R410A
  {
    double pa = 0.74;  //lover limit pressure
    double pb = 1.68;  //upper limit pressure
    double td = -40.0; // lover temp
    double te = -30.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 3 && bar < 2.98 && bar > 1.68) //  R410A
  {
    double pa = 1.68;  //lover limit pressure
    double pb = 2.98;  //upper limit pressure
    double td = -30.0; // lover temp
    double te = -20.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 3 && bar < 4.71 && bar > 2.98) //  R410A
  {
    double pa = 2.98;  //lover limit pressure
    double pb = 4.71;  //upper limit pressure
    double td = -20.0; // lover temp
    double te = -10.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 3 && bar < 6.97 && bar > 4.71) //  R410A
  {
    double pa = 4.71;  //lover limit pressure
    double pb = 6.97;  //upper limit pressure
    double td = -10.0; // lover temp
    double te = 0.0;   // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 3 && bar < 9.83 && bar > 6.97) //  R410A
  {
    double pa = 6.97; //lover limit pressure
    double pb = 9.83; //upper limit pressure
    double td = 0.0;  // lover temp
    double te = 10.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 3 && bar < 11.53 && bar > 9.83) //  R410A
  {
    double pa = 9.83;  //lover limit pressure
    double pb = 11.53; //upper limit pressure
    double td = 10.0;  // lover temp
    double te = 15.00; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }

  else if (reftype == 4 && bar < -0.17) //  R134A
  {
    double pa = -0.50; //lover limit pressure
    double pb = -0.17; //upper limit pressure
    double td = -40.0; // lover temp
    double te = -30.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 4 && bar < 0.31 && bar > -0.17) //  R134A
  {
    double pa = -0.17; //lover limit pressure
    double pb = 0.31;  //upper limit pressure
    double td = -30.0; // lover temp
    double te = -20.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 4 && bar < 0.99 && bar > 0.31) //  R134A
  {
    double pa = 0.31;  //lover limit pressure
    double pb = 0.99;  //upper limit pressure
    double td = -20.0; // lover temp
    double te = -10.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 4 && bar < 1.91 && bar > 0.99) //  R134A
  {
    double pa = 0.99;  //lover limit pressure
    double pb = 1.91;  //upper limit pressure
    double td = -10.0; // lover temp
    double te = 0.0;   // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 4 && bar < 3.13 && bar > 1.91) //  R134A
  {
    double pa = 1.91; //lover limit pressure
    double pb = 3.13; //upper limit pressure
    double td = 0.0;  // lover temp
    double te = 10.0; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }
  else if (reftype == 4 && bar < 3.87 && bar > 3.13) //  R134A
  {
    double pa = 3.13;  //lover limit pressure
    double pb = 3.87;  //upper limit pressure
    double td = 10.0;  // lover temp
    double te = 15.00; // upper temp

    double c = pb - pa;
    double f = te - td;
    double g = bar - pa;
    double h = g / c;
    double q = h * (te - td);
    double z = q + td;
  }

  else
  {
    Serial.println("Akışkan tanımlı değil");
  }
}

void loop()
{
  if (digitalRead(calispin) == 1)
  {
    int deger = analogRead(A1);
    double sicaklik = Termistor(deger); // NTC Sıcaklığı

    measurePressure(); // basıncı oku
    steppoff();        // valfi tam kapat
    steppon();         // valfi tam aç
    eslestir();

    double totalheat = tevap + tsh;
    double tdiff = totalheat - z;
  }
  else
  {
    Serial.println("Calis Komutu Gelmedi");
  }

  while (tdiff > 3.0) // 5,4,3,2,1.1 e kadar çalışır
  {
    eevminus();
  }

  while (tdiff < -3.0) // -3,-2,-1 e kadar çalışır
  {
    eevplus();
  }

  while (tdiff < 0) // 1,0.9,0.8,...,0.3 e kadar çalışır
  {
    eevplussmall();
  }

  while (tdiff > 0)
  {
    eevminussmall();
  }
}