Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Arduino Uno
- Greenhouse sketch v 2.0
- 24 August 2018
- Any sketch that you use a non-standard library in, you should always write where you got the library from.
- This sketch uses the dht library mentioned on this web page:
- https://playground.arduino.cc/Main/DHTLib
- The library files are available at:
- https://github.com/RobTillaart/Arduino/tree/master/libraries/DHTlib
- */
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // These are variables that I have added:
- int sensor_check_cycle = 60; // This is the time interval to check the sensors, in seconds.
- int seconds_counter = 0; // This is used for counting seconds, until it is equal to sensor_check_cycle above.
- int ledBlinkPin = 13; // This sketch blinks the pin-13 LED on and off, so that you can tell that the sketch is working.
- int ledCycle = 5; // This is to set how often the LED will blink on and off. This must be at least 1.
- // 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.
- unsigned long oneSecond_beginTime = 0; // These two variables are for use as a 1-second timer.
- unsigned long oneSecond_currentTime = 0;
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // This library folder must be named "dht", not "DHT", because lower-case letters are used in the dht.h and dht.cpp files.
- /*livrarias usadas*/
- #include "dht.h" // <----------------- lower case letters! (not DHT.h)
- /*pinos usados sensores*/
- #define pinSensorA0 A0
- #define pinSensorChuva A1 // ------ this is rain sensor? I think...
- #define DHTPIN A3
- #define ldr_pin_sensor_light A4
- /*pinos usados Atuadores*/
- #define pinSolenoide 4
- #define COOLER 3
- #define pinMotor 5
- /*PINOS FIM DE CURSO */
- #define sunroof_open_switch 11
- #define sunroof_closed_switch 12
- /*Declaração do Dht e do tipo*/
- #define DHTTYPE DHT11
- dht DHT;
- /*Variaveis globais*/
- int pino_passo = 5;
- int pino_direcao = 4;
- /*direção do giro*/
- int direcao = 1; // This is no longer used, since separate sections are called for opening or closing the sunroof
- // Volta completa pra formar 1 giro
- // Ajuste de acordo com o seu motor
- int passos_motor = 700;
- int sunroof_open_switch_value;
- int sunroof_closed_switch_value;
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // When you write a program, it is helpful to first create all the functions you will need.
- // Each function should do only ONE thing.
- // It either makes a decision, or it does some task.
- // If the function makes the decision, then it should not do any other tasks. The tasks get put into other separate functions.
- // Like so:
- // check_sunroof() ----> this only checks if the sunroof needs to be opened or closed. It does not open or close the sunroof.
- // sunroof_open() -----> this only opens the sunroof.
- // sunroof_close() ----> this only closes the sunroof.
- // Do everything like that.
- // When you are writing a small program, using lots of functions seems silly.
- // But when you begin writing bigger and bigger programs, keeping the code that you are writing organized is the biggest problem you have.
- // And putting a lot of lines of code into one single function just causes you problems later on.
- // You want only related things to happen in a single function: try to keep function size small!
- // 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.
- // Functions, functions, functions!
- // Functions are free! Use as many as you like!
- // The functions do not take up more memory than the lines of code inside them.
- // ,,,,,,,,
- // So first, I made a bunch of functions, and declared them here.
- void do_sensor_check();
- void read_sensor_data();
- void check_sunroof();
- void sunroof_open();
- void sunroof_close();
- void check_cooler();
- void turn_cooler_on();
- void turn_cooler_off();
- 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.
- // It turns a solenoid on and off, but you don't say what the solenoid does...
- void turn_solenoid_on();
- void turn_solenoid_off();
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void setup()
- {
- pinMode(COOLER, OUTPUT);
- pinMode(pinSolenoide, OUTPUT);
- pinMode(pino_passo, OUTPUT);
- pinMode(pino_direcao, OUTPUT);
- pinMode(sunroof_open_switch, INPUT_PULLUP);
- pinMode(sunroof_closed_switch, INPUT_PULLUP);
- Serial.begin(9600);
- //dht1.begin(); // <---------------- this dht library has no begin() method
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void loop() {
- // The code below is only for timing one second:
- oneSecond_currentTime = millis();
- if (oneSecond_currentTime >= oneSecond_beginTime) {
- if (oneSecond_currentTime >= (oneSecond_beginTime + 1000)) {
- seconds_counter += 1; // add 1 second, since it has passed.
- oneSecond_beginTime = millis(); // reset the oneSecond_beginTime to the current time.
- // Below is what blinks the LED on and off:
- int x = seconds_counter % ledCycle;
- if (x == 0) {
- digitalWrite(ledBlinkPin, 1);
- }
- else {
- digitalWrite(ledBlinkPin, 0);
- }
- }
- }
- else {
- // This option is to reset the beginTime if the millis() value has rolled over.
- oneSecond_beginTime = millis();
- }
- // -----------------------------------------------------------------
- // The code below calls do_sensor_check(), if the time interval in seconds has passed:
- if (seconds_counter == sensor_check_cycle) {
- seconds_counter = 0; // reset the seconds counter.
- do_sensor_check(); // This will only check the sensors when the sensor_check_cycle time has passed.
- }
- } // end of main loop()
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void do_sensor_check() {
- // This code is taken from the example sketch in the DHT11 library.
- // This will report any errors that the DHT11 sensor may send.
- int chk = DHT.read11(DHTPIN);
- switch (chk)
- {
- case DHTLIB_OK:
- Serial.println ("DHT sensor = DHTLIB_OK");
- read_sensor_data(); // This only reads the sensor data if the DHT reports its status as "DHTLIB_OK"
- // The cooler depends on the DHT sensor readings to work, so it should only be called if the DHT sensor status is "OK":
- check_cooler();
- break;
- case DHTLIB_ERROR_CHECKSUM:
- Serial.print("Checksum error,\t");
- break;
- case DHTLIB_ERROR_TIMEOUT:
- Serial.print("Time out error,\t");
- break;
- case DHTLIB_ERROR_CONNECT:
- Serial.print("Connect error,\t");
- break;
- case DHTLIB_ERROR_ACK_L:
- Serial.print("Ack Low error,\t");
- break;
- case DHTLIB_ERROR_ACK_H:
- Serial.print("Ack High error,\t");
- break;
- default:
- Serial.print("Unknown error,\t");
- break;
- }
- // The sunroof does not depend on the DHT sensor readings to work, so it can be called here:
- check_sunroof();
- // The function below also does not depend on the DHT sensor reading.
- check_pinsensorA0();
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void read_sensor_data() {
- Serial.println ("reading sensor data");
- // This is where you read the DHT sensor and print out the information from it.
- // That is the ONLY thing that goes in this function.
- // Look at the DHT11 library example dht11_test.ino to see how that is done.
- /*declaraçao de variavel*/
- //float t = dht.readTemperature(); <------ this line was incorrect
- float t = DHT.temperature; // <------------------ this is how the dht11_test.ino sketch shows how to do it.
- float ht = analogRead(pinSensorA0);
- /*monitoramento Serial*/
- Serial.println(" ");
- Serial.print("TempDHT: ");
- Serial.print(t);
- Serial.println(" *C");
- Serial.print(" Atuador1:");
- Serial.println(" ");
- Serial.print(ht);
- Serial.print(" HUMIDADE:");
- Serial.print(" ");
- delay(500); // delay de 1 seg para atualizaçao de dados
- /*Monitoramento da´parte do motor e sensores do motor*/
- Serial.print(" SensorChuva:");
- Serial.print(analogRead(pinSensorChuva));
- Serial.print(" sensor chegada: ");
- Serial.print(analogRead(ldr_pin_sensor_light));
- Serial.print(" ");
- Serial.print(" Atuador:");
- Serial.println(" ");
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void check_sunroof() {
- // The ONLY thing that happens in this function is you are checking if the sunroof needs to be opened or closed.
- Serial.println ("check sunroof");
- if (analogRead(ldr_pin_sensor_light) >= 500 || analogRead(pinSensorChuva) > 700)
- {
- sunroof_open();
- }
- else
- {
- sunroof_close();
- }
- // This function is done! Don't put anything else here.
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void sunroof_open() {
- Serial.println ("sunroof opening");
- // The ONLY thing that you do in this function is you open the sunroof.
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void sunroof_close() {
- Serial.println ("sunroof closing");
- // The ONLY thing that you do in this function is you close the sunroof.
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void check_cooler() {
- Serial.println ("checking cooler");
- // The only thing that this function does is check if the cooler must be turned on or off.
- // The temperature was obtained in read_sensor_data(), but that vatriable "t" was local to that function and won't work here.
- // So you must obtain the DHT.temperature value again:
- float t = DHT.temperature;
- if (t >= 25.00)
- {
- turn_cooler_on();
- }
- else
- {
- turn_cooler_off();
- }
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void turn_cooler_on() {
- Serial.println("Cooler ON");
- digitalWrite(COOLER, LOW);
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void turn_cooler_off() {
- Serial.println("Cooler OFF");
- digitalWrite(COOLER, HIGH);
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void check_pinsensorA0() {
- /*configuraçao da solenoide*/
- Serial.println("checking pinsensorA0");
- // The ONLY thing this does is check if the solenoid should be turned on or off.
- float ht = analogRead(pinSensorA0);
- if (ht > 600) {
- turn_solenoid_on();
- }
- else {
- turn_solenoid_off();
- }
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void turn_solenoid_on() {
- Serial.println ("Aberta solenoide");
- digitalWrite (pinSolenoide, HIGH);
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void turn_solenoid_off() {
- Serial.println ("Fechadao solenoide");
- digitalWrite (pinSolenoide, LOW);
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // -------- the end ----------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement