21.12.2013Teplotní čidlo LM335Z
Kód pro měření ze 4 čidel i s korekcí. Je potřeba nastavit korekci individuálně podlě jednotlivých čidel.
Program očekává příkaz, na který odpoví hodnotou (T1,T2,T3,T4,ALL)
temperaturesensordemo.ino
const int T1_InPin = A7; // Analog input pin to measure
float T1 = 0;
float T1_fix = -0.2;
const int T2_InPin = A5; // Analog input pin to measure
float T2 = 0;
float T2_fix = -1.2;
const int T3_InPin = A4; // Analog input pin to measure
float T3 = 0;
float T3_fix = -1.4;
const int T4_InPin = A3; // Analog input pin to measure
float T4 = 0;
float T4_fix = -2;
const int L1_InPin = A0; // Analog input pin to measure
float L1 = 0;
float L1_fix = 0;
char chr;
char string2[32]; // command string (maximum length of the command is set to 16 bytes)
int string2idx = 0;
int command_int=0;
void setup() {
Serial.begin(9600);
pinMode(T1_InPin, INPUT);
pinMode(T2_InPin, INPUT);
pinMode(T3_InPin, INPUT);
pinMode(T4_InPin, INPUT);
pinMode(L1_InPin, INPUT);
}
void loop() {
while (Serial.available() > 0) { // while there are data on serial input
int inChar = Serial.read(); // fill the byte on input into char variable
chr=(char)inChar;
if (inChar!=13 && inChar!=10) { // if input is not command divider
string2[string2idx++] = inChar; // add the char in the string array
string2[string2idx] = 0; // add 0 byte on the end of the array
}
if (inChar == 13) { // if it's the same char as the divider of commands
command_int=atoi(string2); // if you want to transform the input string into an integer, use this function
if(strcmp(string2,"T1")==0)
{
T1=getTemperatureLevel(T1_InPin,T1_fix); // get temp on sensor 1
Serial.println(T1,5); // send the measurement to the serial port
}
else if(strcmp(string2,"T2")==0)
{
T2=getTemperatureLevel(T2_InPin,T2_fix); // get temp on sensor 1
Serial.println(T2,5); // send the measurement to the serial port
}
else if(strcmp(string2,"T3")==0)
{
T2=getTemperatureLevel(T3_InPin,T3_fix); // get temp on sensor 1
Serial.println(T3,5); // send the measurement to the serial port
}
else if(strcmp(string2,"T4")==0)
{
T2=getTemperatureLevel(T4_InPin,T4_fix); // get temp on sensor 1
Serial.println(T4,5); // send the measurement to the serial port
}
else if(strcmp(string2,"L1")==0)
{
L1=getLightLevel(L1_InPin);
Serial.println(L1,5); // send the measurement to the serial port
}
else if(strcmp(string2,"ALL")==0)
{
T1=getTemperatureLevel(T1_InPin,T1_fix); // get temp on sensor 1
T2=getTemperatureLevel(T2_InPin,T2_fix); // get temp on sensor 1
T3=getTemperatureLevel(T3_InPin,T3_fix); // get temp on sensor 1
T4=getTemperatureLevel(T4_InPin,T4_fix); // get temp on sensor 1
L1=getLightLevel(L1_InPin);
Serial.print(T1,5); // send the measurement to the serial port
Serial.print("|"); // send the measurement to the serial port
Serial.print(T2,5); // send the measurement to the serial port
Serial.print("|"); // send the measurement to the serial port
Serial.print(T3,5); // send the measurement to the serial port
Serial.print("|"); // send the measurement to the serial port
Serial.print(T4,5); // send the measurement to the serial port
Serial.print("|"); // send the measurement to the serial port
Serial.println(L1,5); // send the measurement to the serial port
}
else
{
Serial.print("ERROR#UNKNOWN COMMAND#");
Serial.println(string2);
}
string2[0] = 0;string2idx = 0; // empty the command
}
}
//T1=getTemperatureLevel(T1_InPin,-1.5); // get temp on sensor 1
//Serial.print("output = ");Serial.println(T1,5); // send the measurement to the serial port
//delay(1000);
}
float getTemperatureLevel(int input_pin,float kalibrace)
{
float val=0;
float volt=0;
float temp_k=0;
float temp_c=0;
float correction=273.15;
for(int i=0;i<50;i++){val+=analogRead(input_pin);}val=val/50.0;val=round(val); // read the sensor 100 times and take average value
volt=val*(3.56/1024.0);//0.003320312;
temp_k=volt*100;
temp_c=temp_k-correction+kalibrace;
return temp_c;
}
float getLightLevel(int input_pin)
{
float val=0;
for(int i=0;i<50;i++){val+=analogRead(input_pin);}val=val/50.0;val=round(val); // read the sensor 100 times and take average value
val=100-((val*100.0)/1024.0);
return val;
}
comments powered by Disqus

