/*infobarquee 26/10/2013
projet pare buée télescope
*/
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_BMP085.h> // Adafruit BMP085 libary
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 // 0-83
#define MENU_Y 1 // 0-5
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
void setup()
{
pinMode(led,OUTPUT);
pinMode(led1,OUTPUT);
// setup interrupt-driven keypad arrays
// reset button arrays
for(byte i=0; i<NUM_KEYS; i++){
button_count*=0;*
button_status*=0;
button_flag=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) {}
}
}
/ loop /
void loop()
{
byte i;_
for(i=0; i<NUM_KEYS; i++){
if(button_flag !=0){
button_flag=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;
}
}
}*_
* float h = dht.readHumidity();*
* float t = dht.readTemperature();*
* int pointrosee = dewPoint(t, h);*
* char pointroseeBuf[8];*
* sprintf(pointroseeBuf, "%d",pointrosee);*
* int temp = dht.readTemperature();*
* char tempBuf[8];*
* sprintf(tempBuf, "%d",temp);*
* int hum = dht.readHumidity();*
* char humBuf[8];*
* sprintf(humBuf, "%d",hum);*
* if(tempBuf == pointroseeBuf || hum > 90 ){*
//lcd.backlight(OFF);
digitalWrite(led,1);
digitalWrite(led1,0);
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 );*
* } else {lcd.backlight(ON);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, 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 !=0){
button_flag=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);_
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*<DEBOUNCE_MAX)
_ {_
button_count++;
if(button_count>DEBOUNCE_ON)
_ {
if(button_status == 0)
{
button_flag = 1;
button_status = 1; //button debounced to 'pressed' status
}
}
}
}
else // no button pressed*
* {
if (button_count >0)
{ _
button_flag = 0;
button_count--;
if(button_count<DEBOUNCE_OFF){
button_status=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: Dew point - Wikipedia
double dewPointFast(double t, double h)
{
* double a = 17.271;
double b = 237.7;
double temp = (a * t) / (b + t) + log(h0.01);
double Td = (b * temp) / (a - temp);
* return Td;
}
[/quote]*_