Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. #define TEMPERATURE_SENSOR A14
  2. #define HEATER A4
  3. #define PH_SENSOR_INPUT A13
  4. #define PH_PUMP_ACID A5
  5. #define PH_PUMP_BASE A3
  6. #define STIRRING_PHOTO_SENSOR A8
  7. #define STIRRER RED_LED
  8. double sum = 0;
  9. boolean Heater = false;
  10.  
  11. void setup() {
  12. Serial.begin(115200);
  13. pinMode(STIRRING_PHOTO_SENSOR, OUTPUT);
  14. pinMode(STIRRER, OUTPUT);
  15. }
  16.  
  17. void loop() {
  18. // put your main code here, to run repeatedly:
  19. double targetTemperature;
  20. double targetpH;
  21. int targetStirringSpeed;
  22. double currentpH = 0;
  23. double currentTemperature = readTemperature();
  24. controlHeater(currentTemperature, targetTemperature);
  25. int count = 0;
  26. for(count = 0; count < 10; count++){
  27. currentpH = readpH(currentTemperature);
  28. sum = sum + currentpH;
  29. }
  30. currentpH = sum / 10;
  31. controlpHPumps(currentpH, targetpH);
  32.  
  33. double currentStirringSpeed = readStirringSpeed();
  34. controlStirrer(currentStirringSpeed, targetStirringSpeed);
  35. }
  36.  
  37. double readTemperature() {
  38. const int Nortemperature = 25;
  39. const double resistance = 10200;
  40. const double B = 4220;
  41.  
  42. double Thermistor = analogRead(TEMPERATURE_SENSOR);
  43. Thermistor = (Thermistor * resistance)/(3.3-Thermistor);
  44. double Reverse_currentTemperature = (log(Thermistor/10000))/B + 1/(Nortemperature);
  45. return 1/Reverse_currentTemperature;
  46. }
  47.  
  48. void controlHeater(double currentTemperature, double targetTemperature) {
  49. if(currentTemperature >= targetTemperature){
  50. digitalWrite(HEATER, LOW);
  51. Heater = false;
  52. }
  53. else {
  54. digitalWrite(HEATER, HIGH);
  55. Heater = true;
  56. }
  57. }
  58.  
  59. double readpH(double currentTemperature) {
  60. float reading = analogRead(PH_SENSOR_INPUT);
  61. reading = reading * 3 / 1023;
  62. reading = (reading - 0.4) / (0.0592);
  63. return reading;
  64. }
  65.  
  66. void controlpHPumps(double currentpH, double targetpH) {
  67. if(currentpH > targetpH){
  68. digitalWrite(PH_PUMP_BASE, LOW);
  69. digitalWrite(PH_PUMP_ACID, HIGH);
  70. }
  71. else if(currentpH == targetpH){
  72. digitalWrite(PH_PUMP_BASE, LOW);
  73. digitalWrite(PH_PUMP_ACID, LOW);
  74. }
  75. else {
  76. digitalWrite(PH_PUMP_ACID, LOW);
  77. digitalWrite(PH_PUMP_BASE, HIGH);
  78. }
  79. }
  80.  
  81. bool lastStirringSensorState = false;
  82. long lastTimeSawLight = millis();
  83. int lastStirringSpeedReading = 0;
  84.  
  85. int readStirringSpeed() {
  86. int readingAnalog = analogRead(STIRRING_PHOTO_SENSOR);
  87.  
  88. bool state = readingAnalog > 600;
  89.  
  90. if (lastStirringSensorState != state) {
  91. if (state) { //We just saw light
  92. long currentTime = millis();
  93. int timeForHalfRev = int(currentTime - lastTimeSawLight);
  94. lastTimeSawLight = currentTime;
  95.  
  96. lastStirringSpeedReading = (60*1000)/(timeForHalfRev*2);
  97. Serial.println(lastStirringSpeedReading);
  98. }
  99. }
  100.  
  101. lastStirringSensorState = state;
  102.  
  103. return lastStirringSpeedReading;
  104. }
  105.  
  106. int currentStirringPWM = 1;
  107. long lastTimeChangedPWM = 0;
  108.  
  109. const long PWMChangeTimeout = 100;
  110.  
  111. void controlStirrer(int currentStirringSpeed, int targetStirringSpeed) {
  112. if (millis() - lastTimeChangedPWM >= PWMChangeTimeout) {
  113. lastTimeChangedPWM = millis();
  114.  
  115. if (targetStirringSpeed >= currentStirringSpeed) {
  116. currentStirringPWM++;
  117. }
  118. else{
  119. currentStirringPWM--;
  120. }
  121. analogWrite(STIRRER, currentStirringPWM);
  122.  
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement