Advertisement
James_Slough

Cooler V4

May 15th, 2022 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Defining pins
  2. const int CLD_LED = 3;  // Pin for the cold LED
  3. const int HOT_LED = 4;  // Pin for the hot LED
  4. const int FAN_PIN = 5;  // Pin for the fan
  5. const int PZO_PIN = 7;  // Pin for the piezo buzzer
  6. const int TMP_PIN = A0; // Pin for the thermistor
  7. const int POT_PIN = A2; // Pin for the temperature potentiometer
  8.  
  9. // Defining constants
  10. const float c1 = 1.009249522e-03,
  11.            c2 = 2.378405444e-04,
  12.            c3 = 2.019202697e-07; // Temperature sensor constants
  13. const float tmp_div_res = 10000;  // Resistance of temperature sensor divider
  14.  
  15. // Defining variables
  16. float tgt_tmp = 30; // Target temperature
  17. float tmp_alg;      // Variable for the analog of the thermistor
  18. float tmp_res;      // Variable for resistance of temperature sensor
  19. float log_tmp_res;  // Variable for log of tmp_res
  20. float tmp_deg;      // Variable for temperature of sensor
  21. int pot_alg;        // Variable for the analog of the potentiometer
  22.  
  23. // Temperature to output formula
  24. int temp_to_out() {
  25.     int a = 20;           // Sideways curve adjustment
  26.     int h = 255;          // Maximum output
  27.     int m = int(tgt_tmp); // Maximum temperature
  28.     int x = int(tmp_deg); // Current temperature
  29.     float out = -((h*pow(pow(m+a, 2)-pow(x+a,2), 0.5))/(m+a))+h;
  30.  
  31.     if (x < 30) {//35) {
  32.         out = 0;
  33.     } else if (x >= 35 && x<=40 && out < h/5) {
  34.         out = h/5;
  35.     } else if (x > m) {
  36.         out = h;
  37.     }
  38.  
  39.     //Serial.println(out);
  40.     return int(out);
  41. }
  42.  
  43. void setup() {
  44.     pinMode(CLD_LED, OUTPUT);
  45.     pinMode(HOT_LED, OUTPUT);
  46.     pinMode(FAN_PIN, OUTPUT);
  47.     pinMode(PZO_PIN, OUTPUT);
  48.     pinMode(TMP_PIN, INPUT);
  49.     pinMode(POT_PIN, INPUT);
  50.  
  51.     Serial.begin(9600);
  52. }
  53.  
  54. void loop() {
  55.     // Running functions
  56.     potentiometer_target(); // Edits: tgt_tmp
  57.     read_temp();    // Output: tmp_deg
  58.     handle_leds();
  59.     fan_control();
  60.     handle_piezo();
  61.  
  62.     Serial.print(tmp_deg);
  63.     Serial.print(" / ");
  64.     Serial.println(tgt_tmp);
  65. }
  66.  
  67. // Read temperature
  68. void read_temp() {
  69.     tmp_alg = analogRead(TMP_PIN);                                // Get the analog value of the thermistor
  70.     tmp_res = tmp_div_res * (1023.0 / float(tmp_alg) - 1.0);       // Get thermistor resistance
  71.     log_tmp_res = log(tmp_res);                                   // Get the log of the thermistor resistance
  72.     tmp_deg = 1.0/(c1 + c2*log_tmp_res + c3*pow(log_tmp_res, 3)); // Steinhart equation
  73.     tmp_deg -= 273.15;                                            // Kelvin to Celsius
  74. }
  75.  
  76. // Handle LEDs
  77. void handle_leds() {
  78.     // Turn the correct hot and cold LEDs on
  79.       if (tmp_deg > tgt_tmp) {
  80.         digitalWrite(CLD_LED, LOW);
  81.         digitalWrite(HOT_LED, HIGH);
  82.     } else {
  83.         digitalWrite(CLD_LED, HIGH);
  84.         digitalWrite(HOT_LED, LOW);
  85.     }
  86. }
  87.  
  88. // Controls fan
  89. void fan_control() { analogWrite(FAN_PIN, temp_to_out()); }
  90.  
  91. // Handles the piezo buzzer
  92. void handle_piezo() {
  93.     if (tmp_deg > tgt_tmp) { tone(PZO_PIN, 1000); }
  94.     else { noTone(PZO_PIN); }    
  95. }
  96.  
  97. // Reads the potentiometer and sets target temp accordingly
  98. void potentiometer_target() {
  99.     pot_alg = analogRead(POT_PIN);
  100.     tgt_tmp = map(pot_alg, 1023, 0, 30, 100); // Maps the analog of the potentiometer between 35 and 100
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement