Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- //================= AD converter ADS1115 =================
- #include <Adafruit_ADS1015.h>
- int tecla_lida_1 = 0;
- const int CntLimit = 200; // limite aproximadamente 8000
- int cnt = 0;
- int CntNow = 0;
- Adafruit_ADS1115 ads_ADC1 (0x48); // Construct an ads1115 at address: 0x48 (ADD-GND)
- int16_t intVolt_Rref ; int16_t intVolt_Rt; int16_t intCurrent_Ir; int16_t intT_par;
- float Volt_Rref[CntLimit];
- float Volt_Rt[CntLimit] ;
- unsigned long time_cnt[CntLimit];
- int delay_measure=300;
- #include "max6675.h"
- int ktcSO = 10;
- int ktcCS = 9;
- int ktcCLK = 8; //clock
- MAX6675 ktc(ktcCLK, ktcCS, ktcSO);
- //Adafruit_ADS1115 ads_ADC2 (0x49); // Construct an ads1115 at address: 0x48 (ADD-VDD)
- float T_par[CntLimit] ;
- float Current_I_H[CntLimit] ;
- float Current_I[CntLimit] ;
- //relays
- //int pin_Relay_1 = 4;
- //int pin_Relay_2 = 5;
- //int pin_Relay_3 = 6;
- //int pin_Relay_4 = 7;
- int PinHeater = 7;
- int HeaterState = LOW;
- char arrCharBuffer[CntLimit]; char printBuffer[128];
- float FactorMVoltperBit = 0.00018714 ; // 4920/26290
- float FactorPotentiometer = 34.4; // potential drop across the resistors network for electrolysis Voltage
- void setup(void)
- {
- Wire.begin(); // join i2c bus (address optional for master)
- Serial.begin(9600); // opens serial port, sets data rate to 57600 bps
- pinMode(PinHeater, OUTPUT);
- HeaterState = LOW;
- digitalWrite(PinHeater, HeaterState);
- // The ADC input range (or gain) can be changed via the following
- // functions, but be careful never to exceed VDD +0.3V max, or to
- // exceed the upper and lower limits if you adjust the input range!
- // Setting these values incorrectly may destroy your ADC!
- // ADS1015 ADS1115
- // ------- -------
- // ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default)
- // ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
- // ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
- // ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
- // ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV
- // ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV
- ads_ADC1.begin(); // ads1115 ADC1 initialize
- ads_ADC1.setGain(GAIN_TWO);
- // ads_ADC2.begin(); // ads1115 ADC2 initialize
- // ads_ADC2.setGain(GAIN_FOUR);
- // Switch_Relays_Positive();
- Serial.println("LabIII. Experiment on thermomentry: Max. Applied V[R]=1.4V,Max. Applied V[Heater]=5V ");
- }
- void loop() {
- tecla_lida_1 = 0;
- Serial.println("[[l,L]/[H,h] Heater OFF/ON --- [m,M]Medida Manual --- [a,A] Automatico--- [P,p] Print Measurements");
- while (tecla_lida_1 != 10) {
- if (Serial.available() > 0) {
- tecla_lida_1 = Serial.read();
- //Serial.println(tecla_lida_1);
- switch (tecla_lida_1) {
- // case 108:
- // Switch_Relays_Positive();
- // Serial.println(" Current Positive" );
- // break;
- // case 100:
- // Switch_Relays_Negative();
- // Serial.println(" Current Negative" );
- // break;
- case 72: //H
- case 104: //h
- HeaterState = HIGH;
- digitalWrite(PinHeater, HeaterState);
- Serial.println(" Heater is ON" );
- break;
- case 76: //L
- case 108: //l
- HeaterState = LOW;
- digitalWrite(PinHeater, HeaterState);
- Serial.println(" Heater is OFF" );
- break;
- case 109: //m
- case 77: //M
- Serial.println("Start measuring manually .....");
- MeasureData();
- Serial.println("....finsihed measuring manually");
- break;
- case 65: //A
- case 97: //a
- Serial.println("Start measuring automatically .....");
- for (int j = cnt; j < CntLimit; j = j + 1) {
- MeasureData();
- delay(delay_measure);
- // if (Serial.available() > 0) {
- // tecla_lida_1 = Serial.read();
- // if (tecla_lida_1 == 115 || tecla_lida_1 == 83) break;
- // }
- }
- Serial.println("...finished measuring automatically");
- cnt = 0;
- break;
- case 80: //P
- case 112: //p
- PrintMeasureData();
- break;
- } //switch
- }// serial
- } //while
- }
- /* ADCVoltageCh0 = (float)intVoltageADC0 * FactorMVoltperBit * FactorPotentiometer ;
- sprintf (arrCharBuffer, "Voltage channel 2 [V]= " );
- Serial.print (intVoltageADC2); Serial.print (" equivalent ");
- Serial.print (arrCharBuffer); Serial.println (ADCVoltageCh2, 5);
- */
- void MeasureData() {
- int i;
- int dummyRref = 0;
- int dummyT = 0;
- int dummy3 = 0;
- float dummy4 = 0.0;
- int dummyCNT = 2; // quantas vezes é medido para fazer média
- for (i = 0; i < dummyCNT; i = i + 1) {
- dummyT = dummyT + ads_ADC1.readADC_Differential_2_3(); // V_Rt
- delay(delay_measure);
- // Serial.println(dummyT);
- dummyRref = dummyRref + ads_ADC1.readADC_Differential_0_1(); // V_Rref
- // Serial.println(dummyRref);
- delay(delay_measure);
- // Serial.println(dummyRref);
- dummy3 = dummy3 + analogRead(A0); // heater current
- delay(delay_measure);
- dummy4 = dummy4 + ktc.readCelsius();
- delay(delay_measure);
- }
- time_cnt[cnt] = millis(); //para contagem de "tempo"
- Volt_Rt[cnt] = (1386.0 / 22020.0) * dummyT / dummyCNT;
- Volt_Rref[cnt] = (1256.0 / 20099.0) * dummyRref / dummyCNT; // current ref
- Current_I_H[cnt] = (19.98 / 512.0) * (-512.0 + dummy3 / dummyCNT);
- T_par[cnt] = dummy4 / dummyCNT + 273.13;
- // }
- // for (int cnt = 0; cnt < CntLimit; cnt = cnt + 1) {
- Serial.print("Counts=");
- Serial.print(cnt);
- Serial.print("; Time=");
- Serial.println(time_cnt[cnt]);
- Serial.print(" Temp K = ");
- Serial.print(T_par[cnt] );
- Serial.print("; I_Heater(A) = ");
- Serial.print(Current_I_H[cnt]);
- Serial.print("; I [mA]=" );
- Current_I[cnt] = Volt_Rref[cnt] / (999.6);
- Serial.println(Current_I[cnt]);
- Serial.print(" V_Rref[mV]=");
- Serial.print(Volt_Rref[cnt]);
- Serial.print("; R1[Ohm]=");
- Serial.println(Volt_Rref[cnt] / Current_I[cnt]);
- Serial.print(" V_Rt[mV]=");
- Serial.print(Volt_Rt[cnt]);
- Serial.print("; R2[Ohm]=");
- Serial.println(Volt_Rt[cnt] / Current_I[cnt]);
- if (cnt < CntLimit)
- {
- CntNow = cnt;
- cnt = cnt + 1;
- }
- else
- { cnt = 0;
- CntNow = 0;
- }
- }
- //void Switch_Relays_Positive()
- //{
- // //first switcvh off relay 3 and 4
- // pinMode(pin_Relay_3, OUTPUT);
- // digitalWrite(pin_Relay_3, HIGH);
- // delay(1000);
- // pinMode(pin_Relay_4, OUTPUT);
- // digitalWrite(pin_Relay_4, HIGH);
- // delay(1000);
- //
- // //Then swithch on 1 and 2
- // pinMode(pin_Relay_1, OUTPUT);
- // digitalWrite(pin_Relay_1, HIGH);
- // delay(1000);
- // pinMode(pin_Relay_2, OUTPUT);
- // digitalWrite(pin_Relay_2, HIGH);
- // // Then swtich back 3 and 4
- // delay(1000);
- // pinMode(pin_Relay_3, OUTPUT);
- // digitalWrite(pin_Relay_3, LOW);
- // delay(1000);
- // pinMode(pin_Relay_4, OUTPUT);
- // digitalWrite(pin_Relay_4, LOW);
- // delay(1000);
- //}
- //void Switch_Relays_Negative() {
- // //first switcvh off relay 3 and 4
- // pinMode(pin_Relay_3, OUTPUT);
- // digitalWrite(pin_Relay_3, HIGH);
- // delay(1000);
- // pinMode(pin_Relay_4, OUTPUT);
- // digitalWrite(pin_Relay_4, HIGH);
- // delay(1000);
- //
- // //Then swithch on 1 and 2
- // pinMode(pin_Relay_1, OUTPUT);
- // digitalWrite(pin_Relay_1, LOW);
- // delay(1000);
- // pinMode(pin_Relay_2, OUTPUT);
- // digitalWrite(pin_Relay_2, LOW);
- // // Then swtich back 3 and 4
- // delay(1000);
- // pinMode(pin_Relay_3, OUTPUT);
- // digitalWrite(pin_Relay_3, LOW);
- // delay(1000);
- // pinMode(pin_Relay_4, OUTPUT);
- // digitalWrite(pin_Relay_4, LOW);
- // delay(1000);
- //}
- void PrintMeasureData() {
- Serial.print("# ");
- Serial.print("t[ms] ");
- Serial.print("T[K] ");
- Serial.print("I_H(A) ");
- Serial.print("I [mA] " );
- Serial.print("V[mV] ");
- Serial.print("R[Ohm] ");
- Serial.print("V_Rt[mV] ");
- Serial.println("R2[Ohm] ");
- for (int i = 0; i <= CntNow; i = i + 1) {
- Serial.print(i);
- Serial.print(" ");
- Serial.print(time_cnt[i]);
- Serial.print(" ");
- Serial.print(T_par[i] );
- Serial.print(" ");
- Serial.print(Current_I_H[i]);
- Serial.print(" ");
- Serial.print(Current_I[i]);
- Serial.print(" ");
- Serial.print(Volt_Rref[i]);
- Serial.print(" ");
- Serial.print(Volt_Rref[i] / Current_I[i]);
- Serial.print(" ");
- Serial.print(Volt_Rt[i]);
- Serial.print(" ");
- Serial.println(Volt_Rt[i] / Current_I[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement