温度修正的电导率数据采集和存储-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1920|回复: 0

温度修正的电导率数据采集和存储

[复制链接]
发表于 2021-3-6 23:36 | 显示全部楼层 |阅读模式




  1. #include "Arduino.h"
  2. #include <OneWire.h>
  3. #include <SD.h>//SD卡模块
  4. #include <SPI.h>
  5. #include <OneWire.h>
  6. File myFile;

  7. #define EC_PIN A0
  8. float voltage, ecValue, temperature = 25.0;
  9. float _kvalueLow = 1, _kvalueHigh = 1, _kvalue = 1, rawEC, valueTemp, value;
  10. char teststring;                                    

  11. OneWire  ds(2);  // 连接arduino2引脚
  12. float celsius, fahrenheit;
  13. float Temp_Buffer = 0;

  14. void setup()
  15. {
  16.   pinMode(A0, INPUT);
  17.   Serial.begin(115200);
  18.   if (SD.begin())
  19.   {Serial.println("SD card is ready to use");
  20.   }else
  21.   {Serial.println("SD card aaaazinutialization failed");
  22.    return;
  23.     }
  24. //SD.remove("example.txt");  //删除文件example.txt         
  25. }


  26. void loop()
  27. {
  28.    
  29.     while(Serial.available()){
  30.     teststring = strcat(Serial.read(),".txt") ;
  31.     Serial.print(teststring);
  32.     myFile=SD.open("123.txt",FILE_WRITE);}
  33.    
  34. //   myFile=SD.open("test.txt",FILE_WRITE);//创建并打开文件进行写入
  35. //   myFile.print("ecValue=");
  36. //   myFile.println(ecValue);
  37. //   myFile.close();
  38.    
  39.   Temp_Buffer = readDs18b20();
  40.   
  41.   //digitalWrite(5, HIGH);
  42.   digitalWrite(5, LOW);
  43.   _kvalue = 1;
  44.   static unsigned long timepoint = millis();
  45.   if (millis() - timepoint > 1000U)
  46.   { timepoint = millis();
  47.     //Serial.print("A0="); Serial.print(analogRead(EC_PIN));
  48.     voltage = analogRead(EC_PIN) / 1024.0 * 5000;//电压毫伏
  49.    // Serial.print(" voltage="); Serial.print(voltage);
  50.     rawEC = 1000 * voltage / 820.0 / 200.0;//原始电导率,电压与电导率的线性比列关系,有先前实验得到
  51.     //Serial.print(" rawEC="); Serial.print(rawEC);
  52.     value = rawEC * _kvalue;//初始不计算仪器引起的误差
  53.     if (valueTemp > 2.5) {
  54.       _kvalue = _kvalueHigh;//高校正值
  55.     }
  56.     else if (valueTemp < 2.0) {
  57.       _kvalue = _kvalueLow;//低校正值
  58.     }
  59.    value = rawEC * _kvalue;//校正机械误差后的电导率,_kvalueLow和_kvalueHigh由实际的电导率比上测试得到的电导率
  60.    temperature =celsius; //dht.getTemperature();//校正后的电导率
  61.    ecValue = value / (1.0 + 0.0185 * (temperature - 25.0));//修正后的电导率,修正完以后都是25摄氏度下的数据,因此用除
  62.    //Serial.print(" temperature:");Serial.print(temperature, 1);Serial.print("^C  EC:");
  63.    Serial.print(ecValue);Serial.println("ms/cm");
  64.      
  65.   }
  66. }


  67. float readDs18b20()
  68. {
  69.   byte i;
  70.   byte present = 0;
  71.   byte type_s;
  72.   byte data[12];
  73.   byte addr[8];

  74.    
  75.   if ( !ds.search(addr)) {
  76.     // Serial.println("No more addresses.");
  77.     // Serial.println();
  78.     ds.reset_search();
  79.     // return 0;
  80.   }
  81.   
  82.    //Serial.print("ROM =");
  83.   for( i = 0; i < 8; i++) {
  84.     //Serial.write(' ');
  85.     //Serial.print(addr[i], HEX);
  86.   }

  87.   if (OneWire::crc8(addr, 7) != addr[7]) {
  88.      // Serial.println("CRC is not valid!");
  89.       return 0;
  90.   }
  91.   //Serial.println();
  92.   
  93.   // the first ROM byte indicates which chip
  94.   switch (addr[0]) {
  95.     case 0x10:
  96.       //Serial.println("  Chip = DS18S20");  // or old DS1820
  97.       type_s = 1;
  98.       break;
  99.     case 0x28:
  100.       //Serial.println("  Chip = DS18B20");
  101.       type_s = 0;
  102.       break;
  103.     case 0x22:
  104.      // Serial.println("  Chip = DS1822");
  105.       type_s = 0;
  106.       break;
  107.     default:
  108.      // Serial.println("Device is not a DS18x20 family device.");
  109.       return 0;
  110.   }

  111.   ds.reset();
  112.   ds.select(addr);
  113.   ds.write(0x44,1);         // start conversion, with parasite power on at the end
  114.    
  115.   delay(1000);     // maybe 750ms is enough, maybe not
  116.   // we might do a ds.depower() here, but the reset will take care of it.
  117.    
  118.   present = ds.reset();
  119.   ds.select(addr);   
  120.   ds.write(0xBE);         // Read Scratchpad

  121.   //Serial.print("  Data = ");
  122.   //Serial.print(present,HEX);
  123.   //Serial.print(" ");
  124.   for ( i = 0; i < 9; i++) {           // we need 9 bytes
  125.     data[i] = ds.read();
  126.    // Serial.print(data[i], HEX);
  127.   //  Serial.print(" ");
  128.   }
  129. // Serial.print(" CRC=");
  130. // Serial.print(OneWire::crc8(data, 8), HEX);
  131. // Serial.println();

  132.   // convert the data to actual temperature

  133.   unsigned int raw = (data[1] << 8) | data[0];
  134.   if (type_s) {
  135.     raw = raw << 3; // 9 bit resolution default
  136.     if (data[7] == 0x10) {
  137.       // count remain gives full 12 bit resolution
  138.       raw = (raw & 0xFFF0) + 12 - data[6];
  139.     }
  140.   } else {
  141.     byte cfg = (data[4] & 0x60);
  142.     if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
  143.     else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
  144.     else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
  145.     // default is 12 bit resolution, 750 ms conversion time
  146.   }
  147.   celsius = (float)raw / 16.0;
  148.   //Serial.print("  Temperature = ");
  149.   //Serial.print(celsius);
  150.   //Serial.print(" Celsius, ");   
  151.   return celsius;
  152. }








  153. //用于计算机械误差
  154. //    if()
  155. //    {if (rawEC>0.9 && rawEC<1.9)
  156. //      {compECsolution = 1.413*(1.0+0.0185*(temperature-25.0));//通过温度校正后的到的真实值
  157. //      kvalueLow = compECsolution/(1000.0*voltage/820.0/200.0);//机械校正系数为已知校正溶液的电导率比上实际测试得到的电导率   
  158. //      round(kvalueLow,2);}
  159. //    else if (rawEC>9 && rawEC<16.8)
  160. //      {compECsolution = 12.88*(1.0+0.0185*(temperature-25.0));
  161. //      kvalueHigh = compECsolution//(1000.0*voltage/820.0/200.0);//反过来如果已知了测试值想要得到真实值则应该乘以机械修正系数
  162. //      round(kvalueHigh,2);}
  163. //     }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-1 05:51 , Processed in 0.105459 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表