petit programme que je m'étais fait pour l'astro avec un dht22, un BMP085
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_BMP085.h> // Adafruit BMP085 libary
#include "LCD4884.h"
#include "DFrobot_bmp.h"
#include "DFrobot_chinese.h"
Adafruit_BMP085 bmp;
#define DHTTYPE DHT22
#define DHTPIN A1
DHT dht(DHTPIN, DHTTYPE);
#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",
"PRESSION",
"TOUT"
};
void (*menu_funcs[NUM_MENU_ITEM])(void) = {
temperature,
hygrometrie,
altitude,
pression,
tout
};
char current_menu_item;
void setup()
{
Serial.begin(9600);
Serial.println("DHT TEST PROGRAM ");
Serial.println();
dht.begin();
// Sensor init
if (!bmp.begin()) {
Serial.println("No valid BMP085 sensor found!");
while (true) {}
}
// 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
}
void loop()
{
Serial.println("\n");
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("Read sensor: ");
Serial.print("Humidity : ");
Serial.print(h, 2);
Serial.println("%");
Serial.print("Temperature : ");
Serial.print(t, 2);
Serial.println("C");
Serial.print("Dew Point : ");
Serial.print(dewPoint(t, h));
Serial.println("C");
Serial.print("Dew PointFast : ");
Serial.print(dewPointFast(t,h));
Serial.println("C");
Serial.print("Temperature: ");
Serial.print(bmp.readTemperature());
Serial.print(" "); // Here we have to cheat a little
Serial.write(176); // to show the "°" character
Serial.println("C");
Serial.print("Pression: ");
Serial.print(bmp.readPressure());
Serial.println(" Pa");
Serial.print("Altitude: ");
Serial.print(bmp.readAltitude());
Serial.println(" meters");
delay(2000);
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;
}
}
}
}
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 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()
{
lcd.LCD_write_string_big(10, 1, "+12.30", MENU_NORMAL);
lcd.LCD_write_string(78, 2, "C", MENU_NORMAL);
lcd.LCD_write_string(38, 5, "OK", MENU_HIGHLIGHT );
waitfor_OKkey();
}
void hygrometrie(){
char i,j;
for(i=0; i<5; i++){
for(j=0; j<14; j++){
lcd.LCD_set_XY(j*6,i);
lcd.LCD_write_char(i*14+j+32, MENU_NORMAL);
}
}
lcd.LCD_write_string(38, 5, "OK", MENU_HIGHLIGHT );
waitfor_OKkey();
}
void altitude(){
lcd.LCD_draw_bmp_pixel(0,0, DFrobot_bmp, 84,24);
lcd.LCD_write_chinese(6,3, DFrobot_chinese,12,6,0,0);
lcd.LCD_write_string(38, 5, "OK", MENU_HIGHLIGHT );
waitfor_OKkey();
}
void pression(){
lcd.LCD_write_string( 0, 1, "LCD4884 Shield", MENU_NORMAL);
lcd.LCD_write_string( 0, 3, "www.DFrobot.cn", MENU_NORMAL);
lcd.LCD_write_string(38, 5, "OK", MENU_HIGHLIGHT );
waitfor_OKkey();
}
void tout(){
lcd.LCD_write_string( 0, 1, "LCD4884 Shield", MENU_NORMAL);
lcd.LCD_write_string( 0, 3, "www.DFrobot.cn", MENU_NORMAL);
lcd.LCD_write_string(38, 5, "OK", MENU_HIGHLIGHT );
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();
}