Advertisement
jdog1001

Arduino Uno R3 yogurt maker code

Sep 21st, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. const int mech = 2;
  2. const int SSR = 4;
  3.  
  4. unsigned long timer1;
  5. unsigned long timer2;
  6. unsigned long timer3;
  7. unsigned long timer4;
  8.  
  9. float Period = 10000; //ms
  10. float DC = 0.99; //duty cycle
  11. float Tset = 207; //deg F
  12. float Vs = 4970; //supply voltage (mV)
  13. float R1 = 1700; //ohms
  14. float a = 1.47337E-3;
  15. float b = 2.37225E-4;
  16. float c = 1.07080E-7;
  17. float cal = 1.0; //deg F
  18. float Tinf = 71.0; //deg F
  19.  
  20. int error = 0;
  21. int Vout = 0;
  22. int R2 = 0;
  23. int T = 0;
  24. int x = 0;
  25. int y = 0;
  26. int r = 0;
  27. int z = 0;
  28. int s = 0;
  29. int Q = 0;
  30. int P = 0;
  31. int n = 0;
  32. int w = 0;
  33. int d = 0;
  34.  
  35. void setup(){
  36. Serial.begin(9600);
  37. pinMode(SSR,OUTPUT);
  38. pinMode(mech,OUTPUT);
  39. digitalWrite(mech,HIGH);
  40. }
  41.  
  42. void loop(){
  43. x = analogRead(A1); //Thermometer
  44. y = analogRead(A2); //Also Thermometer
  45. z = analogRead(A3); //SSR
  46. r = analogRead(A4); //Boil Switch
  47. s = analogRead(A5); //Also Boil Switch
  48. float Vout = ((x+y)/2)*(4970/1023.0);
  49. float R2 = (Vout*R1)/(Vs-Vout);
  50. float T = 1/(a + b*log(R2) + c*pow(log(R2),3)); //T in deg K
  51. T = 1.8*(T - 273.15) + 32 + cal; //T in deg F
  52. float error = (Tset-T)/(Tset-Tinf) + 0.002*(T-Tinf);
  53.  
  54. if (d==0){
  55. Serial.print(millis()/1000);
  56. Serial.print(" ");
  57. Serial.println(Vout);
  58. timer4 = millis();
  59. d = 1;
  60. }
  61. if (millis()-timer4 > 1000){
  62. d = 0;
  63. }
  64. //delay(5000);
  65.  
  66. if (T < Tinf){
  67. error = 1;
  68. }
  69. else if (error > 1){
  70. error = 1;
  71. }
  72.  
  73. if (r > 50 && s > 50){
  74. Q = 2;
  75. }
  76. if (r < 50 && s < 50){
  77. if (Q == 2){
  78. Q = 0;
  79. }
  80. }
  81. //...................(Heater Duty Cycle)............................................
  82.  
  83. if (Q==2){ //--------------Boil Switch Only
  84. digitalWrite(SSR,HIGH);
  85. }
  86.  
  87. if (Q==1 && millis()-timer1 <= error*Period*DC){ //Period*DC = Max duty cycle
  88. digitalWrite(SSR,HIGH);
  89. }
  90. else if (Q==1 && millis()-timer1 >= Period){
  91. Q = 0;
  92. }
  93. else if (Q==1){
  94. digitalWrite(SSR,LOW);
  95. }
  96.  
  97. if (Q==0 && T >= Tset){
  98. digitalWrite(SSR,LOW);
  99. }
  100. else if (Q==0 && T < Tset){
  101. timer1 = millis();
  102. Q = 1;
  103. }
  104. //......................(Safety: Stuck SSR)................................
  105.  
  106. if (P==0 && z > 50){
  107. timer2 = millis();
  108. P = 1;
  109. }
  110. if (z < 50){
  111. timer2 = millis();
  112. P = 0;
  113. }
  114. if (Q==2){
  115. timer2 = millis();
  116. }
  117.  
  118. if (millis()-timer2 > Period){
  119. digitalWrite(mech,LOW);
  120. digitalWrite(SSR,LOW);
  121. while(true){ //Relays Off and Do Nothing
  122. }
  123. }
  124. //......................(Safety: Failed analogRead of Temp)................
  125.  
  126. if (n = 1 && abs(x-y) > 56){// Difference of about 10 deg F
  127. digitalWrite(mech,LOW);
  128. digitalWrite(SSR,LOW);
  129. while(true){ //Relays Off and Do Nothing
  130. }
  131. }
  132. n = 1; //first reading eradicate
  133. //....................(Safety: Boil Switch Accidentally Left On)................
  134.  
  135. if (w==0 && Q==2){
  136. timer3 = millis();
  137. w = 1;
  138. }
  139. if (Q != 2){
  140. timer3 = millis();
  141. w = 0;
  142. }
  143.  
  144. if (millis()-timer3 > 12600000){ //Boil Switch Left On for about 3.5 hours
  145. digitalWrite(mech,LOW);
  146. digitalWrite(SSR,LOW);
  147. while(true){ //Relays Off and Do Nothing
  148. }
  149. }
  150. //.......................Safety: Thermometer Disconnect.............................
  151.  
  152. if (Vout > 4960){
  153. digitalWrite(mech,LOW);
  154. digitalWrite(SSR,LOW);
  155. while(true){ //Relays Off and Do Nothing
  156. }
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement