James_Slough

Untitled

Mar 31st, 2022 (edited)
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Name: James Slough
  2. // Date: 01/04/2022
  3. // Purpose: Control a fan and 2 LEDs with a thermistor
  4. // Version: 1
  5.  
  6. // Defining pins
  7. int thermistor_pin = A0; // The pin the thermistor is connected to
  8. int fan_pin = 3;         // The pin the fan is connected to
  9. int hot_led_pin = 4;     // The pin the LED for "device hot" is on
  10. int cool_led_pin = 5;    // The pin the LED for "device cool" is on
  11.  
  12. // Thermistor conversion variables
  13. const float c1 = 1.009249522e-03,
  14.             c2 = 2.378405444e-04,
  15.             c3 = 2.019202697e-07; // Temperature sensor constants
  16. const float tmp_div_res = 10000;  // Resistance of temperature sensor divider
  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 in celsius
  21.  
  22. // Setup function
  23. void setup() {
  24.     // Initializing serial
  25.     Serial.begin(9600);
  26.  
  27.     // Initializing pins
  28.     pinMode(thermistor_pin, INPUT);
  29.     pinMode(fan_pin, OUTPUT);
  30.     pinMode(hot_led_pin, OUTPUT);
  31.     pinMode(cool_led_pin, OUTPUT);
  32. }
  33.  
  34. // Main loop
  35. void loop() {
  36.     // Read the temperature
  37.     read_temperature();
  38.  
  39.     // Display the temperature in serial
  40.     Serial.print("Temperature: ");
  41.     Serial.print(tmp_deg);
  42.     Serial.println(" C");
  43.  
  44.     // Temperature range logic
  45.     if (tmp_deg > 70) { // Highest temperature range
  46.         // Set fan to maximum
  47.         analogWrite(fan_pin, 255);
  48.  
  49.         // Blinking hot LED
  50.         digitalWrite(cool_led_pin, LOW); // Turn the cool LED off
  51.         digitalWrite(hot_led_pin, HIGH); // Turn the hot LED on
  52.         delay(100);                      // 0.1s delay
  53.         digitalWrite(hot_led_pin, LOW);  // Turn the hot LED off
  54.         delay(100);                      // 0.1s delay
  55.     } else if (tmp_deg > 40) { // Middle temperature range
  56.         // Set fan to medium
  57.         analogWrite(fan_pin, 180);
  58.  
  59.         // Hot LED on
  60.         digitalWrite(cool_led_pin, LOW); // Turn the cool LED off
  61.         digitalWrite(hot_led_pin, HIGH); // Turn the hot LED on
  62.     } else if (tmp_deg > 30) { // Lowest temperature range
  63.         // Set fan to low
  64.         analogWrite(fan_pin, 120);
  65.  
  66.         // Blinking cool LED
  67.         digitalWrite(hot_led_pin, LOW);   // Turn the hot LED off
  68.         digitalWrite(cool_led_pin, HIGH); // Turn the cool LED on
  69.         delay(100);                       // 0.1s delay
  70.         digitalWrite(cool_led_pin, LOW);  // Turn the cool LED off
  71.         delay(100);                       // 0.1s delay
  72.     } else { // Lower than minimum temperature range
  73.         // Set fan to off
  74.         analogWrite(fan_pin, 0);
  75.  
  76.         // Cool LED on
  77.         digitalWrite(cool_led_pin, HIGH); // Turn the cool LED on
  78.         digitalWrite(hot_led_pin, LOW);   // Turn the hot LED off
  79.     }
  80. }
  81.  
  82. // Function to read the temperature from the thermistor
  83. void read_temperature() {
  84.     tmp_alg = analogRead(thermistor_pin);                         // Get the analog value of the thermistor
  85.     tmp_res = tmp_div_res * (1023.0 / float(tmp_alg) - 1.0);      // Get thermistor resistance
  86.     log_tmp_res = log(tmp_res);                                   // Get the log of the thermistor resistance
  87.     tmp_deg = 1.0/(c1 + c2*log_tmp_res + c3*pow(log_tmp_res, 3)); // Steinhart equation
  88.     tmp_deg -= 273.15;                                            // Convert Kelvin to Celsius
  89. }
Add Comment
Please, Sign In to add comment