Advertisement
James_Slough

Cooler V5

May 16th, 2022
828
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Including libraries
  2. #include <LiquidCrystal.h>
  3. #include <math.h>
  4.  
  5. // Defining pins
  6. const int BAT_LED = 2;  // Pin for the battery LED
  7. const int CLD_LED = 3;  // Pin for the cold LED
  8. const int HOT_LED = 4;  // Pin for the hot LED
  9. const int FAN_PIN = 5;  // Pin for the fan
  10. const int PLT_PIN = 6;  // Pin for peltier module
  11. const int PZO_PIN = 7;  // Pin for the piezo buzzer
  12. const int TMP_PIN = A0; // Pin for the thermistor
  13. const int BAT_VLT = A1; // Real battery voltage is double this
  14. const int POT_PIN = A2; // Pin for the temperature potentiometer
  15. const int BTN_PIN = A3; // Pin for the mode change button
  16. const int LCD_RS = 8,
  17.           LCD_EN = 9,
  18.           LCD_D4 = 10,
  19.           LCD_D5 = 11,
  20.           LCD_D6 = 12,
  21.           LCD_D7 = 13;   // Pins for LCD display
  22.  
  23.  
  24. // Defining constants
  25. const float c1 = 1.009249522e-03,
  26.            c2 = 2.378405444e-04,
  27.            c3 = 2.019202697e-07; // Temperature sensor constants
  28. const float tmp_div_res = 10000;  // Resistance of temperature sensor divider
  29.  
  30. // Defining variables
  31. float tgt_tmp = 35; // Target temperature
  32. float tmp_alg;      // Variable for the analog of the thermistor
  33. float tmp_res;      // Variable for resistance of temperature sensor
  34. float log_tmp_res;  // Variable for log of tmp_res
  35. float tmp_deg;      // Variable for temperature of sensor
  36. float bat_alg;      // Variable for the analog of the battery
  37. float bat_vlt;      // Variable for the voltage of the batteries
  38. int pot_alg;        // Variable for the analog of the potentiometer
  39. bool btn_dwn;       // Bool for if the button was previously depressed
  40. int lcd_state;      // Stores the state of the lcd
  41.  
  42. // Defining LCD
  43. LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
  44.  
  45. // Temperature to output formula
  46. int temp_to_out() {
  47.     int a = 20;           // Sideways curve adjustment
  48.     int h = 255;          // Maximum output
  49.     int m = int(tgt_tmp); // Maximum temperature
  50.     int x = int(tmp_deg); // Current temperature
  51.     float out = -((h*pow(pow(m+a, 2)-pow(x+a,2), 0.5))/(m+a))+h;
  52.  
  53.     if (x < 35) {
  54.         out = 0;
  55.     } else if (x >= 35 && x<=40 && out < h/5) {
  56.         out = h/5;
  57.     } else if (x > m) {
  58.         out = h;
  59.     }
  60.    
  61.     return int(out);
  62. }
  63.  
  64. // Setup function
  65. void setup() {
  66.     // Initializing pins
  67.     pinMode(BAT_LED, OUTPUT);
  68.     pinMode(CLD_LED, OUTPUT);
  69.     pinMode(HOT_LED, OUTPUT);
  70.     pinMode(FAN_PIN, OUTPUT);
  71.     pinMode(PLT_PIN, OUTPUT);
  72.     pinMode(PZO_PIN, OUTPUT);
  73.     pinMode(TMP_PIN, INPUT);
  74.     pinMode(BAT_VLT, INPUT);
  75.     pinMode(POT_PIN, INPUT);
  76.  
  77.     // Initialize LCD
  78.     lcd.begin(16, 2);
  79.  
  80.     // Initialize serial
  81.     Serial.begin(9600);
  82. }
  83.  
  84. // Main loop
  85. void loop() {
  86.     // Running functions
  87.     potentiometer_target(); // Edits: tgt_tmp
  88.     read_temp();    // Output: tmp_deg
  89.     fan_control();
  90.     peltier_control();
  91.     read_battery(); // Output: bat_vlt
  92.     handle_leds();
  93.     read_button();  // Output: lcd_state
  94.     output_lcd();
  95.     handle_piezo();
  96.  
  97.     // User outputs
  98.     Serial.println(lcd_state);
  99. }
  100.  
  101. // Read temperature
  102. void read_temp() {
  103.     tmp_alg = analogRead(TMP_PIN);                                // Get the analog value of the thermistor
  104.     tmp_res = tmp_div_res * (1023.0 / float(tmp_alg) - 1.0);       // Get thermistor resistance
  105.     log_tmp_res = log(tmp_res);                                   // Get the log of the thermistor resistance
  106.     tmp_deg = 1.0/(c1 + c2*log_tmp_res + c3*pow(log_tmp_res, 3)); // Steinhart equation
  107.     tmp_deg -= 273.15;                                            // Kelvin to Celsius
  108. }
  109.  
  110. // Read battery voltage
  111. void read_battery() {
  112.     bat_alg = analogRead(BAT_VLT);           // Read battery analog
  113.     bat_vlt = ((bat_alg / 1023.0f) * 5) * 2; // Get battery voltage
  114. }
  115.  
  116. // Handle LEDs
  117. void handle_leds() {
  118.     // Turn battery LED on if voltage is at least 5V
  119.     if (bat_vlt <= 4.99) { digitalWrite(BAT_LED, LOW); }
  120.     else { digitalWrite(BAT_LED, HIGH); }
  121.  
  122.     // Turn the correct hot and cold LEDs on
  123.     if (tmp_deg > tgt_tmp) {
  124.         digitalWrite(CLD_LED, LOW);
  125.         digitalWrite(HOT_LED, HIGH);
  126.     } else {
  127.         digitalWrite(CLD_LED, HIGH);
  128.         digitalWrite(HOT_LED, LOW);
  129.     }
  130. }
  131.  
  132. // Controls fan
  133. void fan_control() { analogWrite(FAN_PIN, temp_to_out()); }
  134.  
  135. // Controls peltier module
  136. void peltier_control() { analogWrite(FAN_PIN, temp_to_out()); }
  137.  
  138. // Reads the potentiometer and sets target temp accordingly
  139. void potentiometer_target() {
  140.     pot_alg = analogRead(POT_PIN);
  141.     tgt_tmp = map(pot_alg, 0, 1023, 35, 100); // Maps the analog of the potentiometer between 35 and 100
  142. }
  143.  
  144. // Outputs data to the LCD
  145. void output_lcd() {
  146.     if (lcd_state == 0) {
  147.         lcd.setCursor(0, 0);
  148.         lcd.print("Temp (C): ");
  149.         lcd.print(tmp_deg);
  150.  
  151.         lcd.setCursor(0, 1);
  152.         lcd.print("Target (C): ");
  153.         lcd.print(tgt_tmp);
  154.     } else if (lcd_state == 1) {
  155.         lcd.setCursor(0, 0);
  156.         lcd.print("Cooling (%): ");
  157.         lcd.print(temp_to_out()/2.55);
  158.      
  159.         lcd.setCursor(0, 1);
  160.         lcd.print("Made by Slough");
  161.         //lcd.print("Battery (V): ");
  162.         //lcd.print(bat_vlt);
  163.     }
  164. }
  165.  
  166. // Clears the LCD
  167. void clear_lcd() {
  168.     lcd.clear();
  169. }
  170.  
  171. // Handles the piezo buzzer
  172. void handle_piezo() {
  173.     if (tmp_deg > tgt_tmp) { tone(PZO_PIN, 1000); }
  174.     else { noTone(PZO_PIN); }
  175. }
  176.  
  177. // Read mode change button
  178. void read_button() {
  179.     if (analogRead(BTN_PIN) >= 512 && !btn_dwn) {
  180.         if (lcd_state == 0) { lcd_state = 1; btn_dwn = true; clear_lcd(); }
  181.         else if (lcd_state == 1) { lcd_state = 0; btn_dwn = true; }
  182.     } else {
  183.         btn_dwn = false;
  184.     }
  185. }
Advertisement
RAW Paste Data Copied
Advertisement