โค้ด: เลือกทั้งหมด
// DS18S20 humidity sensor
// detect from wet and dry themometer
// to calculate humidity
//
#include <avr/interrupt.h>
#include <OneWire.h>
OneWire* ds ;
byte data[12];
float temp1,temp2,temp_d,d,Humi,Dewc;
float ah_temp1 = 27;// reference active temp(active over)
float al_humi = 70; // reference active humidity(active under)
int div_time = 20;//different time for sensor checking
int int_counter = 0;
volatile int second = 0;
int oldSecond = 0;
#define atpreaa 1013.25 //Mean Sea Level Stand Pressure
#define M_E 2.72 // E Constant
#define SD 3 //Sensor Dry
#define SW 4 // Sensor Wet
//#define ATMEGA168 1
#define INIT_TIMER_COUNT 6
#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT
#define LEDPIN 13
ISR(TIMER2_OVF_vect) {
RESET_TIMER2;
int_counter += 1;
if (int_counter == 1000 ) {
second+=1;
int_counter = 0;
}
};
void setup_timer(void){
//Timer2 Settings: Timer Prescaler /64,
#ifdef ATMEGA168
TCCR2B |= (1<<CS22); // turn on CS22 bit
TCCR2B &= ~((1<<CS21) | (1<<CS20)); // turn off CS21 and CS20 bits
#else
TCCR2 |= (1<<CS22); // turn on CS22 bit
TCCR2 &= ~((1<<CS21) | (1<<CS20)); // turn off CS21 and CS20 bits
#endif
// Use normal mode
#ifdef ATMEGA168
TCCR2A &= ~((1<<WGM21) | (1<<WGM20)); // turn off WGM21 and WGM20 bits
TCCR2B &= ~(1<<WGM22); // turn off WGM22
#else
TCCR2 &= ~((1<<WGM21) | (1<<WGM20)); // turn off WGM21 and WGM20 bits
#endif
// Use internal clock - external clock not used in Arduino
ASSR |= (0<<AS2);
#ifdef ATMEGA168
TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A); //Timer2 Overflow Interrupt Enable
#else
TIMSK |= (1<<TOIE2) | (0<<OCIE2); //Timer2 Overflow Interrupt Enable
#endif
RESET_TIMER2;
sei();
}
void setup(void) {
Serial.begin(9600);
setup_timer();
}
void loop(void) {
if (oldSecond <= second) {
oldSecond = second + div_time;
ds = &OneWire(SD);
if(readtemp(&temp1)){
ds = &OneWire(SW);
if(readtemp(&temp2)){
temp_d = abs(temp1-temp2);
cal_humid();
cal_dewpoint();
showoutput();
activeoutput();
}
}
}
}
void activeoutput(void){
if((al_humi > Humi) || (ah_temp1 < temp1 )){
digitalWrite(LEDPIN, HIGH);
}else{
digitalWrite(LEDPIN, LOW);
}
}
void showoutput(void){
Serial.print("$");
Serial.print(",");
Serial.print(temp1);
Serial.print(",");
Serial.print(temp2);
Serial.print(",");
Serial.print(temp_d);
Serial.print(",");
Serial.print(Humi);
Serial.print(",");
Serial.print(Dewc);
Serial.println();
}
boolean readtemp(float* temp3){
byte i;
byte present = 0;
byte addr[8];
if ( !ds->search(addr)) {
ds->reset_search();
return false;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
return false;
}
if ( addr[0] != 0x10) {
return false;
}
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
present = ds->reset();
ds->select(addr);
ds->write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds->read();
}
*temp3=data[0];
if(data[1] !=0)
*temp3 = *temp3 - 256.0 ;
*temp3 =(float) (*temp3/2.0);
return true;
}
void cal_dewpoint(void){
float B;
B = log(d / 6.108) / 17.27;
Dewc = (237.3 * B) / (1 - B);
}
float cal_vapor(float ac){
return 6.122 * pow(M_E,(17.67*ac)/(243.5+ac));
}
void cal_humid(void){
float c,dw,e;
c=cal_vapor(temp1);//saturation vapor pressure
dw=cal_vapor(temp2);//actual vapor pressure
d=dw - (0.00066*(1+0.00115*temp2)*(temp_d)*atpreaa);
Humi=(d/c)*100;
}
link เพิ่มเติม
viewtopic.php?f=4&t=2832
http://www.engineeringtoolbox.com/humid ... d_561.html
http://en.wikipedia.org/wiki/Dew_point
http://mygarden.bangnamphueng.com/?p=71#more-71