Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Arduino setup
- /************************************************************************************/
- /* The Arduino software is to control the main regulation of the brewery. */
- /* Software gets all its variables from a Google spreadsheet and are stored in */
- /* different variables in the Arduino main program. */
- /* The main program keeps track of the different brewingsteps and calls other */
- /* code-snippets for executing each step of the brewing process */
- /* */
- /* BREWING STEPS */
- /* 1. Fill HLT tank to a given level. The level is set in the Arduino config */
- /* spreadsheet. */
- /* 2. While filling the HLT tank the snippet also heats the water to a given temp. */
- /* 3. When correct temp is reached, start pumping water into the mash-tun. */
- /* 4. Once the water is all transfered to the mash-tun, start resirculating the */
- /* mash and also fill the HLT tank to new level and heat water. */
- /* Resirculation is controlled by number of minutes in config spreadsheet */
- /* 5. Once initial mashing is complete, transfer mash to boiling kettle and */
- /* start emptying the HLT tank to wash the sparge. */
- /* 6. Start boiling the worth. Once boiling, let it boil for the amount of time */
- /* defined in the config spreadsheet. */
- /* 7. When done boiling, start chilling the worth to temperature given in the */
- /* config spreadsheet. */
- /* 8. Empty boiler. */
- /************************************************************************************/
- /************************************************************************************/
- /* ARDUINO PIN OVERVIEW/ASSIGNMENTS */
- /* */
- /* ANALOG PINS */
- /* HLTTempRead =0 */
- /* HLTLevelRead =1 */
- /* */
- /* DIGITAL PINS */
- /* HLTFillValvePin =2 */
- /* HLTHeater1Pin =3 */
- /* HLTHeater2Pin =4 */
- /* HLTEmptyValvePin =5 */
- /* MashPumpPin =6 */
- // Main program
- // Include files
- //#include "HLTTemp.h"
- //Define constants
- #define j 50
- // Declaration of variables
- // Integers
- int BrewingProcess = 0; // Possible values: 0=Not started, 1=Startup, 2=Heating HLT, 3=Mashing
- int sleep = 10000; // Variable for sleeping for 10 sec
- int MashToBePreheated = 0; //Variable for holding if the mashtun is to be preheated. 0: No preheat. 1:Preheat
- //See PreheatMash() function for more details.
- bool connected;//Is the connection to brygging.bogetvedt.com up and running?
- float TempToWrite = 0.0; //Holds temperature to be written to database
- //HLT tank variables
- int HLTFillValvePin = 2; //Set valve for filling HLT tank to digital out 5.
- int HLTHeater1Pin = 3; //Set the heater for HLT tank to digital out 6.
- int HLTEmptyValvePin = 5; //Set valve for emptying HLT tank to digital out 7.
- int HLTTempRead = 2; //Analog pin for temperature sensor
- int HLTLevelRead = 3; //Analog pin for level sensor
- int HLTFilled = 0; //Holds variable to tell if HLT tank is full or not.
- int HLTTempered = 0; //Holds variable to tell if HLT water is hot enough.
- int HLTEmpty = 0; //Holds variable to tell if HLT tank is emptied.
- int HLTLevelBit=0;
- int i;
- byte kbd;
- //Mash tank variables
- int MashPumpPin = 8; //Sets MashPump to digital 8.
- int MashResirculateValvePin = 9; //Sets valve for resirculating worh to digital 9.
- int MashHeaterPin = 10; //Sets the heater for mash to digital out 10.
- int MashMinutes = 90; //The number of minutes for the mashing process.
- // Floats
- float HLTTemp = 0.0; //Holds each temp measurement
- float HLTTempTot = 0.0; //Holds the sum of serveral measurements
- float HLTTempAvg = 0.0; //Holds the average temperature of serveral readings
- float HLTTempMean = 0.0; //Holds the mean of serveral temperaturemeasurements
- float HLTLevel = 0.0;
- float HLTTempSetpoint=0.0;
- float HLTTempDeviation = 0.0;
- float HLTLevelSetpoint=750.00;
- float HLTLevelEmpty = 125.0;
- // Strings
- // Arrays
- float HLTTempArray[j];
- float HLTTempArray2[j-20]; //Holds temp-array with 20 removed values. Removed values are 10 lowest and 10 highest in HLTTempArray
- // Ethernet declarations
- #include <SPI.h>
- #include <Ethernet.h>
- // the media access control (ethernet hardware) address for the shield:
- // MAC For my ethernet shield BC:B1:F3:4A:B4:EA
- byte mac[] = { 0xBC, 0xB1, 0xF3, 0x4A, 0xB4, 0xEA };
- //the IP address for the shield: 192.168.1.5
- byte ip[] = { 192, 168, 1, 25 };
- // Default gateway
- byte gateway[] = { 192,168,1,1 };
- //What server to connect to?
- byte server[] = { 83, 143, 81, 42 };//
- EthernetClient Client;
- void setup()
- {
- Serial.begin(9600);
- Serial.println("Starting up.....");
- EthernetConnect();
- //Initialize all nessecary ports and variables
- pinMode(HLTFillValvePin, OUTPUT);
- pinMode(HLTHeater1Pin, OUTPUT);
- pinMode(HLTEmptyValvePin, OUTPUT);
- digitalWrite(HLTEmptyValvePin, LOW);
- Ethernet.begin(mac,ip);
- Serial.println(Ethernet.localIP());
- }
- void EthernetConnect()
- {
- Serial.println("Initializing ethernet module");
- Ethernet.begin(mac,ip);
- //Try to connect to brygging.bogetvedt.com
- connected=Client.connect(server, 80);
- if(connected==1)
- {
- Serial.println("CONNECTED");
- //Client.stop();
- Serial.println(Ethernet.localIP());
- }
- return;
- }
- void loop()
- {
- if (BrewingProcess=0);
- {
- digitalWrite(HLTFillValvePin, LOW);
- digitalWrite(HLTHeater1Pin,LOW);
- //Set all variables - This is to be read from a google spreadsheet
- HLTTempSetpoint = 29.0; // To be replaced by a call to GetGoogleData();
- Serial.print(HLTTempSetpoint); Serial.println(" C er satt som setpunkt for temperatur");
- Serial.println("Venter på at bryggeprosessen skal starte");
- BrewingProcess=-1;
- }
- while(BrewingProcess==-1)
- {
- kbd=Serial.read();
- if(kbd==115)
- {
- Serial.println("Brewingprocess initiated.........");
- BrewingProcess=1;
- }
- }
- while(BrewingProcess==1)
- //Starting up the brewery.
- //First connect to brygging.bogetvedt.com to write to database.
- //Then start filling water and start heating.
- {
- if(MashToBePreheated == 1)
- {
- Serial.println("ENTERING PREHEAT MODE!!!!!!");
- PreheatMash();
- }
- Serial.println("1. Bryggeprosess startet. Fyller og varmer vann");
- digitalWrite(HLTFillValvePin, HIGH);
- delay(sleep); //Wait for water to cover heat elements
- digitalWrite(HLTHeater1Pin,HIGH);
- delay(sleep);
- Serial.println("2. Ventil for vann åpnet samt varmeelement på.");
- BrewingProcess=2;
- Serial.print("3. Bryggeprosessvariabel er nå: "); Serial.println(BrewingProcess);
- }
- while(BrewingProcess==2)
- //This mode is for checking temperature&level of HLT water
- {
- //Check if level is OK
- if(HLTLevelBit==0) //Set to 0... Now overriding
- {
- Serial.println("4. Sjekker om tank er full");
- HLTLevelBit=CheckHLTLevel(0); //Call function telling it is in filling mode
- //HLTLevelBit=1; //Tvinge gjennom full tank...
- }
- Serial.print("5. Status for fylling er:"); Serial.println(HLTLevelBit);
- Serial.println("6. Sjekker temperatur i meskevann");
- HLTTempered=CheckHLTTemp();
- TempToWrite=HLTTempered;
- WriteHLTTempToDB(); //Write HLTTempered to database
- Serial.print("7. Status for temperatur er: "); Serial.println(HLTTempered);
- if(HLTLevelBit==1 && HLTTempered==1)
- {
- //Both water and temp OK. Start mashing
- Serial.println("8. Vann ok. Kan starte meskingen.");
- digitalWrite(HLTEmptyValvePin, HIGH);
- BrewingProcess=3;
- }
- }
- while(BrewingProcess==3) //Emptying HLT tank
- {
- //Open valve
- if(HLTEmpty==0 || HLTEmpty==2)
- {
- digitalWrite(MashPumpPin,HIGH);
- HLTEmpty=CheckHLTLevel(1);
- }
- else
- {
- //Tank is empty.
- digitalWrite(MashPumpPin,LOW);
- }
- Serial.println("9. Mesker!!!!");
- delay(sleep);
- }
- }
- /********************************************************************/
- /* FUNCTIONS FOR HLT REGULATIONS ARE LOCATED HERE!!!! */
- /********************************************************************/
- /********************************************************************/
- /* Function for pre-heating the mashtun */
- /********************************************************************/
- void PreheatMash()
- {
- //Defining internal variables for preheat
- float PreheatLevel = 0.0; // How many liters is in the HLT for preheating
- float PreheatVolume = 25.0; //How many liters to preheat?
- float HLTTempLimit = 25.0; //How hot should the preheated water be?
- int Preheating = 0; //Variable for holding the preheating status.
- Serial.println("Preheating the mash equipment."); Serial.println("Filling the preheat liquid.");
- digitalWrite(HLTFillValvePin, HIGH);
- delay(10000);
- Serial.print("Preheating start:");Serial.println(Preheating);
- digitalWrite(HLTHeater1Pin,HIGH);
- while(Preheating<=1)
- {
- Preheating =0;
- PreheatLevel = analogRead(HLTLevelRead);
- Serial.print("PreheatLevel:"); Serial.println(PreheatLevel);
- delay(5000);
- if (PreheatLevel >= PreheatVolume)
- {
- digitalWrite(HLTFillValvePin, LOW);
- Preheating = Preheating+1;
- Serial.println("Preheat volume reached.");
- }
- HLTTempered = CheckHLTTemp();
- Serial.print("HLTTemp:"); Serial.println(HLTTemp);
- delay(5000);
- if (HLTTemp >=HLTTempLimit)
- {
- digitalWrite(HLTHeater1Pin, LOW);
- Preheating = Preheating+1;
- Serial.println("Preheat temperature reached.");
- }
- Serial.print("Preheating midt:"); Serial.println(Preheating);
- }
- while(Preheating ==2) //Waterlevel and temperature is at correct level. Transfer to mash
- {
- Serial.println("Both preheat volume and preheat temperature is reached. Emptying HLT.");
- digitalWrite(HLTEmptyValvePin, HIGH);
- PreheatLevel = analogRead(HLTLevelRead);
- if (PreheatLevel <=HLTLevelEmpty)
- {
- digitalWrite(HLTEmptyValvePin, LOW);
- return;
- }
- }
- Serial.print("Preheating end:");Serial.println(Preheating);
- delay(50000);
- return;
- }
- /********************************************************************/
- /* Function for checking if the level of the HLT tank is reached. */
- /********************************************************************/
- int CheckHLTLevel(int type)
- {
- //RETURNVALUES
- //0: HLT tank is filling.
- //1: HLT Tank is full.
- //2: HLT tank is emptying
- //3: HLT tank has been emptied.
- int returnvalue=0;
- //Getting HLTLevel
- Serial.println("4.1 Leser vannivå");
- HLTLevel=analogRead(HLTLevelRead);
- Serial.print(HLTLevel);
- if(type==0)
- {
- if(HLTLevel>=HLTLevelSetpoint)
- {
- digitalWrite(HLTFillValvePin, LOW); //Turn off valve for filling. Level reached
- returnvalue=1; //Tank is full
- }
- else
- {
- returnvalue=0;
- }
- }
- else if (type == 1) //The process is in emptying HLT mode. Mashing started.
- {
- if(HLTLevel<=HLTLevelEmpty) // The HLT tank is emprty. Close HLT valve and open mash sirculation valve.
- {
- Serial.print("------------------------------------HLT TANK EMPTY----------------------------------------------------");
- returnvalue=3;
- }
- else
- {
- Serial.print("------------------------------------EMPTYING HLT TANK----------------------------------------------------");
- returnvalue=2; //Tank is still emptying
- }
- }
- return(returnvalue);
- }
- /********************************************************************/
- /* Function for checking the temperature level of the HLT tank. */
- /* Turn on&off element */
- /********************************************************************/
- int CheckHLTTemp()
- {
- //RETURNVALUES
- //1. Water hot enough
- //2. Water not hot enough
- int returnvalue=0;
- HLTTempTot=0;
- //Getting the HLTTemp
- Serial.println("6.1 Reading temperature sensor");
- for(i=0;i<j;i++)
- { //Get readings from analog port
- HLTTemp = analogRead(HLTTempRead);
- HLTTemp = ((5.0 * HLTTemp * 100.0)/1024.0);
- HLTTempArray[i]=HLTTemp;
- HLTTempTot = HLTTempTot + HLTTemp;
- delay(250);
- //Serial.print("Reading tempnr."); Serial.print(i); Serial.print(": "); Serial.println(HLTTemp);
- }
- //Calculating average and mean
- HLTTempAvg=HLTTempTot/j;
- //Serial.print("6.1.1 Sittberegning av temp viser: "); Serial.println(HLTTempAvg);
- Serial.println("---------------------------------Sorterer-------------------------------");
- HLTTempMean=bubbleSort();
- //Serial.print("6.1.2 Median av temp viser: "); Serial.println(HLTTempMean);
- HLTTemp=(HLTTempMean + HLTTempAvg)/2;
- Serial.print("6.2 Målt temperatur er: "); Serial.println(HLTTemp);
- Serial.print("6.3 Referansetemperatur er: "); Serial.println(HLTTempSetpoint+HLTTempDeviation);
- delay(sleep);
- if(HLTTemp >=(HLTTempSetpoint+HLTTempDeviation))
- {
- digitalWrite(HLTHeater1Pin, LOW); //Turn off element. Within Setpoint limit
- returnvalue= 1;
- }
- else if (HLTTemp >=(HLTTempSetpoint - HLTTempDeviation))
- {
- digitalWrite(HLTHeater1Pin, LOW); //Turn off element. Within Setpoint limit
- returnvalue=1;
- }
- else
- {
- digitalWrite(HLTHeater1Pin, HIGH); //Make shure the heater is on. Not reached setpoint or gone below.
- returnvalue=0;
- }
- return returnvalue;
- }
- float func_avg()
- {
- float calcavg;
- HLTTempAvg=HLTTempTot/i;
- //print the array
- return calcavg;
- }
- float bubbleSort()
- {
- int out=0;
- int in=0;
- float swapper;
- //Sort the array Temp[]
- for(out=0 ; out < j; out++)
- { // outer loop
- for(in=out; in<j; in++)
- { // inner loop
- //Serial.print("Comparing stringnr: "); Serial.print(in); Serial.print(" with stringnr: "); Serial.println(in+1);
- //Serial.print("Comparing value: "); Serial.print(HLTTempArray[in]); Serial.print(" with value: "); Serial.println(HLTTempArray[in+1]);
- if( (HLTTempArray[in] > HLTTempArray[in+1]) && (in+1<j))
- { // out of order?
- // swap them:
- //Serial.println("Swapping");
- swapper = HLTTempArray[in];
- HLTTempArray[in] = HLTTempArray[in+1];
- HLTTempArray[in+1] = swapper;
- //delay(500);
- }
- }
- }
- //Remove the lowest and highest values to remove most of the noice
- //Serial.print("Liste: ");
- in=0;
- for (out=0; out < j; out++)
- { //Outer loop to scroll trough all values in the array... Remove bottom 10 and top 10
- //Serial.print(out);Serial.print(":");Serial.println(HLTTempArray[out]);
- //delay(1000);
- //in=0;
- if(out>9 && out<(j-10))
- {
- HLTTempArray2[in]=HLTTempArray[out];
- in++;
- }
- }
- //Get median value
- HLTTempMean = HLTTempArray2[(j-20)/2];
- return HLTTempMean;
- }
- void WriteHLTTempToDB()
- {
- //This function will connect to a php script at brygging.bogetvedt.com and log temperature to database bogetve_brygging
- //Create class for Ethernet Client
- Serial.print(".1 Ethernet skriving");
- //EthernetClient Client;
- //connected = Client.connect(server, 80); //Connect to server
- if(connected>0)
- {
- Serial.println("connected");
- Serial.println("Inserting data");
- /*
- //Try to insert data into table Test
- Client.print("GET /Scripts/insert.php?");
- Client.print("Felt1=");
- Client.print(TempToWrite);
- Client.println(" HTTP/1.1");
- Client.println("Host: brygging.bogetvedt.com");
- Client.println();
- Client.stop();
- delay(1000);
- */
- }
- else
- {
- Serial.println("NOT CONNECTED");
- }
- return;
- }
- /********************************************************************/
- /* FUNCTIONS FOR MASH REGULATIONS ARE LOCATED HERE!!!! */
- /********************************************************************/
- /********************************************************************/
- /* This function is for controlling the sirculation in the mash */
- /* and checking of the temperature is ok. */
- /********************************************************************/
- /*
- void SirculateMash()
- {
- int SirculationTime = 90; //total mash sirculation time
- //First check if the temperature is as it should. Call CheckMashTemp()
- MashTemp=CheckMashTemp()
- if(MashTemp=1) //Temperature too low
- {
- digitalWrite(MashHeaterPin,HIGH); //Turn on heatingelement for the mash
- }
- else //Temperature OK or too high
- {
- digitalWrite(MashHeaterPin,LOW); //Turn off heatingelement for the mash
- }
- }
- */
- /********************************************************************/
- /* This function is checking if the mash temperature is ok. */
- /********************************************************************/
- /*
- int CheckMashTemp()
- {
- }
- */
- /********************************************************************/
- /* MISC FUNCTIONS ARE LOCATED HERE!!!! */
- /********************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement