Aquardino

Sistema di controllo dell’acquario basato su arduino

COMPONENTI

  • Arduino Uno
  • HD44780 – display LCD 16×2 
  • I2c Serial Module x display
  • DS18B20 – Sonda di Temperatura Digitale
  • DS3231 – time
  • VBESTLIFE29748130zt – sensore PH
  • IR reciever
/*
 * alessandrobuzzi.com
 * 
 * https://github.com/Arduino-IRremote/Arduino-IRremote/blob/master/examples/SimpleReceiver/SimpleReceiver.ino
 */

//#define DECODE_HASH         // special decoder for all protocols
#define DECODE_NEC
#define IR_RECEIVE_PIN      7

#include <Arduino.h>
#include <IRremote.h>
#include <DS1307RTC.h>         // Library for RTC Module

// Relay
const int SW1 = 8;  // the Arduino pin, which connects to the IN pin of relay
const int SW2 = 9;  // the Arduino pin, which connects to the IN pin of relay
const int SW3 = 10;  // the Arduino pin, which connects to the IN pin of relay
// time night lights
const int OnHour = 18; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 1;
const int OffHour = 22; //SET TIME TO OFF RELAY
const int OffMin = 1;
// time sun lights
const int OnHour1 = 9; //SET TIME TO ON RELAY2 (24 HOUR FORMAT)
const int OnMin1 = 1;
const int OffHour1 = 18; //SET TIME TO OFF RELAY
const int OffMin1 = 1;

/// sensori e LCD
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// inizializzazione della libreria in cui è descritta la modalità di utilizzo dei pin
LiquidCrystal_I2C lcd(0x27, 16, 2); // impostazione dell'indirizzo dell'LCD 0x27 di 16 caratteri e 2 linee
//-----------------------------

// Data wire is plugged into port 4 on the Arduino
#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

DeviceAddress sensor1 = { 0x28, 0x58, 0xA0, 0x96, 0x42, 0x20, 0x1, 0xAB };
DeviceAddress sensor2 = { 0x28, 0x62, 0x62, 0x9, 0x42, 0x20, 0x1, 0x53 };

//PH sensor
#define SensorPin A0            //pH meter Analog output to Arduino Analog Input 0
#define Offset 0.00            //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth  40    //times of collection
int pHArray[ArrayLenth];   //Store the average value of the sensor feedback
int pHArrayIndex=0;
//---

// costanti per timing loop
int Period1, Period2;
unsigned long Time1, Time2;

void setup() {
    Serial.begin(115200);
    //Start the receiver, enable feedback LED and take LED feedback pin from the internal boards definition
    IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);
    Serial.print(F("Ready to receive IR signals at pin "));
    Serial.println(IR_RECEIVE_PIN);
    
    // relays on
    pinMode(SW1, OUTPUT);
    pinMode(SW2, OUTPUT);
    pinMode(SW3, OUTPUT);
    digitalWrite(SW1, HIGH); // Relay is active low, so HIGH will turn it off at startup
    digitalWrite(SW2, HIGH); 
    digitalWrite(SW3, HIGH); 

    //lcd e sensori
    //lcd
    lcd.begin();      // inizializzazione dell'LCD
    lcd.backlight();  // attivazione della retroilluminazione
    sensors.begin();

    //timing loop
    Time1 = Time2 = millis();
    Period1 = 10000;
    Period2 = 2000;
}
// time response IR
int on = 0;
unsigned long last = millis();

void loop() {
    // ricevitore IR
    if (IrReceiver.decode()) {
    if (millis() - last > 1000) { /// time response IR
      
        // Print a short summary of received data
        IrReceiver.printIRResultShort(&Serial);
        if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
            // We have an unknown protocol here, print more info
            IrReceiver.printIRResultRawFormatted(&Serial, true);
        }
        Serial.println();
        
        }// time response
        last = millis();
        //Enable receiving of the next value
        IrReceiver.resume(); // Enable receiving of the next value

        /*
         * Finally, check the received data and perform actions according to the received command
         */
        if (IrReceiver.decodedIRData.command == 0xC) {
            // 1
            digitalWrite(SW1, LOW);
        } else if (IrReceiver.decodedIRData.command == 0x18) {
            // 2
            digitalWrite(SW1, HIGH);
        }else if (IrReceiver.decodedIRData.command == 0x8) {
            // 4
             digitalWrite(SW2, LOW);
        }else if (IrReceiver.decodedIRData.command == 0x1C) {
            // 5
            digitalWrite(SW2, HIGH);
        }else if (IrReceiver.decodedIRData.command == 0x42) {
            // 7
            digitalWrite(SW3, LOW);
        }else if (IrReceiver.decodedIRData.command == 0x52) {
            // 8
            digitalWrite(SW3, HIGH);  // tasto 8
        }else if (IrReceiver.decodedIRData.command == 0x9) {
            // DOWN
            digitalWrite(SW1, HIGH); // All off 
            digitalWrite(SW2, HIGH);
            digitalWrite(SW3, HIGH);
        }else if (IrReceiver.decodedIRData.command == 0x7) {
            // UP
             digitalWrite(SW1, LOW); // All on 
             digitalWrite(SW2, LOW);
             digitalWrite(SW3, LOW);
        }
    }
    
    //timer  
    tmElements_t tm; 
    RTC.read(tm); 
    
      //timerl luci base
      if(tm.Hour == OnHour && tm.Minute == OnMin){
        digitalWrite(SW1,HIGH);
        Serial.println("LIGHT ON");
        }
        
        else if(tm.Hour == OffHour && tm.Minute == OffMin){
          digitalWrite(SW1,LOW);
          Serial.println("LIGHT OFF");
        }
       //timer luce notturna
       if(tm.Hour == OnHour1 && tm.Minute == OnMin1){
        digitalWrite(SW2,HIGH);
        Serial.println("LIGHT ON");
        }
        
        else if(tm.Hour == OffHour1 && tm.Minute == OffMin1){
          digitalWrite(SW2,LOW);
          Serial.println("LIGHT OFF");
        } 
        //Serial.print(tm.Hour);
        //Serial.write(':');
        //Serial.print(tm.Minute);
        //Serial.println();
        //delay(10000);
        
    // define loop time
    if(millis()-Time1>=Period1){       
        //Sensori e LCD
          Serial.print("Requesting temperatures...");
          sensors.requestTemperatures(); // Send the command to get temperatures
          Serial.println("DONE");
          
          Serial.print("Sensor 1(*C): ");
          Serial.print(sensors.getTempC(sensor1)); 
          Serial.print(" Sensor 1(*F): ");
          Serial.println(sensors.getTempF(sensor1)); 
         
          Serial.print("Sensor 2(*C): ");
          Serial.print(sensors.getTempC(sensor2)); 
          Serial.print(" Sensor 2(*F): ");
          Serial.println(sensors.getTempF(sensor2)); 
          //-----//
          lcd.setCursor(0,0);
          lcd.print("T: "); lcd.print(sensors.getTempC(sensor1));
          lcd.setCursor(10,0);
          lcd.print(""); lcd.print(sensors.getTempC(sensor2));
            
        
          
        
        //--PH sensor
          static unsigned long samplingTime = millis();
          static unsigned long printTime = millis();
          static float pHValue,voltage;
          if(millis()-samplingTime > samplingInterval)
          {
              pHArray[pHArrayIndex++]=analogRead(SensorPin);
              if(pHArrayIndex==ArrayLenth)pHArrayIndex=0;
              voltage = avergearray(pHArray, ArrayLenth)*5.0/1024;
              pHValue = 3.5*voltage+Offset;
              samplingTime=millis();
          }
          if(millis() - printTime > printInterval)   //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
          {
            Serial.print("Voltage:");
                Serial.print(voltage,2);
                Serial.print("    pH value: ");
            Serial.println(pHValue,2);
                digitalWrite(LED,digitalRead(LED)^1);
                printTime=millis();
            // LCD display
             lcd.setCursor(0,1);
             lcd.print("PH: "); lcd.print(pHValue,2);
                
          }
  Time1=millis();
  }          
} //END VOID LOOP



// PH sensor function
double avergearray(int* arr, int number){
  int i;
  int max,min;
  double avg;
  long amount=0;
  if(number<=0){
    Serial.println("Error number for the array to avraging!/n");
    return 0;
  }
  if(number<5){   //less than 5, calculated directly statistics
    for(i=0;i<number;i++){
      amount+=arr[i];
    }
    avg = amount/number;
    return avg;
  }else{
    if(arr[0]<arr[1]){
      min = arr[0];max=arr[1];
    }
    else{
      min=arr[1];max=arr[0];
    }
    for(i=2;i<number;i++){
      if(arr[i]<min){
        amount+=min;        //arr<min
        min=arr[i];
      }else {
        if(arr[i]>max){
          amount+=max;    //arr>max
          max=arr[i];
        }else{
          amount+=arr[i]; //min<=arr<=max
        }
      }//if
    }//for
    avg = (double)amount/(number-2);
  }//if
  return avg;

  
}

Comments

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

P