utils.ino 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //Read Temp read the current temperature of the hotplate
  2. /*
  3. * Read Current temperature from ADC
  4. * The ADC is reading from a voltage divider
  5. * 5V --- 1k Ohm --- (ADC) --- 100k NTC --- GND
  6. *
  7. * ADC Reading / Temperature (degree C)
  8. * 250 / 25
  9. * 225 / 100
  10. * 155 / 160 (Preheat Temp)
  11. * 123 / 220 (Reflow Temp)
  12. * 100 / 240 (Hard cutoff Temp)
  13. *
  14. * Each reading takes around 100ms
  15. */
  16. int readTemp() {
  17. int numReadings = 10; // Number of readings to average
  18. int totalSensorValue = 0;
  19. for (int i = 0; i < numReadings; ++i) {
  20. totalSensorValue += analogRead(TEMP_PIN);
  21. delay(10); // Optional: add a small delay between readings if needed
  22. }
  23. // Calculate the average sensor value
  24. int averageSensorValue = totalSensorValue / numReadings;
  25. return averageSensorValue;
  26. }
  27. //enterStandbyMode move the hotplate state to standby and cutoff power to hotplate
  28. void enterStandbyMode() {
  29. reflowing = false;
  30. USBSerial_println("Reflow Stopped");
  31. //Set target temperature to room temp and shut down heater
  32. targetTempADC = COOLDOWN_TEMP_ADC;
  33. targetPwrPWM = 0;
  34. //Set yellow LED to slow blinkRed
  35. digitalWrite(LED_REFLOW, LOW);
  36. digitalWrite(LED_PREHEAT, LOW);
  37. blinkYellow = true;
  38. blinkRed = false;
  39. fastblinkRed = false;
  40. //Set reflow stage to standby
  41. reflowStages = 0;
  42. }
  43. //Update heater power state base on temp reading
  44. //The hotter the plate, the smaller the ADC reading
  45. //Return true when the temperature is within target range
  46. bool updateHeaterPowerState() {
  47. int currentADC = readTemp();
  48. if (currentADC <= CUTOFF_TEMP_ADC) {
  49. digitalWrite(HEATER_PIN, LOW);
  50. USBSerial_println("!!! OVERHEAT !!!");
  51. digitalWrite(LED_PREHEAT, HIGH);
  52. digitalWrite(LED_REFLOW, HIGH);
  53. return false;
  54. }
  55. if (currentADC > targetTempADC + offset) {
  56. //Temperature too low. Turn on the heater
  57. analogWrite(HEATER_PIN, targetPwrPWM);
  58. //USBSerial_print("+ ");
  59. //USBSerial_println(currentADC);
  60. return false;
  61. } else if (currentADC < targetTempADC - offset) {
  62. //Temperature too high. Turn off the heater
  63. analogWrite(HEATER_PIN, 0);
  64. //USBSerial_print("- ");
  65. //USBSerial_println(currentADC);
  66. return false;
  67. } else {
  68. //Within range. Keep the current state
  69. return true;
  70. }
  71. }
  72. //Update Key States handle touch button events listening and global state update
  73. void updateKeyStates() {
  74. //Read the current buttons state
  75. updateButtonPressState();
  76. //Start button, require hold down for 3 seconds to start
  77. if (startPressed) {
  78. startCountdown--;
  79. if (startCountdown <= 0 && !reflowing) {
  80. if (stopPressed) {
  81. //Holding two buttons together. Enter desolder mode
  82. USBSerial_println("!!! Desolder Mode Started !!!");
  83. reflowing = true;
  84. startCountdown = 0; //Prevent underflow counter
  85. playStartingLEDBlinks(); //Play fast blink start warning
  86. targetTempADC = REFLOW_TEMP_ADC; //Set the target temp to reflow target temp
  87. targetPwrPWM = REFLOW_PWR_PWM; //Set power rating to 100%
  88. reflowStages = 10; //Set the reflow stage to desolder
  89. //Set red LED to blink fast
  90. blinkYellow = false;
  91. blinkRed = false;
  92. fastblinkRed = true;
  93. } else {
  94. //Holding start button only. Enter automatic reflow mode
  95. /* START REFLOW PROCESS - PREHEAT */
  96. USBSerial_println("!!! Reflow Started !!!");
  97. reflowing = true;
  98. startCountdown = 0; //Prevent it from going underflow
  99. playStartingLEDBlinks(); //Play fast blink start warning
  100. targetTempADC = PREHEAT_START_ADC; //Set the target temp to preheat starting temp
  101. targetPwrPWM = PREHEAT_PWR_PWM; //Set power rating to preheat
  102. reflowStages = 1; //Set the reflow stage to preheating
  103. //Set the blinking LED status to off
  104. blinkYellow = false;
  105. blinkRed = false;
  106. fastblinkRed = false;
  107. digitalWrite(LED_PREHEAT, HIGH); //Light up the preheat LED
  108. }
  109. return;
  110. }
  111. } else {
  112. //Press released
  113. startCountdown = HOLDSTART_TIME;
  114. }
  115. //Stop button, stop immediately
  116. if (stopPressed) {
  117. enterStandbyMode();
  118. }
  119. }