{"id":29400,"date":"2021-04-09T14:19:06","date_gmt":"2021-04-09T12:19:06","guid":{"rendered":"http:\/\/alessandrobuzzi.com\/?p=29400"},"modified":"2021-04-09T14:19:06","modified_gmt":"2021-04-09T12:19:06","slug":"aquardino","status":"publish","type":"post","link":"https:\/\/alessandrobuzzi.com\/?p=29400","title":{"rendered":"Aquardino"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Sistema di controllo dell&#8217;acquario basato su arduino<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">COMPONENTI<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Arduino Uno<\/li><li>HD44780 &#8211; display LCD 16&#215;2&nbsp;<\/li><li>I2c Serial Module x display<\/li><li>DS18B20 &#8211; Sonda di Temperatura Digitale<\/li><li>DS3231 &#8211; time<\/li><li>VBESTLIFE29748130zt &#8211; sensore PH<\/li><li>IR reciever<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"http:\/\/alessandrobuzzi.com\/wp-content\/uploads\/2021\/04\/Aquanrdino-2_bb-1024x727.png\" alt=\"\" class=\"wp-image-29401\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">\/*\n * alessandrobuzzi.com\n * \n * https:\/\/github.com\/Arduino-IRremote\/Arduino-IRremote\/blob\/master\/examples\/SimpleReceiver\/SimpleReceiver.ino\n *\/\n\n\/\/#define DECODE_HASH         \/\/ special decoder for all protocols\n#define DECODE_NEC\n#define IR_RECEIVE_PIN      7\n\n#include &lt;Arduino.h>\n#include &lt;IRremote.h>\n#include &lt;DS1307RTC.h>         \/\/ Library for RTC Module\n\n\/\/ Relay\nconst int SW1 = 8;  \/\/ the Arduino pin, which connects to the IN pin of relay\nconst int SW2 = 9;  \/\/ the Arduino pin, which connects to the IN pin of relay\nconst int SW3 = 10;  \/\/ the Arduino pin, which connects to the IN pin of relay\n\/\/ time night lights\nconst int OnHour = 18; \/\/SET TIME TO ON RELAY (24 HOUR FORMAT)\nconst int OnMin = 1;\nconst int OffHour = 22; \/\/SET TIME TO OFF RELAY\nconst int OffMin = 1;\n\/\/ time sun lights\nconst int OnHour1 = 9; \/\/SET TIME TO ON RELAY2 (24 HOUR FORMAT)\nconst int OnMin1 = 1;\nconst int OffHour1 = 18; \/\/SET TIME TO OFF RELAY\nconst int OffMin1 = 1;\n\n\/\/\/ sensori e LCD\n#include &lt;Wire.h>\n#include &lt;LiquidCrystal_I2C.h>\n#include &lt;OneWire.h>\n#include &lt;DallasTemperature.h>\n\n\/\/ inizializzazione della libreria in cui \u00e8 descritta la modalit\u00e0 di utilizzo dei pin\nLiquidCrystal_I2C lcd(0x27, 16, 2); \/\/ impostazione dell'indirizzo dell'LCD 0x27 di 16 caratteri e 2 linee\n\/\/-----------------------------\n\n\/\/ Data wire is plugged into port 4 on the Arduino\n#define ONE_WIRE_BUS 4\n\/\/ Setup a oneWire instance to communicate with any OneWire devices (not just Maxim\/Dallas temperature ICs)\nOneWire oneWire(ONE_WIRE_BUS);\n\n\/\/ Pass our oneWire reference to Dallas Temperature. \nDallasTemperature sensors(&amp;oneWire);\n\nDeviceAddress sensor1 = { 0x28, 0x58, 0xA0, 0x96, 0x42, 0x20, 0x1, 0xAB };\nDeviceAddress sensor2 = { 0x28, 0x62, 0x62, 0x9, 0x42, 0x20, 0x1, 0x53 };\n\n\/\/PH sensor\n#define SensorPin A0            \/\/pH meter Analog output to Arduino Analog Input 0\n#define Offset 0.00            \/\/deviation compensate\n#define LED 13\n#define samplingInterval 20\n#define printInterval 800\n#define ArrayLenth  40    \/\/times of collection\nint pHArray[ArrayLenth];   \/\/Store the average value of the sensor feedback\nint pHArrayIndex=0;\n\/\/---\n\n\/\/ costanti per timing loop\nint Period1, Period2;\nunsigned long Time1, Time2;\n\nvoid setup() {\n    Serial.begin(115200);\n    \/\/Start the receiver, enable feedback LED and take LED feedback pin from the internal boards definition\n    IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);\n    Serial.print(F(\"Ready to receive IR signals at pin \"));\n    Serial.println(IR_RECEIVE_PIN);\n    \n    \/\/ relays on\n    pinMode(SW1, OUTPUT);\n    pinMode(SW2, OUTPUT);\n    pinMode(SW3, OUTPUT);\n    digitalWrite(SW1, HIGH); \/\/ Relay is active low, so HIGH will turn it off at startup\n    digitalWrite(SW2, HIGH); \n    digitalWrite(SW3, HIGH); \n\n    \/\/lcd e sensori\n    \/\/lcd\n    lcd.begin();      \/\/ inizializzazione dell'LCD\n    lcd.backlight();  \/\/ attivazione della retroilluminazione\n    sensors.begin();\n\n    \/\/timing loop\n    Time1 = Time2 = millis();\n    Period1 = 10000;\n    Period2 = 2000;\n}\n\/\/ time response IR\nint on = 0;\nunsigned long last = millis();\n\nvoid loop() {\n    \/\/ ricevitore IR\n    if (IrReceiver.decode()) {\n    if (millis() - last > 1000) { \/\/\/ time response IR\n      \n        \/\/ Print a short summary of received data\n        IrReceiver.printIRResultShort(&amp;Serial);\n        if (IrReceiver.decodedIRData.protocol == UNKNOWN) {\n            \/\/ We have an unknown protocol here, print more info\n            IrReceiver.printIRResultRawFormatted(&amp;Serial, true);\n        }\n        Serial.println();\n        \n        }\/\/ time response\n        last = millis();\n        \/\/Enable receiving of the next value\n        IrReceiver.resume(); \/\/ Enable receiving of the next value\n\n        \/*\n         * Finally, check the received data and perform actions according to the received command\n         *\/\n        if (IrReceiver.decodedIRData.command == 0xC) {\n            \/\/ 1\n            digitalWrite(SW1, LOW);\n        } else if (IrReceiver.decodedIRData.command == 0x18) {\n            \/\/ 2\n            digitalWrite(SW1, HIGH);\n        }else if (IrReceiver.decodedIRData.command == 0x8) {\n            \/\/ 4\n             digitalWrite(SW2, LOW);\n        }else if (IrReceiver.decodedIRData.command == 0x1C) {\n            \/\/ 5\n            digitalWrite(SW2, HIGH);\n        }else if (IrReceiver.decodedIRData.command == 0x42) {\n            \/\/ 7\n            digitalWrite(SW3, LOW);\n        }else if (IrReceiver.decodedIRData.command == 0x52) {\n            \/\/ 8\n            digitalWrite(SW3, HIGH);  \/\/ tasto 8\n        }else if (IrReceiver.decodedIRData.command == 0x9) {\n            \/\/ DOWN\n            digitalWrite(SW1, HIGH); \/\/ All off \n            digitalWrite(SW2, HIGH);\n            digitalWrite(SW3, HIGH);\n        }else if (IrReceiver.decodedIRData.command == 0x7) {\n            \/\/ UP\n             digitalWrite(SW1, LOW); \/\/ All on \n             digitalWrite(SW2, LOW);\n             digitalWrite(SW3, LOW);\n        }\n    }\n    \n    \/\/timer  \n    tmElements_t tm; \n    RTC.read(tm); \n    \n      \/\/timerl luci base\n      if(tm.Hour == OnHour &amp;&amp; tm.Minute == OnMin){\n        digitalWrite(SW1,HIGH);\n        Serial.println(\"LIGHT ON\");\n        }\n        \n        else if(tm.Hour == OffHour &amp;&amp; tm.Minute == OffMin){\n          digitalWrite(SW1,LOW);\n          Serial.println(\"LIGHT OFF\");\n        }\n       \/\/timer luce notturna\n       if(tm.Hour == OnHour1 &amp;&amp; tm.Minute == OnMin1){\n        digitalWrite(SW2,HIGH);\n        Serial.println(\"LIGHT ON\");\n        }\n        \n        else if(tm.Hour == OffHour1 &amp;&amp; tm.Minute == OffMin1){\n          digitalWrite(SW2,LOW);\n          Serial.println(\"LIGHT OFF\");\n        } \n        \/\/Serial.print(tm.Hour);\n        \/\/Serial.write(':');\n        \/\/Serial.print(tm.Minute);\n        \/\/Serial.println();\n        \/\/delay(10000);\n        \n    \/\/ define loop time\n    if(millis()-Time1>=Period1){       \n        \/\/Sensori e LCD\n          Serial.print(\"Requesting temperatures...\");\n          sensors.requestTemperatures(); \/\/ Send the command to get temperatures\n          Serial.println(\"DONE\");\n          \n          Serial.print(\"Sensor 1(*C): \");\n          Serial.print(sensors.getTempC(sensor1)); \n          Serial.print(\" Sensor 1(*F): \");\n          Serial.println(sensors.getTempF(sensor1)); \n         \n          Serial.print(\"Sensor 2(*C): \");\n          Serial.print(sensors.getTempC(sensor2)); \n          Serial.print(\" Sensor 2(*F): \");\n          Serial.println(sensors.getTempF(sensor2)); \n          \/\/-----\/\/\n          lcd.setCursor(0,0);\n          lcd.print(\"T: \"); lcd.print(sensors.getTempC(sensor1));\n          lcd.setCursor(10,0);\n          lcd.print(\"\"); lcd.print(sensors.getTempC(sensor2));\n            \n        \n          \n        \n        \/\/--PH sensor\n          static unsigned long samplingTime = millis();\n          static unsigned long printTime = millis();\n          static float pHValue,voltage;\n          if(millis()-samplingTime > samplingInterval)\n          {\n              pHArray[pHArrayIndex++]=analogRead(SensorPin);\n              if(pHArrayIndex==ArrayLenth)pHArrayIndex=0;\n              voltage = avergearray(pHArray, ArrayLenth)*5.0\/1024;\n              pHValue = 3.5*voltage+Offset;\n              samplingTime=millis();\n          }\n          if(millis() - printTime > printInterval)   \/\/Every 800 milliseconds, print a numerical, convert the state of the LED indicator\n          {\n            Serial.print(\"Voltage:\");\n                Serial.print(voltage,2);\n                Serial.print(\"    pH value: \");\n            Serial.println(pHValue,2);\n                digitalWrite(LED,digitalRead(LED)^1);\n                printTime=millis();\n            \/\/ LCD display\n             lcd.setCursor(0,1);\n             lcd.print(\"PH: \"); lcd.print(pHValue,2);\n                \n          }\n  Time1=millis();\n  }          \n} \/\/END VOID LOOP\n\n\n\n\/\/ PH sensor function\ndouble avergearray(int* arr, int number){\n  int i;\n  int max,min;\n  double avg;\n  long amount=0;\n  if(number&lt;=0){\n    Serial.println(\"Error number for the array to avraging!\/n\");\n    return 0;\n  }\n  if(number&lt;5){   \/\/less than 5, calculated directly statistics\n    for(i=0;i&lt;number;i++){\n      amount+=arr[i];\n    }\n    avg = amount\/number;\n    return avg;\n  }else{\n    if(arr[0]&lt;arr[1]){\n      min = arr[0];max=arr[1];\n    }\n    else{\n      min=arr[1];max=arr[0];\n    }\n    for(i=2;i&lt;number;i++){\n      if(arr[i]&lt;min){\n        amount+=min;        \/\/arr&lt;min\n        min=arr[i];\n      }else {\n        if(arr[i]>max){\n          amount+=max;    \/\/arr>max\n          max=arr[i];\n        }else{\n          amount+=arr[i]; \/\/min&lt;=arr&lt;=max\n        }\n      }\/\/if\n    }\/\/for\n    avg = (double)amount\/(number-2);\n  }\/\/if\n  return avg;\n\n  \n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sistema di controllo dell&#8217;acquario basato su arduino COMPONENTI Arduino Uno HD44780 &#8211; display LCD 16&#215;2&nbsp; I2c Serial Module x display DS18B20 &#8211; Sonda di Temperatura Digitale DS3231 &#8211; time VBESTLIFE29748130zt &#8211; sensore PH IR reciever<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-29400","post","type-post","status-publish","format-standard","hentry","category-non-categorizzato"],"acf":[],"_links":{"self":[{"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/posts\/29400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29400"}],"version-history":[{"count":0,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/posts\/29400\/revisions"}],"wp:attachment":[{"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}