วันนี้ทำโปรแกรมอ่านค่าเซ็นเซอร์วัดอุณหภูมิครับซึงผมที่ได้ทำโปรแกรมสามตัวครับโดยจะเอามาคำนวณหาค่าโดยใช้ฟังก์ชั้นทางคณิตศาสตร์ในการคำนวณครับ
const int analogInPin = A0;
int sensorValue = 0;
float RValue = 0;
float Rsensor = 0;
float Temperature = 0;
float Ta = 0;
float Tb = 0;
float Tc = 0;
float Td = 0;
const long table [72][2] =
{
{-55,10723236},{-50,7366689},{-44,5132757},{-40,3624165},{-35,2591304},
{-30,1874913},{-25,1371886},{-20,1014545},{-15,757881},{-10,571590},
{-5,435026},{0,333964},{5,258497},{10,201659},{15,158499},{20,125468},
{25,100000},{30,80223},{35,64759},{40,52589},{45,42951},{50,35272},
{55,29119},{60,24161},{65,20144},{70,16874},{75,14198},{80,11998},
{85,10181},{90,8674},{95,7419},{100,6369},{105,5487},{110,4744},
{115,4115},{120,3581},{125,3126},{130,2737},{135,2404},{140,2117},
{145,1869},{150,1655},{155,1469},{160,1307},{165,1166},{170,1043},
{175,934},{180,839},{185,755},{190,681},{195,615},{200,557},{205,505},
{210,459},{215,418},{220,382},{225,349},{230,319},{235,292},{240,269},
{245,247},{250,227},{255,210},{260,194},{265,179},{270,166},{275,154},
{280,143},{285,133},{290,124},{295,115},{300,107}};
void setup()
{
pinMode(A0,INPUT);
Serial.begin(9600);
}
void loop()
{
sensorValue = analogRead(analogInPin);
Serial.print("sensor = " );
Serial.print(sensorValue);
//RValue = ((50/(5-((sensorValue*5)/1024)))-10);
RValue = (sensorValue*5.0000)/1024.0000;
Serial.print(" \t R = " );
Serial.print(RValue);
Rsensor = ((50/(5 - RValue))-10)*1000;
Serial.print(" Rsensor = " );
Serial.println(Rsensor);
for(int i =0 ; i<72 ; i++)
{
if(Rsensor>table[i][1])
{
Serial.print(table[i][1]); //R_MAX
Serial.print(',');
Serial.print(table[i][0]); //T_MAX
Serial.print(',');
Serial.print(table[i-1][1]); //R_MIN
Serial.print(',');
Serial.println(table[i-1][0]); //T_MIN
Ta = ((table[i-1][1])-Rsensor);
Serial.print(" Ta = " );
Serial.print(Ta);
Tb = Ta/((table[i-1][1])-(table[i][1]));
Serial.print(" Tb = " );
Serial.print(Tb);
Tc = Tb*5;
Serial.print(" Tc = " );
Serial.print(Tc);
Temperature = (table[i][0])-Tc;
Serial.print(" Temperature = " );
Serial.println(Temperature);
break;
}
}
delay(5000);
}
ส่วนโปรแกรมที่เส็ดใช้ได้แล้วนะครับจะเป็นโปรแกรมนี้ครับ
const int analogInPin = A0;
int sensorValue = 0;
float RValue = 0;
float Rsensor = 0;
float Temperature = 0;
float Ta = 0;
float Tb = 0;
float Tc = 0;
float Td = 0;
const long table [72][2] =
{
{-55,10723236},{-50,7366689},{-44,5132757},{-40,3624165},{-35,2591304},
{-30,1874913},{-25,1371886},{-20,1014545},{-15,757881},{-10,571590},
{-5,435026},{0,333964},{5,258497},{10,201659},{15,158499},{20,125468},
{25,100000},{30,80223},{35,64759},{40,52589},{45,42951},{50,35272},
{55,29119},{60,24161},{65,20144},{70,16874},{75,14198},{80,11998},
{85,10181},{90,8674},{95,7419},{100,6369},{105,5487},{110,4744},
{115,4115},{120,3581},{125,3126},{130,2737},{135,2404},{140,2117},
{145,1869},{150,1655},{155,1469},{160,1307},{165,1166},{170,1043},
{175,934},{180,839},{185,755},{190,681},{195,615},{200,557},{205,505},
{210,459},{215,418},{220,382},{225,349},{230,319},{235,292},{240,269},
{245,247},{250,227},{255,210},{260,194},{265,179},{270,166},{275,154},
{280,143},{285,133},{290,124},{295,115},{300,107}};
void setup()
{
pinMode(A0,INPUT);
Serial.begin(9600);
}
void loop()
{
sensorValue = analogRead(analogInPin);
RValue = (sensorValue*5.0000)/1024.0000;
Rsensor = ((50/(5 - RValue))-10)*1000;
for(int i =0 ; i<72 ; i++)
{
if(Rsensor>table[i][1])
{
Ta = ((table[i-1][1])-Rsensor);
Tb = Ta/((table[i-1][1])-(table[i][1]));
Tc = Tb*5;
Temperature = (table[i][0])-Tc;
Serial.print(" Temperature = " );
Serial.println(Temperature);
break;
}
}
delay(5000);
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น