mich29800

Watchdog

Feb 12th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "DHT.h"
  2. #define DHTPIN 5
  3. #define DHTTYPE DHT22 // DHT 22
  4. DHT dht(DHTPIN, DHTTYPE);
  5.  
  6. //Rajouté *****************************
  7. #include <avr/sleep.h>
  8. #include <avr/power.h>
  9. #include <avr/wdt.h>
  10. int f_wdt; // flag interruption
  11. //***************************************
  12.  
  13. void setup() {
  14. Serial.begin(9600);
  15. pinMode(6, OUTPUT); // BLUE led rgb
  16. pinMode(7, OUTPUT); // GREEN
  17. pinMode(8, OUTPUT); // RED
  18. Serial.println("DHTxx test!");
  19. dht.begin();
  20.  
  21. //Rajouté *****************************
  22. //0=16ms, 1=32ms, 2=64ms, 3=128ms, 4=250ms,
  23. //5=500ms, 6=1 sec,7=2 sec, 8=4 sec, 9=8 sec
  24.  setup_watchdog(9); // interruption toutes les 8 secondes
  25. //***************************************
  26. }
  27. void loop() {
  28.  
  29. {
  30. //digitalWrite(6,125);digitalWrite(7,125);digitalWrite(8,125);
  31. // Wait a few seconds between measurements.
  32.  
  33. delay(2000);// à virer
  34.  
  35. // Reading temperature or humidity takes about 250 milliseconds!
  36. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  37. float h = dht.readHumidity();
  38. // Read temperature as Celsius (the default)
  39. float t = dht.readTemperature();
  40. // Read temperature as Fahrenheit (isFahrenheit = true)
  41. float f = dht.readTemperature(true);
  42. // Check if any reads failed and exit early (to try again).
  43. if (isnan(h) || isnan(t) || isnan(f))
  44. {
  45. Serial.println("Failed to read from DHT sensor!");
  46. return;
  47. }
  48. Serial.print("Humidity: "); Serial.print(h); Serial.print("%\t");
  49. Serial.print("Temperature: "); Serial.print(t); Serial.println("*C");
  50. Serial.flush();
  51. if (t < 21.0)
  52. {
  53. digitalWrite(6, HIGH);// set LED BLUE
  54. digitalWrite(7, LOW);
  55. digitalWrite(8, LOW);
  56. }
  57. if ((t > 21.0) && (t < 22.0)) {
  58. digitalWrite(6, LOW);// set LED GREEN
  59. digitalWrite(7, HIGH);
  60. digitalWrite(8, LOW);
  61. }
  62. if (t > 22.0) {
  63. digitalWrite(6, LOW);// set LED RED
  64. digitalWrite(7, LOW);
  65. digitalWrite(8, HIGH);
  66. }
  67.  
  68. f_wdt = 0; // Ne pas oublier d'initialiser le flag, sinon pas de sommeil
  69. //********************MODIFIE*********************************************
  70. while (f_wdt<2)    // remplacer 2 par une autre valeur pour avoir un autre multiple de 8s
  71. {
  72. enterSleep(); //Revenir en mode veille
  73. }
  74.  
  75.  
  76. }
  77. // Tout ce qui est ci-dessous a été rajouté*********************
  78. //**************************************************************
  79.  
  80.  
  81. // Interruption Watchdog********
  82. ISR(WDT_vect)
  83. {
  84.  
  85.     f_wdt++; // **********incrémentation compteur watchdog**********
  86. }
  87.  
  88. // fonction mise en veille*********
  89. void enterSleep(void)
  90. {
  91. // choix mode sleep complet
  92.   set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  93.   sleep_enable();
  94.   sleep_mode();
  95.   //le micro attend ici l'interruption Watchdog
  96.   sleep_disable(); // désactivation veille
  97.  
  98. }
  99.  
  100. // initialisation compteur watchdog
  101. void setup_watchdog(int ii)
  102. {
  103.   byte bb;
  104.   int ww;
  105.   if (ii > 9 ) ii = 9;   // pour 8 secondes
  106.   bb = ii & 7;
  107.   if (ii > 7) bb |= (1 << 5);
  108.   bb |= (1 << WDCE);
  109.   ww = bb;
  110.   // Clear the reset flag
  111.   MCUSR &= ~(1 << WDRF);
  112.   // start timed sequence
  113.   WDTCSR |= (1 << WDCE) | (1 << WDE);
  114.   // set new watchdog timeout value
  115.   WDTCSR = bb;
  116.   WDTCSR |= _BV(WDIE);
  117. }
Add Comment
Please, Sign In to add comment