Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DHT.h"
- #define DHTPIN 5
- #define DHTTYPE DHT22 // DHT 22
- DHT dht(DHTPIN, DHTTYPE);
- //Rajouté *****************************
- #include <avr/sleep.h>
- #include <avr/power.h>
- #include <avr/wdt.h>
- int f_wdt; // flag interruption
- //***************************************
- void setup() {
- Serial.begin(9600);
- pinMode(6, OUTPUT); // BLUE led rgb
- pinMode(7, OUTPUT); // GREEN
- pinMode(8, OUTPUT); // RED
- Serial.println("DHTxx test!");
- dht.begin();
- //Rajouté *****************************
- //0=16ms, 1=32ms, 2=64ms, 3=128ms, 4=250ms,
- //5=500ms, 6=1 sec,7=2 sec, 8=4 sec, 9=8 sec
- setup_watchdog(9); // interruption toutes les 8 secondes
- //***************************************
- }
- void loop() {
- {
- //digitalWrite(6,125);digitalWrite(7,125);digitalWrite(8,125);
- // Wait a few seconds between measurements.
- delay(2000);// à virer
- // Reading temperature or humidity takes about 250 milliseconds!
- // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
- float h = dht.readHumidity();
- // Read temperature as Celsius (the default)
- float t = dht.readTemperature();
- // Read temperature as Fahrenheit (isFahrenheit = true)
- float f = dht.readTemperature(true);
- // Check if any reads failed and exit early (to try again).
- if (isnan(h) || isnan(t) || isnan(f))
- {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- Serial.print("Humidity: "); Serial.print(h); Serial.print("%\t");
- Serial.print("Temperature: "); Serial.print(t); Serial.println("*C");
- Serial.flush();
- if (t < 21.0)
- {
- digitalWrite(6, HIGH);// set LED BLUE
- digitalWrite(7, LOW);
- digitalWrite(8, LOW);
- }
- if ((t > 21.0) && (t < 22.0)) {
- digitalWrite(6, LOW);// set LED GREEN
- digitalWrite(7, HIGH);
- digitalWrite(8, LOW);
- }
- if (t > 22.0) {
- digitalWrite(6, LOW);// set LED RED
- digitalWrite(7, LOW);
- digitalWrite(8, HIGH);
- }
- f_wdt = 0; // Ne pas oublier d'initialiser le flag, sinon pas de sommeil
- //********************MODIFIE*********************************************
- while (f_wdt<2) // remplacer 2 par une autre valeur pour avoir un autre multiple de 8s
- {
- enterSleep(); //Revenir en mode veille
- }
- }
- // Tout ce qui est ci-dessous a été rajouté*********************
- //**************************************************************
- // Interruption Watchdog********
- ISR(WDT_vect)
- {
- f_wdt++; // **********incrémentation compteur watchdog**********
- }
- // fonction mise en veille*********
- void enterSleep(void)
- {
- // choix mode sleep complet
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- sleep_enable();
- sleep_mode();
- //le micro attend ici l'interruption Watchdog
- sleep_disable(); // désactivation veille
- }
- // initialisation compteur watchdog
- void setup_watchdog(int ii)
- {
- byte bb;
- int ww;
- if (ii > 9 ) ii = 9; // pour 8 secondes
- bb = ii & 7;
- if (ii > 7) bb |= (1 << 5);
- bb |= (1 << WDCE);
- ww = bb;
- // Clear the reset flag
- MCUSR &= ~(1 << WDRF);
- // start timed sequence
- WDTCSR |= (1 << WDCE) | (1 << WDE);
- // set new watchdog timeout value
- WDTCSR = bb;
- WDTCSR |= _BV(WDIE);
- }
Add Comment
Please, Sign In to add comment