Advertisement
toplinearfunction

thermocircuit

Nov 11th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. //================= AD converter ADS1115 =================
  3. #include <Adafruit_ADS1015.h>
  4. int tecla_lida_1 = 0;
  5. const int CntLimit = 200; // limite aproximadamente 8000
  6. int cnt = 0;
  7. int CntNow = 0;
  8. Adafruit_ADS1115 ads_ADC1 (0x48);   // Construct an ads1115 at address: 0x48 (ADD-GND)
  9. int16_t  intVolt_Rref ; int16_t  intVolt_Rt; int16_t  intCurrent_Ir; int16_t  intT_par;
  10. float Volt_Rref[CntLimit];
  11. float Volt_Rt[CntLimit] ;
  12. unsigned long time_cnt[CntLimit];
  13.  
  14. int delay_measure=300;
  15.  
  16. #include "max6675.h"
  17. int ktcSO = 10;
  18. int ktcCS = 9;
  19. int ktcCLK = 8; //clock
  20.  
  21. MAX6675 ktc(ktcCLK, ktcCS, ktcSO);
  22.  
  23. //Adafruit_ADS1115 ads_ADC2 (0x49);   // Construct an ads1115 at address: 0x48 (ADD-VDD)
  24. float T_par[CntLimit] ;
  25. float Current_I_H[CntLimit] ;
  26. float Current_I[CntLimit] ;
  27. //relays
  28. //int pin_Relay_1 = 4;
  29. //int pin_Relay_2 = 5;
  30. //int pin_Relay_3 = 6;
  31. //int pin_Relay_4 = 7;
  32. int PinHeater = 7;
  33. int HeaterState = LOW;
  34. char arrCharBuffer[CntLimit]; char printBuffer[128];
  35. float FactorMVoltperBit = 0.00018714 ; // 4920/26290
  36. float FactorPotentiometer = 34.4; // potential drop across the resistors network for electrolysis Voltage
  37.  
  38.  
  39. void setup(void)
  40. {
  41.   Wire.begin(); // join i2c bus (address optional for master)
  42.   Serial.begin(9600);     // opens serial port, sets data rate to 57600 bps
  43.  
  44.   pinMode(PinHeater, OUTPUT);
  45.   HeaterState = LOW;
  46.   digitalWrite(PinHeater, HeaterState);
  47.  
  48.   // The ADC input range (or gain) can be changed via the following
  49.   // functions, but be careful never to exceed VDD +0.3V max, or to
  50.   // exceed the upper and lower limits if you adjust the input range!
  51.   // Setting these values incorrectly may destroy your ADC!
  52.   //                                                                ADS1015  ADS1115
  53.   //                                                                -------  -------
  54.   // ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- 6.144V  1 bit = 3mV      0.1875mV (default)
  55.   // ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  56.   // ads.setGain(GAIN_TWO);        // 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV
  57.   // ads.setGain(GAIN_FOUR);       // 4x gain   +/- 1.024V  1 bit = 0.5mV    0.03125mV
  58.   // ads.setGain(GAIN_EIGHT);      // 8x gain   +/- 0.512V  1 bit = 0.25mV   0.015625mV
  59.   // ads.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV
  60.  
  61.  
  62.   ads_ADC1.begin(); // ads1115 ADC1 initialize
  63.   ads_ADC1.setGain(GAIN_TWO);
  64.  
  65.   // ads_ADC2.begin(); // ads1115 ADC2 initialize
  66.   //  ads_ADC2.setGain(GAIN_FOUR);
  67.   //  Switch_Relays_Positive();
  68.  
  69.   Serial.println("LabIII. Experiment on thermomentry: Max. Applied V[R]=1.4V,Max. Applied V[Heater]=5V ");
  70. }
  71.  
  72. void loop() {
  73.   tecla_lida_1 = 0;
  74.   Serial.println("[[l,L]/[H,h] Heater OFF/ON --- [m,M]Medida Manual --- [a,A] Automatico--- [P,p] Print Measurements");
  75.  
  76.   while (tecla_lida_1 != 10) {
  77.     if (Serial.available() > 0) {
  78.       tecla_lida_1 = Serial.read();
  79.       //Serial.println(tecla_lida_1);
  80.       switch (tecla_lida_1) {
  81.         //        case 108:
  82.         //          Switch_Relays_Positive();
  83.         //          Serial.println(" Current Positive" );
  84.         //          break;
  85.         //        case 100:
  86.         //          Switch_Relays_Negative();
  87.         //          Serial.println(" Current Negative" );
  88.         //          break;
  89.        
  90.         case 72: //H
  91.         case 104: //h
  92.           HeaterState = HIGH;
  93.           digitalWrite(PinHeater, HeaterState);
  94.           Serial.println(" Heater is ON" );
  95.           break;
  96.          
  97.         case 76: //L
  98.         case 108: //l
  99.           HeaterState = LOW;
  100.           digitalWrite(PinHeater, HeaterState);
  101.           Serial.println(" Heater is OFF" );
  102.           break;
  103.          
  104.         case 109: //m
  105.         case 77: //M
  106.           Serial.println("Start measuring manually .....");
  107.           MeasureData();
  108.           Serial.println("....finsihed measuring manually");
  109.           break;
  110.          
  111.         case 65: //A
  112.         case 97: //a
  113.           Serial.println("Start measuring automatically .....");
  114.           for (int j = cnt; j < CntLimit; j = j + 1) {
  115.             MeasureData();
  116.             delay(delay_measure);
  117.             //             if (Serial.available() > 0) {
  118.             //               tecla_lida_1 = Serial.read();
  119.             //             if (tecla_lida_1 == 115 || tecla_lida_1 == 83)  break;
  120.             //              }
  121.           }
  122.           Serial.println("...finished measuring automatically");
  123.           cnt = 0;
  124.           break;
  125.          
  126.         case 80:   //P
  127.         case 112:  //p
  128.           PrintMeasureData();
  129.           break;
  130.          
  131.       } //switch
  132.     }// serial
  133.   } //while
  134. }
  135.  
  136.  
  137. /* ADCVoltageCh0 = (float)intVoltageADC0 * FactorMVoltperBit * FactorPotentiometer ;
  138.     sprintf (arrCharBuffer, "Voltage channel 2 [V]= " );
  139.     Serial.print (intVoltageADC2); Serial.print (" equivalent  ");
  140.     Serial.print (arrCharBuffer);   Serial.println (ADCVoltageCh2, 5);
  141. */
  142.  
  143. void MeasureData() {
  144.   int i;
  145.   int dummyRref = 0;
  146.   int dummyT = 0;
  147.   int dummy3 = 0;
  148.   float dummy4 = 0.0;
  149.   int dummyCNT = 2; // quantas vezes é medido para fazer média
  150.  
  151.   for (i = 0; i < dummyCNT; i = i + 1) {
  152.     dummyT =  dummyT + ads_ADC1.readADC_Differential_2_3(); // V_Rt
  153.     delay(delay_measure);
  154.    //  Serial.println(dummyT);
  155.     dummyRref =  dummyRref +  ads_ADC1.readADC_Differential_0_1(); // V_Rref
  156.    //      Serial.println(dummyRref);
  157.     delay(delay_measure);
  158.     //    Serial.println(dummyRref);
  159.     dummy3 =  dummy3 + analogRead(A0); // heater current
  160.     delay(delay_measure);
  161.     dummy4 =  dummy4 + ktc.readCelsius();
  162.     delay(delay_measure);
  163.   }
  164.  
  165.   time_cnt[cnt] = millis();  //para contagem de "tempo"
  166.   Volt_Rt[cnt] =  (1386.0 / 22020.0) * dummyT / dummyCNT;
  167.   Volt_Rref[cnt] =  (1256.0 / 20099.0) * dummyRref / dummyCNT; // current ref
  168.   Current_I_H[cnt] = (19.98 / 512.0) * (-512.0 + dummy3 / dummyCNT);
  169.   T_par[cnt] = dummy4 / dummyCNT + 273.13;
  170.   //    }
  171.   // for (int cnt = 0; cnt < CntLimit; cnt = cnt + 1) {
  172.   Serial.print("Counts=");
  173.   Serial.print(cnt);
  174.   Serial.print("; Time=");
  175.   Serial.println(time_cnt[cnt]);
  176.  
  177.   Serial.print(" Temp K = ");
  178.   Serial.print(T_par[cnt] );
  179.   Serial.print("; I_Heater(A) = ");
  180.   Serial.print(Current_I_H[cnt]);
  181.   Serial.print(";   I [mA]=" );
  182.   Current_I[cnt] = Volt_Rref[cnt] / (999.6);
  183.   Serial.println(Current_I[cnt]);
  184.  
  185.   Serial.print(" V_Rref[mV]=");
  186.   Serial.print(Volt_Rref[cnt]);
  187.   Serial.print("; R1[Ohm]=");
  188.   Serial.println(Volt_Rref[cnt] / Current_I[cnt]);
  189.  
  190.   Serial.print(" V_Rt[mV]=");
  191.   Serial.print(Volt_Rt[cnt]);
  192.   Serial.print("; R2[Ohm]=");
  193.   Serial.println(Volt_Rt[cnt] / Current_I[cnt]);
  194.   if  (cnt < CntLimit)
  195.   {
  196.     CntNow = cnt;
  197.     cnt = cnt + 1;
  198.   }
  199.   else
  200.   { cnt = 0;
  201.     CntNow = 0;
  202.   }
  203.  
  204. }
  205. //void Switch_Relays_Positive()
  206. //{
  207. //  //first switcvh off relay 3 and 4
  208. //  pinMode(pin_Relay_3, OUTPUT);
  209. //  digitalWrite(pin_Relay_3, HIGH);
  210. //  delay(1000);
  211. //  pinMode(pin_Relay_4, OUTPUT);
  212. //  digitalWrite(pin_Relay_4, HIGH);
  213. //  delay(1000);
  214. //
  215. //  //Then swithch on 1 and 2
  216. //  pinMode(pin_Relay_1, OUTPUT);
  217. //  digitalWrite(pin_Relay_1, HIGH);
  218. //  delay(1000);
  219. //  pinMode(pin_Relay_2, OUTPUT);
  220. //  digitalWrite(pin_Relay_2, HIGH);
  221. //  // Then swtich back 3 and 4
  222. //  delay(1000);
  223. //  pinMode(pin_Relay_3, OUTPUT);
  224. //  digitalWrite(pin_Relay_3, LOW);
  225. //  delay(1000);
  226. //  pinMode(pin_Relay_4, OUTPUT);
  227. //  digitalWrite(pin_Relay_4, LOW);
  228. //  delay(1000);
  229. //}
  230. //void Switch_Relays_Negative() {
  231. //  //first switcvh off relay 3 and 4
  232. //  pinMode(pin_Relay_3, OUTPUT);
  233. //  digitalWrite(pin_Relay_3, HIGH);
  234. //  delay(1000);
  235. //  pinMode(pin_Relay_4, OUTPUT);
  236. //  digitalWrite(pin_Relay_4, HIGH);
  237. //  delay(1000);
  238. //
  239. //  //Then swithch on 1 and 2
  240. //  pinMode(pin_Relay_1, OUTPUT);
  241. //  digitalWrite(pin_Relay_1, LOW);
  242. //  delay(1000);
  243. //  pinMode(pin_Relay_2, OUTPUT);
  244. //  digitalWrite(pin_Relay_2, LOW);
  245. //  // Then swtich back 3 and 4
  246. //  delay(1000);
  247. //  pinMode(pin_Relay_3, OUTPUT);
  248. //  digitalWrite(pin_Relay_3, LOW);
  249. //  delay(1000);
  250. //  pinMode(pin_Relay_4, OUTPUT);
  251. //  digitalWrite(pin_Relay_4, LOW);
  252. //  delay(1000);
  253. //}
  254. void PrintMeasureData() {
  255.   Serial.print("#   ");
  256.   Serial.print("t[ms]   ");
  257.   Serial.print("T[K]    ");
  258.   Serial.print("I_H(A)  ");
  259.   Serial.print("I [mA]  " );
  260.   Serial.print("V[mV]   ");
  261.   Serial.print("R[Ohm]  ");
  262.   Serial.print("V_Rt[mV]    ");
  263.   Serial.println("R2[Ohm]   ");
  264.   for (int i = 0; i <= CntNow; i = i + 1) {
  265.     Serial.print(i);
  266.     Serial.print("  ");
  267.     Serial.print(time_cnt[i]);
  268.     Serial.print("  ");
  269.     Serial.print(T_par[i] );
  270.     Serial.print("  ");
  271.     Serial.print(Current_I_H[i]);
  272.     Serial.print("  ");
  273.     Serial.print(Current_I[i]);
  274.     Serial.print("  ");
  275.     Serial.print(Volt_Rref[i]);
  276.     Serial.print("  ");
  277.     Serial.print(Volt_Rref[i] / Current_I[i]);
  278.     Serial.print("  ");
  279.     Serial.print(Volt_Rt[i]);
  280.     Serial.print("  ");
  281.     Serial.println(Volt_Rt[i] / Current_I[i]);
  282.   }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement