utils.ino 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. //Update heater power state base on temp reading
  28. //The hotter the plate, the smaller the ADC reading
  29. //Return true when the temperature is within target range
  30. bool updateHeaterPowerState(){
  31. int currentADC = readTemp();
  32. if (currentADC <= CUTOFF_TEMP_ADC){
  33. digitalWrite(HEATER_PIN, LOW);
  34. USBSerial_println("!!! OVERHEAT !!!");
  35. digitalWrite(LED_PREHEAT, HIGH);
  36. digitalWrite(LED_REFLOW, HIGH);
  37. return false;
  38. }
  39. if (currentADC > targetTempADC + offset){
  40. //Temperature too low. Turn on the heater
  41. analogWrite(HEATER_PIN, targetPwrPWM);
  42. //USBSerial_print("+ ");
  43. //USBSerial_println(currentADC);
  44. return false;
  45. }else if (currentADC < targetTempADC - offset){
  46. //Temperature too high. Turn off the heater
  47. analogWrite(HEATER_PIN, 0);
  48. //USBSerial_print("- ");
  49. //USBSerial_println(currentADC);
  50. return false;
  51. }else{
  52. //Within range. Keep the current state
  53. return true;
  54. }
  55. }
  56. //Update Key States handle touch button events listening and global state update
  57. void updateKeyStates(){
  58. TouchKey_Process();
  59. uint8_t touchResult = TouchKey_Get();
  60. //Start button, require hold down for 3 seconds to start
  61. if (touchResult & (1 << 2)) {
  62. startCountdown--;
  63. if (startCountdown <= 0 && !reflowing){
  64. /* START REFLOW PROCESS - PREHEAT */
  65. USBSerial_println("!!! Reflow Started !!!");
  66. reflowing = true;
  67. startCountdown = 0; //Prevent it from going underflow
  68. playStartingLEDBlinks(); //Play fast blink start warning
  69. targetTempADC = PREHEAT_START_ADC; //Set the target temp to preheat starting temp
  70. targetPwrPWM = PREHEAT_PWR_PWM; //Set power rating to preheat
  71. reflowStages = 1; //Set the reflow stage to preheating
  72. //Set the blinking LED status to off
  73. blinkYellow = false;
  74. blinkRed = false;
  75. fastblinkRed = false;
  76. digitalWrite(LED_PREHEAT, HIGH); //Light up the preheat LED
  77. }
  78. } else {
  79. //Touch released
  80. startCountdown = HOLDSTART_TIME;
  81. }
  82. //Stop button, stop immediately
  83. if (touchResult & (1 << 5)) {
  84. reflowing= false;
  85. USBSerial_println("Reflow Stopped");
  86. //Set target temperature to room temp and shut down heater
  87. targetTempADC = COOLDOWN_TEMP_ADC;
  88. targetPwrPWM = 0;
  89. //Set yellow LED to slow blinkRed
  90. digitalWrite(LED_REFLOW, LOW);
  91. digitalWrite(LED_PREHEAT, LOW);
  92. blinkYellow = true;
  93. blinkRed = false;
  94. fastblinkRed = false;
  95. //Set reflow stage to standby
  96. reflowStages = 0;
  97. }
  98. }