#include <DHT.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;
#define DHTTYPE DHT22
#define DHTPIN A1
DHT dht(DHTPIN, DHTTYPE);
#include "LCD4884.h"
#include "DFrobot_bmp.h"
#include "DFrobot_chinese.h"
//keypad debounce parameter
#define DEBOUNCE_MAX 15
#define DEBOUNCE_ON 10
#define DEBOUNCE_OFF 3
#define NUM_KEYS 5
#define NUM_MENU_ITEM 5
// joystick number
#define LEFT_KEY 0
#define CENTER_KEY 1
#define DOWN_KEY 2
#define RIGHT_KEY 3
#define UP_KEY 4
// menu starting points
#define MENU_X 10
#define MENU_Y 1
int adc_key_val[5] ={
50, 200, 400, 600, 800 };
// debounce counters
byte button_count[NUM_KEYS];
// button status - pressed/released
byte button_status[NUM_KEYS];
// button on flags for user program
byte button_flag[NUM_KEYS];
// menu definition
char menu_items[NUM_MENU_ITEM][12]={
"TEMPERATURE",
"HYGROMETRIE",
"ALTITUDE",
"POINT_ROSEE",
"VENTILO"
};
void (*menu_funcs[NUM_MENU_ITEM])(void) = {
temperature,
hygrometrie,
altitude,
about,
ventilo
};
char current_menu_item;
#define led 13
#define led1 12
#define chauffage 11
int pointrosee ;
char pointroseeBuf[8];
int temp ;
char tempBuf[8];
int hum ;
char humBuf[8];
void setup()
{
pinMode(led,OUTPUT);
pinMode(led1,OUTPUT);
pinMode(chauffage, OUTPUT);
// setup interrupt-driven keypad arrays
// reset button arrays
for(byte i=0; i<NUM_KEYS; i++){
button_count[i]=0;
button_status[i]=0;
button_flag[i]=0;
}
// Setup timer2 -- Prescaler/256
TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
TCCR2B &= ~(1<<WGM22);
TCCR2B = (1<<CS22)|(1<<CS21);
ASSR |=(0<<AS2);
// Use normal mode
TCCR2A =0;
//Timer2 Overflow Interrupt Enable
TIMSK2 |= (0<<OCIE2A);
TCNT2=0x6; // counting starts from 6;
TIMSK2 = (1<<TOIE2);
SREG|=1<<SREG_I;
lcd.LCD_init();
lcd.LCD_clear();
//menu initialization
init_MENU();
current_menu_item = 0;
lcd.backlight(ON);//Turn on the backlight
//lcd.backlight(OFF); // Turn off the backlight
dht.begin();
// Sensor init
if (!bmp.begin()) {
Serial.println("No valid BMP085 sensor found!");
while (true) {}
}
}
void loop()
{
analogWrite(chauffage,0);
byte i;
for(i=0; i<NUM_KEYS; i++){
if(button_flag[i] !=0){
button_flag[i]=0; // reset button flag
switch(i){
case UP_KEY:
// current item to normal display
lcd.LCD_write_string(MENU_X, MENU_Y + current_menu_item, menu_items[current_menu_item], MENU_NORMAL );
current_menu_item -=1;
if(current_menu_item <0) current_menu_item = NUM_MENU_ITEM -1;
// next item to highlight display
lcd.LCD_write_string(MENU_X, MENU_Y + current_menu_item, menu_items[current_menu_item], MENU_HIGHLIGHT );
break;
case DOWN_KEY:
// current item to normal display
lcd.LCD_write_string(MENU_X, MENU_Y + current_menu_item, menu_items[current_menu_item], MENU_NORMAL );
current_menu_item +=1;
if(current_menu_item >(NUM_MENU_ITEM-1)) current_menu_item = 0;
// next item to highlight display
lcd.LCD_write_string(MENU_X, MENU_Y + current_menu_item, menu_items[current_menu_item], MENU_HIGHLIGHT );
break;
case LEFT_KEY:
init_MENU();
current_menu_item = 0;
break;
case CENTER_KEY:
lcd.LCD_clear();
(*menu_funcs[current_menu_item])();
lcd.LCD_clear();
init_MENU();
current_menu_item = 0;
break;
}
}
}
lecture();
while(tempBuf == pointroseeBuf || hum > 90 ){
lecture();
//lcd.backlight(OFF);
digitalWrite(led,1);
digitalWrite(led1,0);
analogWrite(chauffage,255);
lcd.LCD_write_string(10, 1, tempBuf, MENU_NORMAL);
lcd.LCD_write_string(10, 2, humBuf, MENU_NORMAL);
// lcd.LCD_write_string(38, 5, "OK", MENU_HIGHLIGHT );
// waitfor_OKkey();
}
digitalWrite(led,0);
digitalWrite(led1,1);
}
/* menu functions */
void init_MENU(void){
byte i;
lcd.LCD_clear();
lcd.LCD_write_string(MENU_X, MENU_Y, menu_items[0], MENU_HIGHLIGHT );
for (i=1; i<NUM_MENU_ITEM; i++){
lcd.LCD_write_string(MENU_X, MENU_Y+i, menu_items[i], MENU_NORMAL);
}
}
// waiting for center key press
void waitfor_OKkey(){
byte i;
byte key = 0xFF;
while (key!= CENTER_KEY){
for(i=0; i<NUM_KEYS; i++){
if(button_flag[i] !=0){
button_flag[i]=0; // reset button flag
if(i== CENTER_KEY) key=CENTER_KEY;
}
}
}
}
void temperature()
{
float t = dht.readTemperature();
char tempBuf[6]="";
sprintf(tempBuf, "%d",(int)t);
lcd.LCD_write_string_big(10, 1,tempBuf, MENU_NORMAL);
lcd.LCD_write_string(78, 2, "C", MENU_NORMAL);
lcd.LCD_write_string(38, 5, "OK", MENU_HIGHLIGHT );
waitfor_OKkey();
}
void hygrometrie(){
float h = dht.readHumidity();
char humBuf[6];
sprintf(humBuf, "%d",(int)h);
lcd.LCD_write_string_big(10, 1,humBuf, MENU_NORMAL);
lcd.LCD_write_string(78, 2, "%", MENU_NORMAL);
lcd.LCD_write_string(38, 5, "OK", MENU_HIGHLIGHT );
waitfor_OKkey();
}
void altitude(){
int pression = bmp.readAltitude();
char pressionBuf[8];
sprintf(pressionBuf, "%d",pression);
lcd.LCD_write_string_big(1, 1,pressionBuf, MENU_NORMAL);
lcd.LCD_write_string(78, 2, "M", MENU_NORMAL);
lcd.LCD_write_string(38, 5, "OK", MENU_HIGHLIGHT );
waitfor_OKkey();
}
void about(){
float h = dht.readHumidity();
float t = dht.readTemperature();
int point_rosee = dewPoint(t, h);
char point_roseeBuf[8];
sprintf(point_roseeBuf, "%d",point_rosee);
int point_rosee_fast = dewPointFast(t, h);
char point_rosee_fastBuf[8];
sprintf(point_rosee_fastBuf, "%d",point_rosee_fast);
lcd.LCD_write_string( 0, 20, point_rosee_fastBuf, MENU_NORMAL);
lcd.LCD_write_string( 15, 20, "C", MENU_NORMAL);
lcd.LCD_write_string( 23, 20, "PR Fast", MENU_NORMAL);
lcd.LCD_write_string( 0, 1, point_roseeBuf, MENU_NORMAL);
lcd.LCD_write_string( 15, 1, "C", MENU_NORMAL);
lcd.LCD_write_string( 23, 1, "P.rosee", MENU_NORMAL);
lcd.LCD_write_string(38, 5, "OK", MENU_HIGHLIGHT );
waitfor_OKkey();
}
void ventilo(){
lcd.LCD_write_string(0, 1,"Ventilo", MENU_NORMAL);
lcd.LCD_write_string(38, 5, "OK", MENU_HIGHLIGHT );
digitalWrite(led,1);
digitalWrite(led1,0);
analogWrite(chauffage, 255);
waitfor_OKkey();
}
// The followinging are interrupt-driven keypad reading functions
// which includes DEBOUNCE ON/OFF mechanism, and continuous pressing detection
// Convert ADC value to key number
char get_key(unsigned int input)
{
char k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{
return k;
}
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}
void update_adc_key(){
int adc_key_in;
char key_in;
byte i;
adc_key_in = analogRead(0);
key_in = get_key(adc_key_in);
for(i=0; i<NUM_KEYS; i++)
{
if(key_in==i) //one key is pressed
{
if(button_count[i]<DEBOUNCE_MAX)
{
button_count[i]++;
if(button_count[i]>DEBOUNCE_ON)
{
if(button_status[i] == 0)
{
button_flag[i] = 1;
button_status[i] = 1; //button debounced to 'pressed' status
}
}
}
}
else // no button pressed
{
if (button_count[i] >0)
{
button_flag[i] = 0;
button_count[i]--;
if(button_count[i]<DEBOUNCE_OFF){
button_status[i]=0; //button debounced to 'released' status
}
}
}
}
}
// Timer2 interrupt routine -
// 1/(160000000/256/(256-6)) = 4ms interval
ISR(TIMER2_OVF_vect) {
TCNT2 = 6;
update_adc_key();
}
double dewPoint(double t, double h)
{
// (1) Saturation Vapor Pressure = ESGG(T)
double RATIO = 373.15 / (273.15 + t);
double RHS = -7.90298 * (RATIO - 1);
RHS += 5.02808 * log10(RATIO);
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1/RATIO ))) - 1) ;
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
RHS += log10(1013.246);
// factor -3 is to adjust units - Vapor Pressure SVP * humidity
double VP = pow(10, RHS - 3) * h;
// (2) DEWPOINT = F(Vapor Pressure)
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558 - T);
}
// delta max = 0.6544 wrt dewPoint()
// 6.9 x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double t, double h)
{
double a = 17.271;
double b = 237.7;
double temp = (a * t) / (b + t) + log(h*0.01);
double Td = (b * temp) / (a - temp);
return Td;
}
void lecture(){
float h = dht.readHumidity();
float t = dht.readTemperature();
pointrosee = dewPoint(t, h);
char pointroseeBuf[8];
sprintf(pointroseeBuf, "%d",pointrosee);
temp = dht.readTemperature();
char tempBuf[8];
sprintf(tempBuf, "%d",temp);
hum = dht.readHumidity();
char humBuf[8];
sprintf(humBuf, "%d",hum);
}