Fab Academy 2015

Fab Academy

How to Make Almost Anything...


Programming

The program that manages the aquarium is very simple, basically reads the data from the sensors, check if the switches are pressed to turn lights and displays the information from sensors on screen.

Program

	#include  //Import the necesary libreries
	#include 
	#include  
	#include 

	#define Sens_Temp 7 //Pin for tge temperature sensor (data)
	#define Sonda_pH 0 //Pin for the pH probe
	#define boton1 9 //botton 1
	#define boton2 8 //botton 2
	#define led1 9 //led 1
	#define led2 10 //led2

	OneWire ourWire(Sens_Temp); //Sets the pin for communication (OneWire)
	DallasTemperature sensors(&ourWire); //initializes temperature sensor (DallasTemperature)
	LiquidCrystal_I2C lcd(0x27,16,2);  //Set the LCD address to 0x27 for a 16 chars and 2 line display

			 
	unsigned long int avgValue;  //Store the average value of the sensor feedback
	float b;
	int buf[10],temp;

	unsigned long tiempo=millis()+5000; //time variable for screen refresh

	boolean pulsador1_est =0;
	boolean pulsador2_est =0;
	boolean pulsador1;
	boolean pulsador2; 

	void setup()
	{
	  lcd.init();      //Initialize the lcd 
	  sensors.begin(); //Initialize sensors
	  Serial.begin(9600);
	  lcd.backlight();
	  lcd.print(" OPEN AQUARIUM"); //Displays the logo screen
	  //delay(5000);
	  pinMode(led1,OUTPUT);
	  pinMode(led2,OUTPUT);
	}


	void loop()
	{
	  
	  /*TEMPERATURE SENSOR */
	  sensors.requestTemperatures(); 
	 
	  
	  /*pH PROBE */
		for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
	  { 
		buf[i]=analogRead(Sonda_pH);
		delay(10);
	  }
	  for(int i=0;i<9;i++)        //sort the analog from small to large
	  {
		for(int j=i+1;j<10;j++)
		{
		  if(buf[i]>buf[j])
		  {
			temp=buf[i];
			buf[i]=buf[j];
			buf[j]=temp;
		  }
		}
	  }
	  avgValue=0;
	  for(int i=2;i<8;i++)                      //take the average value of 6 center sample
		avgValue+=buf[i];
	  float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
	  phValue=3.5*phValue;                      //convert the millivolt into pH value
		
	  
	  /*LIGHT AND BUTTON*/
	  pulsador1=digitalRead(boton1);
	  pulsador2=digitalRead(boton2);
	  if (pulsador1)
	  {
		pulsador1_est=!pulsador1_est;
		delay(100);
	  }
	  if (pulsador2)
	  {
		pulsador2_est=!pulsador2_est;
		delay(100);
	  }
	  digitalWrite(led1,pulsador1_est);
	  digitalWrite(led2,pulsador2_est);
	 

	 /*DISPLAY THE DATA*/
	  if (tiempomillis())
	  {
		lcd.clear();
		lcd.print("Temperatura: ");
		lcd.print(sensors.getTempCByIndex(0));
		lcd.setCursor(0, 1);
		lcd.print("pH: ");
		lcd.print(phValue);
		tiempo = millis() +5000;
	  }
	}
	

For the program work, it is necessary to use libraries, these libraries can be downloaded below.

Source Files