Advertisement
skizziks_53

Arduino DHT greenhouse v2.0

Aug 24th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.25 KB | None | 0 0
  1. /*
  2.    Arduino Uno
  3.    Greenhouse sketch v 2.0
  4.    24 August 2018
  5.  
  6.    Any sketch that you use a non-standard library in, you should always write where you got the library from.
  7.  
  8.    This sketch uses the dht library mentioned on this web page:
  9.    https://playground.arduino.cc/Main/DHTLib
  10.    The library files are available at:
  11.    https://github.com/RobTillaart/Arduino/tree/master/libraries/DHTlib
  12.  
  13.  
  14. */
  15.  
  16. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  17. // These are variables that I have added:
  18.  
  19. int sensor_check_cycle = 60; // This is the time interval to check the sensors, in seconds.
  20. int seconds_counter = 0; // This is used for counting seconds, until it is equal to sensor_check_cycle above.
  21.  
  22. int ledBlinkPin = 13; // This sketch blinks the pin-13 LED on and off, so that you can tell that the sketch is working.
  23. int ledCycle = 5; // This is to set how often the LED will blink on and off. This must be at least 1.
  24. // If an Arduino is left waiting a long time, it is good to have some way to tell if it is still working and not stopped.
  25.  
  26. unsigned long oneSecond_beginTime = 0; // These two variables are for use as a 1-second timer.
  27. unsigned long oneSecond_currentTime = 0;
  28.  
  29.  
  30. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  31.  
  32. // This library folder must be named "dht", not "DHT", because lower-case letters are used in the dht.h and dht.cpp files.
  33. /*livrarias usadas*/
  34. #include "dht.h" // <----------------- lower case letters! (not DHT.h)
  35.  
  36. /*pinos usados sensores*/
  37. #define pinSensorA0 A0
  38. #define pinSensorChuva A1 // ------ this is rain sensor? I think...
  39.  
  40. #define DHTPIN A3
  41. #define ldr_pin_sensor_light A4
  42.  
  43. /*pinos usados Atuadores*/
  44. #define pinSolenoide 4
  45. #define COOLER 3
  46. #define pinMotor 5
  47.  
  48. /*PINOS FIM DE CURSO */
  49. #define sunroof_open_switch   11
  50. #define sunroof_closed_switch 12
  51.  
  52. /*Declaração do Dht e do tipo*/
  53. #define DHTTYPE DHT11
  54. dht DHT;
  55.  
  56. /*Variaveis globais*/
  57. int pino_passo = 5;
  58. int pino_direcao = 4;
  59.  
  60. /*direção do giro*/
  61. int direcao = 1; // This is no longer used, since separate sections are called for opening or closing the sunroof
  62.  
  63. // Volta completa pra formar 1 giro
  64. // Ajuste de acordo com o seu motor
  65. int passos_motor = 700;
  66.  
  67. int sunroof_open_switch_value;
  68. int sunroof_closed_switch_value;
  69.  
  70. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  71.  
  72. // When you write a program, it is helpful to first create all the functions you will need.
  73. // Each function should do only ONE thing.
  74. // It either makes a decision, or it does some task.
  75. // If the function makes the decision, then it should not do any other tasks. The tasks get put into other separate functions.
  76.  
  77. // Like so:
  78. // check_sunroof() ----> this only checks if the sunroof needs to be opened or closed. It does not open or close the sunroof.
  79. // sunroof_open() -----> this only opens the sunroof.
  80. // sunroof_close() ----> this only closes the sunroof.
  81. // Do everything like that.
  82.  
  83. // When you are writing a small program, using lots of functions seems silly.
  84. // But when you begin writing bigger and bigger programs, keeping the code that you are writing organized is the biggest problem you have.
  85. // And putting a lot of lines of code into one single function just causes you problems later on.
  86.  
  87. // You want only related things to happen in a single function: try to keep function size small!
  88. // Functions keep your code organized better, and inside each function you can first put a Serial.print("the-function-name") line to show that the function is being called properly.
  89. // Functions, functions, functions!
  90. // Functions are free! Use as many as you like!
  91. // The functions do not take up more memory than the lines of code inside them.
  92. // ,,,,,,,,
  93. // So first, I made a bunch of functions, and declared them here.
  94. void do_sensor_check();
  95. void read_sensor_data();
  96. void check_sunroof();
  97. void sunroof_open();
  98. void sunroof_close();
  99. void check_cooler();
  100. void turn_cooler_on();
  101. void turn_cooler_off();
  102.  
  103. void check_pinsensorA0(); // I don't know what this part of the code was supposed to do??? But I can still add the functions for it anyway.
  104. // It turns a solenoid on and off, but you don't say what the solenoid does...
  105. void turn_solenoid_on();
  106. void turn_solenoid_off();
  107.  
  108.  
  109. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  110.  
  111. void setup()
  112. {
  113.   pinMode(COOLER, OUTPUT);
  114.   pinMode(pinSolenoide, OUTPUT);
  115.   pinMode(pino_passo, OUTPUT);
  116.   pinMode(pino_direcao, OUTPUT);
  117.   pinMode(sunroof_open_switch, INPUT_PULLUP);
  118.   pinMode(sunroof_closed_switch, INPUT_PULLUP);
  119.   Serial.begin(9600);
  120.   //dht1.begin(); // <---------------- this dht library has no begin() method
  121. }
  122.  
  123. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  124.  
  125.  
  126. void loop() {
  127.  
  128.   // The code below is only for timing one second:
  129.   oneSecond_currentTime = millis();
  130.   if (oneSecond_currentTime >= oneSecond_beginTime) {
  131.     if (oneSecond_currentTime >= (oneSecond_beginTime + 1000)) {
  132.       seconds_counter += 1; // add 1 second, since it has passed.
  133.       oneSecond_beginTime = millis(); // reset the oneSecond_beginTime to the current time.
  134.       // Below is what blinks the LED on and off:
  135.       int x = seconds_counter % ledCycle;
  136.       if (x == 0) {
  137.         digitalWrite(ledBlinkPin, 1);
  138.       }
  139.       else {
  140.         digitalWrite(ledBlinkPin, 0);
  141.       }
  142.     }
  143.   }
  144.   else {
  145.     // This option is to reset the beginTime if the millis() value has rolled over.
  146.     oneSecond_beginTime = millis();
  147.   }
  148.  
  149.   // -----------------------------------------------------------------
  150.  
  151.   // The code below calls do_sensor_check(), if the time interval in seconds has passed:
  152.   if (seconds_counter == sensor_check_cycle) {
  153.     seconds_counter = 0; // reset the seconds counter.
  154.     do_sensor_check(); // This will only check the sensors when the sensor_check_cycle time has passed.
  155.   }
  156.  
  157. } // end of main loop()
  158.  
  159. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  160.  
  161. void do_sensor_check() {
  162.  
  163.   // This code is taken from the example sketch in the DHT11 library.
  164.   // This will report any errors that the DHT11 sensor may send.
  165.   int chk = DHT.read11(DHTPIN);
  166.   switch (chk)
  167.   {
  168.     case DHTLIB_OK:
  169.       Serial.println ("DHT sensor = DHTLIB_OK");
  170.       read_sensor_data(); // This only reads the sensor data if the DHT reports its status as "DHTLIB_OK"
  171.       // The cooler depends on the DHT sensor readings to work, so it should only be called if the DHT sensor status is "OK":
  172.       check_cooler();
  173.       break;
  174.     case DHTLIB_ERROR_CHECKSUM:
  175.       Serial.print("Checksum error,\t");
  176.       break;
  177.     case DHTLIB_ERROR_TIMEOUT:
  178.       Serial.print("Time out error,\t");
  179.       break;
  180.     case DHTLIB_ERROR_CONNECT:
  181.       Serial.print("Connect error,\t");
  182.       break;
  183.     case DHTLIB_ERROR_ACK_L:
  184.       Serial.print("Ack Low error,\t");
  185.       break;
  186.     case DHTLIB_ERROR_ACK_H:
  187.       Serial.print("Ack High error,\t");
  188.       break;
  189.     default:
  190.       Serial.print("Unknown error,\t");
  191.       break;
  192.   }
  193.  
  194.   // The sunroof does not depend on the DHT sensor readings to work, so it can be called here:
  195.   check_sunroof();
  196.   // The function below also does not depend on the DHT sensor reading.
  197.   check_pinsensorA0();
  198. }
  199.  
  200. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  201.  
  202. void read_sensor_data() {
  203.   Serial.println ("reading sensor data");
  204.   // This is where you read the DHT sensor and print out the information from it.
  205.   // That is the ONLY thing that goes in this function.
  206.   // Look at the DHT11 library example dht11_test.ino to see how that is done.
  207.  
  208.   /*declaraçao de variavel*/
  209.   //float t = dht.readTemperature(); <------ this line was incorrect
  210.   float t = DHT.temperature; // <------------------ this is how the dht11_test.ino sketch shows how to do it.
  211.   float ht = analogRead(pinSensorA0);
  212.  
  213.   /*monitoramento Serial*/
  214.   Serial.println("  ");
  215.   Serial.print("TempDHT: ");
  216.   Serial.print(t);
  217.   Serial.println(" *C");
  218.   Serial.print("  Atuador1:");
  219.   Serial.println("     ");
  220.   Serial.print(ht);
  221.   Serial.print("  HUMIDADE:");
  222.   Serial.print("  ");
  223.   delay(500); // delay de 1 seg para atualizaçao de dados
  224.  
  225.   /*Monitoramento da´parte do motor e sensores do motor*/
  226.   Serial.print("  SensorChuva:");
  227.   Serial.print(analogRead(pinSensorChuva));
  228.   Serial.print(" sensor chegada: ");
  229.   Serial.print(analogRead(ldr_pin_sensor_light));
  230.   Serial.print(" ");
  231.   Serial.print("  Atuador:");
  232.   Serial.println(" ");
  233. }
  234.  
  235. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  236.  
  237. void check_sunroof() {
  238.   // The ONLY thing that happens in this function is you are checking if the sunroof needs to be opened or closed.
  239.   Serial.println ("check sunroof");
  240.   if (analogRead(ldr_pin_sensor_light) >= 500 || analogRead(pinSensorChuva)  >  700)
  241.   {
  242.     sunroof_open();
  243.   }
  244.   else
  245.   {
  246.     sunroof_close();
  247.   }
  248.   // This function is done! Don't put anything else here.
  249. }
  250.  
  251. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  252.  
  253. void sunroof_open() {
  254.   Serial.println ("sunroof opening");
  255.   // The ONLY thing that you do in this function is you open the sunroof.
  256.  
  257. }
  258.  
  259. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  260.  
  261. void sunroof_close() {
  262.   Serial.println ("sunroof closing");
  263.   // The ONLY thing that you do in this function is you close the sunroof.
  264.  
  265. }
  266.  
  267. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  268.  
  269. void check_cooler() {
  270.   Serial.println ("checking cooler");
  271.   // The only thing that this function does is check if the cooler must be turned on or off.
  272.  
  273.   // The temperature was obtained in read_sensor_data(), but that vatriable "t" was local to that function and won't work here.
  274.   // So you must obtain the DHT.temperature value again:
  275.   float t = DHT.temperature;
  276.   if (t >= 25.00)
  277.   {
  278.     turn_cooler_on();
  279.   }
  280.   else
  281.   {
  282.     turn_cooler_off();
  283.   }
  284. }
  285.  
  286. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  287.  
  288. void turn_cooler_on() {
  289.   Serial.println("Cooler ON");
  290.   digitalWrite(COOLER, LOW);
  291. }
  292.  
  293. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  294.  
  295. void turn_cooler_off() {
  296.   Serial.println("Cooler OFF");
  297.   digitalWrite(COOLER, HIGH);
  298. }
  299.  
  300. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  301.  
  302. void check_pinsensorA0() {
  303.   /*configuraçao da solenoide*/
  304.   Serial.println("checking pinsensorA0");
  305.   // The ONLY thing this does is check if the solenoid should be turned on or off.
  306.   float ht = analogRead(pinSensorA0);
  307.   if (ht > 600) {
  308.     turn_solenoid_on();
  309.   }
  310.   else {
  311.     turn_solenoid_off();
  312.   }
  313. }
  314.  
  315. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  316.  
  317. void turn_solenoid_on() {
  318.   Serial.println ("Aberta solenoide");
  319.   digitalWrite (pinSolenoide, HIGH);
  320. }
  321.  
  322. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  323.  
  324. void turn_solenoid_off() {
  325.   Serial.println ("Fechadao solenoide");
  326.   digitalWrite (pinSolenoide, LOW);
  327. }
  328.  
  329. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  330.  
  331. // -------- the end ----------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement