Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Ethernet.h>
- #include <EEPROM.h>
- #define PumpPin 2 // Pump relay is connected to Digital pin2
- #define RelayKog 6 // Overkognings ventil relæ.
- #define btn1 176
- #define btn2 402
- #define btn3 766
- #define btn4 950
- #define btn5 1022
- // Ethernet conf start
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- byte ip[] = { 172, 16, 1, 209 };
- byte server[] = { 172, 16, 1, 203 }; // Google
- Client client(server, 80);
- // Ethernet conf stop
- // Temperature calculator start
- double Thermister(int RawADC)
- {
- double Temp;
- Temp = log(((10240000/RawADC) - 10000));
- Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
- Temp = Temp - 273.15; // Convert Kelvin to Celcius
- return Temp;
- }
- // Temperature calculator end
- // Various settings start
- int TankTop = 0; // VVB beholder top temp
- int TankMidt = 0; // VVB beholder midt temp
- int TankBund = 0; // VVB beholder bund temp
- int SolIndF = 0; // Solfanger indgangs temp
- int SolUdF = 0; // Solfanger udgangs temp
- int SolIndT = 0; // VVB beholder sol spiral ind temp
- int SolUdT = 0; // VVB beholder sol spiral ud temp
- int difftemp = 0;
- int kogtemp = 0;
- int plus = 0;
- int minus = 0;
- unsigned long previouswebMillis = 0;
- unsigned long previouslcdMillis = 0;
- unsigned long previouspumpeMillis = 0;
- unsigned long previouskogMillis = 0;
- unsigned long intervalweb = 30000; // Interval between web updating, 3sek
- unsigned long intervallcd = 500; // Interval betwen LCD updates, 0.5sec
- unsigned long intervalpumpe = 31000; // Pumpens kørsel
- unsigned long intervalkog = 6000; // Pumpens kørsel
- // Various settings stop
- void setup()
- {
- pinMode(PumpPin, OUTPUT); // sets the digital pin 2 as output
- pinMode(RelayKog, OUTPUT); // sets the digital pin 2 as output
- pinMode(3, OUTPUT); // s0
- pinMode(4, OUTPUT); // s1
- pinMode(5, OUTPUT); // s2
- Serial.begin(9600); // setup serial
- backlightOn(); // Turn on backlight for Serial LCD
- // Network setup start
- Ethernet.begin(mac, ip);
- // if (client.connect()) {
- // Serial.println("connected");
- // client.println("GET /search?q=arduino HTTP/1.0");
- // client.println();
- // } else {
- // Serial.println("connection failed");
- // }
- // Network setup end
- } // Setup end
- // ADKey button setup
- int pBtn(int input)
- {
- int tolerance = 10; //This is to make up for changing resistance or voltage, this will allow ex. 176 to be down to 166 or up to 186, and still trigger as button 1
- int button = 0;
- if (input <= btn1 + tolerance && input >= btn1 - tolerance) button = 1;
- else if (input <= btn2 + tolerance && input >= btn2 - tolerance) button = 2;
- else if (input <= btn3 + tolerance && input >= btn3 - tolerance) button = 3;
- else if (input <= btn4 + tolerance && input >= btn4 - tolerance) button = 4;
- else if (input <= btn5 + tolerance && input >= btn5 - tolerance) button = 5;
- return button;
- }
- // ADKey button setup end
- void loop()
- {
- /// Udlæs temperaturer fra multiplexer start
- // Read tank top 000
- digitalWrite(3, 0);
- digitalWrite(4, 0);
- digitalWrite(5, 0);
- TankTop = analogRead(0); // read the input pin
- // Read tank midt 001
- digitalWrite(3, 1);
- digitalWrite(4, 0);
- digitalWrite(5, 0);
- TankMidt = analogRead(0); // read the input pin
- //Read tank bund 010
- digitalWrite(3, 0);
- digitalWrite(4, 1);
- digitalWrite(5, 0);
- TankBund = analogRead(0); // read the input pin
- // Read Solfanger indgang 011
- digitalWrite(3, 1);
- digitalWrite(4, 1);
- digitalWrite(5, 0);
- SolIndF = analogRead(0); // read the input pin
- // Read solfanger udgang 100
- digitalWrite(3, 0);
- digitalWrite(4, 0);
- digitalWrite(5, 1);
- SolUdF = analogRead(0); // read the input pin
- // Read water temp ved VVB ind 101
- digitalWrite(3, 1);
- digitalWrite(4, 0);
- digitalWrite(5, 1);
- SolIndT = analogRead(0); // read the input pin
- // Read water temp ved VVB ud 110
- digitalWrite(3, 0);
- digitalWrite(4, 1);
- digitalWrite(5, 1);
- SolUdT = analogRead(0); // read the input pin
- /// Udlæs temperaturer fra multiplexer stop
- // Rediger EEPROM for difference temp start
- difftemp = EEPROM.read(1);
- int btnIn = analogRead(1);
- if(pBtn(btnIn) == 2)
- {
- EEPROM.write(1, difftemp - 1);
- delay(500);
- }else{}
- if(pBtn(btnIn) == 4)
- {
- EEPROM.write(1, difftemp + 1);
- delay(500);
- }else{}
- // Rediger EEPROM for difference temp slut
- // Rediger EEPROM for kog temp start
- kogtemp = EEPROM.read(2);
- if(pBtn(btnIn) == 1)
- {
- EEPROM.write(2, kogtemp - 1);
- delay(100);
- }else{}
- if(pBtn(btnIn) == 3)
- {
- EEPROM.write(2, kogtemp + 1);
- delay(100);
- }else{}
- // Rediger EEPROM for difference temp slut
- // Reager på overkogning start
- if (Thermister(TankBund) >= kogtemp) //
- {
- previouskogMillis = millis();
- }
- // Reager på overophedning slut
- // Tænd/sluk ventil
- if (millis() - previouskogMillis <= intervalkog && previouskogMillis != 0)
- {
- digitalWrite(RelayKog, HIGH);
- } else { digitalWrite(RelayKog, LOW); }
- // Reager på overophedning slut
- // Reager på overophedning start
- if (SolUdF >= 910) // Varmere end 80grader
- {
- previouspumpeMillis = millis();
- }
- // Kør pumpe manuelt.
- if(pBtn(btnIn) == 5)
- {
- previouspumpeMillis = millis();
- }
- // Normal drift start
- //if (SolUdF >= 753) { // Varmere end 50grader
- if (Thermister(SolUdF) >= Thermister(TankBund) + difftemp)
- {
- previouspumpeMillis = millis();
- }
- //}
- // Normal drift stop
- // Tænd/sluk pumpe
- if (millis() - previouspumpeMillis <= intervalpumpe && previouspumpeMillis != 0)
- {
- digitalWrite(PumpPin, HIGH);
- } else { digitalWrite(PumpPin, LOW); }
- // Print on Serial LCD start
- if (millis() - previouslcdMillis >= intervallcd) {
- // save the last time we updated the server
- previouslcdMillis = millis();
- //indstilLCD();
- clearLCD();
- backlightOn();
- //Print text on each line
- //print a character at the end of each line:
- goTo(0);
- delay(10);
- Serial.print("Tank");
- goTo(20);
- Serial.print("T");
- Serial.print(Thermister(TankTop));
- goTo(40);
- Serial.print("M");
- Serial.print(Thermister(TankMidt));
- goTo(60);
- Serial.print("B");
- Serial.print(Thermister(TankBund));
- goTo(7);
- Serial.print("Roer");
- goTo(27);
- Serial.print("I");
- Serial.print(Thermister(SolIndT));
- goTo(47);
- Serial.print("U");
- Serial.print(Thermister(SolUdT));
- goTo(67);
- Serial.print("Dif");
- goTo(70);
- Serial.print(difftemp);
- goTo(14);
- Serial.print("Fanger");
- goTo(34);
- Serial.print("I");
- Serial.print(Thermister(SolIndF));
- goTo(54);
- Serial.print("U");
- Serial.print(Thermister(SolUdF));
- goTo(74);
- Serial.print("Kog");
- Serial.print(kogtemp);
- }
- // Print on Serial LCD stop
- // Netdelen
- if ((millis() - previouswebMillis) >= intervalweb)
- {
- previouswebMillis = millis();
- String msg = "";
- msg = msg + TankTop;
- msg = msg + "|";
- msg = msg + TankMidt;
- msg = msg + "|";
- msg = msg + TankBund;
- msg = msg + "|";
- msg = msg + SolIndT;
- msg = msg + "|";
- msg = msg + SolUdT;
- msg = msg + "|";
- msg = msg + SolIndF;
- msg = msg + "|";
- msg = msg + SolUdF;
- msg = msg + "|";
- msg = msg + digitalRead(PumpPin);
- msg = msg + "|";
- if (client.connect())
- {
- //Serial.print("Updating - ");
- //Serial.println(msg);
- client.print("GET /arduino/solfanger.php?print=");
- client.println(msg);
- client.stop();
- }
- else
- {
- //Serial.print("No connection - ");
- //Serial.println(msg);
- }
- }
- //Netdelen slut
- }// Loop end
- // Serial LCD setup start
- void selectLineOne(){ //puts the cursor at line 0 char 0.
- Serial.print(0xFE, BYTE); //command flag
- Serial.print(128, BYTE); //position
- }
- void selectLineTwo(){ //puts the cursor at line 2 char 0.
- Serial.print(0xFE, BYTE); //command flag
- Serial.print(192, BYTE); //position
- }
- void selectLineThree(){ //puts the cursor at line 3 char 0.
- Serial.print(0xFE, BYTE); //command flag
- Serial.print(148, BYTE); //position
- }
- void selectLineFour(){ //puts the cursor at line 4 char 0.
- Serial.print(0xFE, BYTE); //command flag
- Serial.print(212, BYTE); //position
- }
- void goTo(int position) { //position = line 1: 0-19, line 2: 20-39, etc, 79+ defaults back to 0
- if (position<20){ Serial.print(0xFE, BYTE); //command flag
- Serial.print((position+128), BYTE); //position
- }else if (position<40){Serial.print(0xFE, BYTE); //command flag
- Serial.print((position+128+64-20), BYTE); //position
- }else if (position<60){Serial.print(0xFE, BYTE); //command flag
- Serial.print((position+128+20-40), BYTE); //position
- }else if (position<80){Serial.print(0xFE, BYTE); //command flag
- Serial.print((position+128+84-60), BYTE); //position
- } else { goTo(0); }
- }
- void clearLCD(){
- Serial.print(0xFE, BYTE); //command flag
- Serial.print(0x01, BYTE); //clear command.
- }
- void indstilLCD(){
- Serial.print(0x7C, BYTE); //command flag for backlight stuff
- Serial.print(0x03, BYTE); //clear command.
- }
- void backlightOn(){ //turns on the backlight
- Serial.print(0x7C, BYTE); //command flag for backlight stuff
- Serial.print(157, BYTE); ////128(off), 140(40%), 150(73%), 157(100%) Light level
- }
- void serCommand(){ //a general function to call the command flag for issuing all other commands
- Serial.print(0xFE, BYTE);
- }
- // Serial LCD setup stop
Advertisement
Add Comment
Please, Sign In to add comment