Azuria

Arduino - Solfanger styring 0.5 med Arduino

Apr 21st, 2011
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.48 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <EEPROM.h>
  4.  
  5. #define PumpPin 2 // Pump relay is connected to Digital pin2
  6. #define RelayKog 6 // Overkognings ventil relæ.
  7.  
  8. #define btn1 176
  9. #define btn2 402
  10. #define btn3 766
  11. #define btn4 950
  12. #define btn5 1022
  13.  
  14.  
  15. // Ethernet conf start
  16. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  17. byte ip[] = { 172, 16, 1, 209 };
  18. byte server[] = { 172, 16, 1, 203 }; // Google
  19.  
  20. Client client(server, 80);
  21. // Ethernet conf stop
  22.  
  23. // Temperature calculator start
  24. double Thermister(int RawADC)
  25. {
  26.   double Temp;
  27.   Temp = log(((10240000/RawADC) - 10000));
  28.   Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  29.   Temp = Temp - 273.15;            // Convert Kelvin to Celcius
  30.   return Temp;
  31. }
  32. // Temperature calculator end
  33.  
  34.  
  35. // Various settings start
  36. int TankTop = 0;              // VVB beholder top temp
  37. int TankMidt = 0;             // VVB beholder midt temp
  38. int TankBund = 0;             // VVB beholder bund temp
  39. int SolIndF = 0;              // Solfanger indgangs temp
  40. int SolUdF = 0;               // Solfanger udgangs temp
  41. int SolIndT = 0;              // VVB beholder sol spiral ind temp
  42. int SolUdT = 0;              // VVB beholder sol spiral ud temp
  43. int difftemp = 0;
  44. int kogtemp = 0;
  45. int plus = 0;
  46. int minus = 0;
  47. unsigned long previouswebMillis = 0;
  48. unsigned long previouslcdMillis = 0;
  49. unsigned long previouspumpeMillis = 0;
  50. unsigned long previouskogMillis = 0;
  51. unsigned long intervalweb = 30000;     // Interval between web updating, 3sek
  52. unsigned long intervallcd = 500;      // Interval betwen LCD updates, 0.5sec
  53. unsigned long intervalpumpe = 31000;    // Pumpens kørsel
  54. unsigned long intervalkog = 6000;    // Pumpens kørsel
  55. // Various settings stop
  56.  
  57. void setup()
  58. {
  59.   pinMode(PumpPin, OUTPUT);      // sets the digital pin 2 as output
  60.   pinMode(RelayKog, OUTPUT);      // sets the digital pin 2 as output
  61.   pinMode(3, OUTPUT);    // s0
  62.   pinMode(4, OUTPUT);    // s1
  63.   pinMode(5, OUTPUT);    // s2
  64.   Serial.begin(9600);          //  setup serial
  65.   backlightOn();                 // Turn on backlight for Serial LCD
  66. // Network setup start
  67.   Ethernet.begin(mac, ip);  
  68. //  if (client.connect()) {
  69. //  Serial.println("connected");
  70. //  client.println("GET /search?q=arduino HTTP/1.0");
  71. //  client.println();
  72. //  } else {
  73. //  Serial.println("connection failed");
  74. //  }
  75. // Network setup end
  76. } // Setup end
  77.  
  78. // ADKey button setup
  79. int pBtn(int input)
  80. {
  81.   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
  82.  
  83.   int button = 0;
  84.  
  85.   if (input <= btn1 + tolerance && input >= btn1 - tolerance) button = 1;
  86.   else if (input <= btn2 + tolerance && input >= btn2 - tolerance) button = 2;
  87.   else if (input <= btn3 + tolerance && input >= btn3 - tolerance) button = 3;
  88.   else if (input <= btn4 + tolerance && input >= btn4 - tolerance) button = 4;
  89.   else if (input <= btn5 + tolerance && input >= btn5 - tolerance) button = 5;
  90.  
  91.   return button;
  92. }
  93. // ADKey button setup end
  94.  
  95. void loop()
  96. {
  97. /// Udlæs temperaturer fra multiplexer start
  98. // Read tank top 000
  99. digitalWrite(3, 0);
  100. digitalWrite(4, 0);
  101. digitalWrite(5, 0);
  102. TankTop = analogRead(0);    // read the input pin
  103.  
  104. // Read tank midt 001
  105. digitalWrite(3, 1);
  106. digitalWrite(4, 0);
  107. digitalWrite(5, 0);
  108. TankMidt = analogRead(0);    // read the input pin
  109.  
  110. //Read tank bund 010
  111. digitalWrite(3, 0);
  112. digitalWrite(4, 1);
  113. digitalWrite(5, 0);
  114. TankBund = analogRead(0);    // read the input pin
  115.  
  116. // Read Solfanger indgang 011
  117. digitalWrite(3, 1);
  118. digitalWrite(4, 1);
  119. digitalWrite(5, 0);
  120. SolIndF = analogRead(0);    // read the input pin
  121.  
  122. // Read solfanger udgang 100
  123. digitalWrite(3, 0);
  124. digitalWrite(4, 0);
  125. digitalWrite(5, 1);
  126. SolUdF = analogRead(0);    // read the input pin
  127.  
  128. // Read water temp ved VVB ind 101
  129. digitalWrite(3, 1);
  130. digitalWrite(4, 0);
  131. digitalWrite(5, 1);
  132. SolIndT = analogRead(0);    // read the input pin
  133.  
  134. // Read water temp ved VVB ud 110
  135. digitalWrite(3, 0);
  136. digitalWrite(4, 1);
  137. digitalWrite(5, 1);
  138. SolUdT = analogRead(0);    // read the input pin
  139. /// Udlæs temperaturer fra multiplexer stop
  140.  
  141. // Rediger EEPROM for difference temp start
  142. difftemp = EEPROM.read(1);
  143. int btnIn = analogRead(1);
  144. if(pBtn(btnIn) == 2)
  145. {
  146.   EEPROM.write(1, difftemp - 1);
  147.   delay(500);
  148. }else{}
  149. if(pBtn(btnIn) == 4)
  150. {
  151.   EEPROM.write(1, difftemp + 1);
  152.   delay(500);
  153. }else{}
  154. // Rediger EEPROM for difference temp slut
  155.  
  156. // Rediger EEPROM for kog temp start
  157. kogtemp = EEPROM.read(2);
  158.  
  159. if(pBtn(btnIn) == 1)
  160. {
  161.   EEPROM.write(2, kogtemp - 1);
  162.   delay(100);
  163. }else{}
  164. if(pBtn(btnIn) == 3)
  165. {
  166.   EEPROM.write(2, kogtemp + 1);
  167.   delay(100);
  168. }else{}
  169. // Rediger EEPROM for difference temp slut
  170.  
  171. // Reager på overkogning start
  172. if (Thermister(TankBund) >= kogtemp)  //
  173. {
  174.   previouskogMillis = millis();
  175. }
  176. // Reager på overophedning slut
  177.  
  178. // Tænd/sluk ventil
  179. if (millis() - previouskogMillis <= intervalkog && previouskogMillis != 0)
  180. {
  181.     digitalWrite(RelayKog, HIGH);
  182.   } else { digitalWrite(RelayKog, LOW); }
  183. // Reager på overophedning slut
  184.  
  185.  
  186. // Reager på overophedning start
  187. if (SolUdF >= 910)  // Varmere end 80grader
  188. {
  189.   previouspumpeMillis = millis();
  190. }
  191.  
  192. // Kør pumpe manuelt.
  193. if(pBtn(btnIn) == 5)
  194. {
  195.   previouspumpeMillis = millis();
  196. }
  197.  
  198. // Normal drift start
  199. //if (SolUdF >= 753) { // Varmere end 50grader
  200. if (Thermister(SolUdF) >= Thermister(TankBund) + difftemp)
  201. {
  202.   previouspumpeMillis = millis();
  203. }
  204. //}
  205. // Normal drift stop
  206.  
  207. // Tænd/sluk pumpe
  208. if (millis() - previouspumpeMillis <= intervalpumpe && previouspumpeMillis != 0)
  209. {
  210.     digitalWrite(PumpPin, HIGH);
  211.   } else { digitalWrite(PumpPin, LOW); }
  212.  
  213. // Print on Serial LCD start
  214.   if (millis() - previouslcdMillis >= intervallcd) {
  215.     // save the last time we updated the server
  216.     previouslcdMillis = millis();  
  217. //indstilLCD();
  218. clearLCD();
  219. backlightOn();
  220. //Print text on each line
  221.  
  222.  //print a character at the end of each line:
  223. goTo(0);
  224. delay(10);
  225. Serial.print("Tank");
  226. goTo(20);
  227. Serial.print("T");
  228. Serial.print(Thermister(TankTop));
  229. goTo(40);
  230. Serial.print("M");
  231. Serial.print(Thermister(TankMidt));
  232. goTo(60);
  233. Serial.print("B");
  234. Serial.print(Thermister(TankBund));
  235. goTo(7);
  236. Serial.print("Roer");
  237. goTo(27);
  238. Serial.print("I");
  239. Serial.print(Thermister(SolIndT));
  240. goTo(47);
  241. Serial.print("U");
  242. Serial.print(Thermister(SolUdT));
  243. goTo(67);
  244. Serial.print("Dif");
  245. goTo(70);
  246. Serial.print(difftemp);
  247. goTo(14);
  248. Serial.print("Fanger");
  249. goTo(34);
  250. Serial.print("I");
  251. Serial.print(Thermister(SolIndF));
  252. goTo(54);
  253. Serial.print("U");
  254. Serial.print(Thermister(SolUdF));
  255. goTo(74);
  256. Serial.print("Kog");
  257. Serial.print(kogtemp);
  258. }
  259. // Print on Serial LCD stop
  260.  
  261. // Netdelen
  262.   if ((millis() - previouswebMillis) >= intervalweb)
  263.   {
  264.     previouswebMillis = millis();
  265.     String msg = "";
  266.     msg = msg + TankTop;
  267.     msg = msg + "|";
  268.     msg = msg + TankMidt;
  269.     msg = msg + "|";
  270.     msg = msg + TankBund;
  271.     msg = msg + "|";
  272.     msg = msg + SolIndT;
  273.     msg = msg + "|";
  274.     msg = msg + SolUdT;
  275.     msg = msg + "|";
  276.     msg = msg + SolIndF;
  277.     msg = msg + "|";
  278.     msg = msg + SolUdF;
  279.     msg = msg + "|";
  280.     msg = msg + digitalRead(PumpPin);
  281.     msg = msg + "|";
  282.  
  283.     if (client.connect())
  284.     {
  285.       //Serial.print("Updating - ");
  286.       //Serial.println(msg);
  287.       client.print("GET /arduino/solfanger.php?print=");
  288.       client.println(msg);
  289.       client.stop();
  290.     }
  291.     else
  292.     {
  293.       //Serial.print("No connection - ");
  294.       //Serial.println(msg);
  295.     }
  296. }
  297. //Netdelen slut
  298.  
  299. }// Loop end
  300.  
  301. // Serial LCD setup start
  302. void selectLineOne(){  //puts the cursor at line 0 char 0.
  303.    Serial.print(0xFE, BYTE);   //command flag
  304.    Serial.print(128, BYTE);    //position
  305. }
  306. void selectLineTwo(){  //puts the cursor at line 2 char 0.
  307.    Serial.print(0xFE, BYTE);   //command flag
  308.    Serial.print(192, BYTE);    //position
  309. }
  310. void selectLineThree(){  //puts the cursor at line 3 char 0.
  311.    Serial.print(0xFE, BYTE);   //command flag
  312.    Serial.print(148, BYTE);    //position
  313. }
  314. void selectLineFour(){  //puts the cursor at line 4 char 0.
  315.    Serial.print(0xFE, BYTE);   //command flag
  316.    Serial.print(212, BYTE);    //position
  317. }
  318. void goTo(int position) { //position = line 1: 0-19, line 2: 20-39, etc, 79+ defaults back to 0
  319. if (position<20){ Serial.print(0xFE, BYTE);   //command flag
  320.               Serial.print((position+128), BYTE);    //position
  321. }else if (position<40){Serial.print(0xFE, BYTE);   //command flag
  322.               Serial.print((position+128+64-20), BYTE);    //position
  323. }else if (position<60){Serial.print(0xFE, BYTE);   //command flag
  324.               Serial.print((position+128+20-40), BYTE);    //position
  325. }else if (position<80){Serial.print(0xFE, BYTE);   //command flag
  326.               Serial.print((position+128+84-60), BYTE);    //position              
  327. } else { goTo(0); }
  328. }
  329. void clearLCD(){
  330.    Serial.print(0xFE, BYTE);   //command flag
  331.    Serial.print(0x01, BYTE);   //clear command.
  332. }
  333. void indstilLCD(){
  334.    Serial.print(0x7C, BYTE);   //command flag for backlight stuff
  335.    Serial.print(0x03, BYTE);   //clear command.
  336. }  
  337. void backlightOn(){  //turns on the backlight
  338.     Serial.print(0x7C, BYTE);   //command flag for backlight stuff
  339.     Serial.print(157, BYTE);    ////128(off), 140(40%), 150(73%), 157(100%) Light level
  340.     }
  341. void serCommand(){   //a general function to call the command flag for issuing all other commands  
  342.   Serial.print(0xFE, BYTE);  
  343. }
  344. // Serial LCD setup stop
Advertisement
Add Comment
Please, Sign In to add comment