温度修正的电导率数据采集和存储
#include "Arduino.h"
#include <OneWire.h>
#include <SD.h>//SD卡模块
#include <SPI.h>
#include <OneWire.h>
File myFile;
#define EC_PIN A0
float voltage, ecValue, temperature = 25.0;
float _kvalueLow = 1, _kvalueHigh = 1, _kvalue = 1, rawEC, valueTemp, value;
char teststring;
OneWireds(2);// 连接arduino2引脚
float celsius, fahrenheit;
float Temp_Buffer = 0;
void setup()
{
pinMode(A0, INPUT);
Serial.begin(115200);
if (SD.begin())
{Serial.println("SD card is ready to use");
}else
{Serial.println("SD card aaaazinutialization failed");
return;
}
//SD.remove("example.txt");//删除文件example.txt
}
void loop()
{
while(Serial.available()){
teststring = strcat(Serial.read(),".txt") ;
Serial.print(teststring);
myFile=SD.open("123.txt",FILE_WRITE);}
// myFile=SD.open("test.txt",FILE_WRITE);//创建并打开文件进行写入
// myFile.print("ecValue=");
// myFile.println(ecValue);
// myFile.close();
Temp_Buffer = readDs18b20();
//digitalWrite(5, HIGH);
digitalWrite(5, LOW);
_kvalue = 1;
static unsigned long timepoint = millis();
if (millis() - timepoint > 1000U)
{ timepoint = millis();
//Serial.print("A0="); Serial.print(analogRead(EC_PIN));
voltage = analogRead(EC_PIN) / 1024.0 * 5000;//电压毫伏
// Serial.print(" voltage="); Serial.print(voltage);
rawEC = 1000 * voltage / 820.0 / 200.0;//原始电导率,电压与电导率的线性比列关系,有先前实验得到
//Serial.print(" rawEC="); Serial.print(rawEC);
value = rawEC * _kvalue;//初始不计算仪器引起的误差
if (valueTemp > 2.5) {
_kvalue = _kvalueHigh;//高校正值
}
else if (valueTemp < 2.0) {
_kvalue = _kvalueLow;//低校正值
}
value = rawEC * _kvalue;//校正机械误差后的电导率,_kvalueLow和_kvalueHigh由实际的电导率比上测试得到的电导率
temperature =celsius; //dht.getTemperature();//校正后的电导率
ecValue = value / (1.0 + 0.0185 * (temperature - 25.0));//修正后的电导率,修正完以后都是25摄氏度下的数据,因此用除
//Serial.print(" temperature:");Serial.print(temperature, 1);Serial.print("^CEC:");
Serial.print(ecValue);Serial.println("ms/cm");
}
}
float readDs18b20()
{
byte i;
byte present = 0;
byte type_s;
byte data;
byte addr;
if ( !ds.search(addr)) {
// Serial.println("No more addresses.");
// Serial.println();
ds.reset_search();
// return 0;
}
//Serial.print("ROM =");
for( i = 0; i < 8; i++) {
//Serial.write(' ');
//Serial.print(addr, HEX);
}
if (OneWire::crc8(addr, 7) != addr) {
// Serial.println("CRC is not valid!");
return 0;
}
//Serial.println();
// the first ROM byte indicates which chip
switch (addr) {
case 0x10:
//Serial.println("Chip = DS18S20");// or old DS1820
type_s = 1;
break;
case 0x28:
//Serial.println("Chip = DS18B20");
type_s = 0;
break;
case 0x22:
// Serial.println("Chip = DS1822");
type_s = 0;
break;
default:
// Serial.println("Device is not a DS18x20 family device.");
return 0;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
//Serial.print("Data = ");
//Serial.print(present,HEX);
//Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data = ds.read();
// Serial.print(data, HEX);
//Serial.print(" ");
}
// Serial.print(" CRC=");
// Serial.print(OneWire::crc8(data, 8), HEX);
// Serial.println();
// convert the data to actual temperature
unsigned int raw = (data << 8) | data;
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data == 0x10) {
// count remain gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data;
}
} else {
byte cfg = (data & 0x60);
if (cfg == 0x00) raw = raw << 3;// 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
//Serial.print("Temperature = ");
//Serial.print(celsius);
//Serial.print(" Celsius, ");
return celsius;
}
//用于计算机械误差
// if()
// {if (rawEC>0.9 && rawEC<1.9)
// {compECsolution = 1.413*(1.0+0.0185*(temperature-25.0));//通过温度校正后的到的真实值
// kvalueLow = compECsolution/(1000.0*voltage/820.0/200.0);//机械校正系数为已知校正溶液的电导率比上实际测试得到的电导率
// round(kvalueLow,2);}
// else if (rawEC>9 && rawEC<16.8)
// {compECsolution = 12.88*(1.0+0.0185*(temperature-25.0));
// kvalueHigh = compECsolution//(1000.0*voltage/820.0/200.0);//反过来如果已知了测试值想要得到真实值则应该乘以机械修正系数
// round(kvalueHigh,2);}
// }
页:
[1]