Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Including libraries
- #include <LiquidCrystal.h>
- #include <math.h>
- // Defining pins
- const int BAT_LED = 2; // Pin for the battery LED
- const int CLD_LED = 3; // Pin for the cold LED
- const int HOT_LED = 4; // Pin for the hot LED
- const int FAN_PIN = 5; // Pin for the fan
- const int PLT_PIN = 6; // Pin for peltier module
- const int PZO_PIN = 7; // Pin for the piezo buzzer
- const int TMP_PIN = A0; // Pin for the thermistor
- const int BAT_VLT = A1; // Real battery voltage is double this
- const int POT_PIN = A2; // Pin for the temperature potentiometer
- const int BTN_PIN = A3; // Pin for the mode change button
- const int LCD_RS = 8,
- LCD_EN = 9,
- LCD_D4 = 10,
- LCD_D5 = 11,
- LCD_D6 = 12,
- LCD_D7 = 13; // Pins for LCD display
- // Defining constants
- const float c1 = 1.009249522e-03,
- c2 = 2.378405444e-04,
- c3 = 2.019202697e-07; // Temperature sensor constants
- const float tmp_div_res = 10000; // Resistance of temperature sensor divider
- // Defining variables
- float tgt_tmp = 35; // Target temperature
- float tmp_alg; // Variable for the analog of the thermistor
- float tmp_res; // Variable for resistance of temperature sensor
- float log_tmp_res; // Variable for log of tmp_res
- float tmp_deg; // Variable for temperature of sensor
- float bat_alg; // Variable for the analog of the battery
- float bat_vlt; // Variable for the voltage of the batteries
- int pot_alg; // Variable for the analog of the potentiometer
- bool btn_dwn; // Bool for if the button was previously depressed
- int lcd_state; // Stores the state of the lcd
- // Defining LCD
- LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
- // Temperature to output formula
- int temp_to_out() {
- int a = 20; // Sideways curve adjustment
- int h = 255; // Maximum output
- int m = int(tgt_tmp); // Maximum temperature
- int x = int(tmp_deg); // Current temperature
- float out = -((h*pow(pow(m+a, 2)-pow(x+a,2), 0.5))/(m+a))+h;
- if (x < 35) {
- out = 0;
- } else if (x >= 35 && x<=40 && out < h/5) {
- out = h/5;
- } else if (x > m) {
- out = h;
- }
- return int(out);
- }
- // Setup function
- void setup() {
- // Initializing pins
- pinMode(BAT_LED, OUTPUT);
- pinMode(CLD_LED, OUTPUT);
- pinMode(HOT_LED, OUTPUT);
- pinMode(FAN_PIN, OUTPUT);
- pinMode(PLT_PIN, OUTPUT);
- pinMode(PZO_PIN, OUTPUT);
- pinMode(TMP_PIN, INPUT);
- pinMode(BAT_VLT, INPUT);
- pinMode(POT_PIN, INPUT);
- // Initialize LCD
- lcd.begin(16, 2);
- // Initialize serial
- Serial.begin(9600);
- }
- // Main loop
- void loop() {
- // Running functions
- potentiometer_target(); // Edits: tgt_tmp
- read_temp(); // Output: tmp_deg
- fan_control();
- peltier_control();
- read_battery(); // Output: bat_vlt
- handle_leds();
- read_button(); // Output: lcd_state
- output_lcd();
- handle_piezo();
- // User outputs
- Serial.println(lcd_state);
- }
- // Read temperature
- void read_temp() {
- tmp_alg = analogRead(TMP_PIN); // Get the analog value of the thermistor
- tmp_res = tmp_div_res * (1023.0 / float(tmp_alg) - 1.0); // Get thermistor resistance
- log_tmp_res = log(tmp_res); // Get the log of the thermistor resistance
- tmp_deg = 1.0/(c1 + c2*log_tmp_res + c3*pow(log_tmp_res, 3)); // Steinhart equation
- tmp_deg -= 273.15; // Kelvin to Celsius
- }
- // Read battery voltage
- void read_battery() {
- bat_alg = analogRead(BAT_VLT); // Read battery analog
- bat_vlt = ((bat_alg / 1023.0f) * 5) * 2; // Get battery voltage
- }
- // Handle LEDs
- void handle_leds() {
- // Turn battery LED on if voltage is at least 5V
- if (bat_vlt <= 4.99) { digitalWrite(BAT_LED, LOW); }
- else { digitalWrite(BAT_LED, HIGH); }
- // Turn the correct hot and cold LEDs on
- if (tmp_deg > tgt_tmp) {
- digitalWrite(CLD_LED, LOW);
- digitalWrite(HOT_LED, HIGH);
- } else {
- digitalWrite(CLD_LED, HIGH);
- digitalWrite(HOT_LED, LOW);
- }
- }
- // Controls fan
- void fan_control() { analogWrite(FAN_PIN, temp_to_out()); }
- // Controls peltier module
- void peltier_control() { analogWrite(FAN_PIN, temp_to_out()); }
- // Reads the potentiometer and sets target temp accordingly
- void potentiometer_target() {
- pot_alg = analogRead(POT_PIN);
- tgt_tmp = map(pot_alg, 0, 1023, 35, 100); // Maps the analog of the potentiometer between 35 and 100
- }
- // Outputs data to the LCD
- void output_lcd() {
- if (lcd_state == 0) {
- lcd.setCursor(0, 0);
- lcd.print("Temp (C): ");
- lcd.print(tmp_deg);
- lcd.setCursor(0, 1);
- lcd.print("Target (C): ");
- lcd.print(tgt_tmp);
- } else if (lcd_state == 1) {
- lcd.setCursor(0, 0);
- lcd.print("Cooling (%): ");
- lcd.print(temp_to_out()/2.55);
- lcd.setCursor(0, 1);
- lcd.print("Made by Slough");
- //lcd.print("Battery (V): ");
- //lcd.print(bat_vlt);
- }
- }
- // Clears the LCD
- void clear_lcd() {
- lcd.clear();
- }
- // Handles the piezo buzzer
- void handle_piezo() {
- if (tmp_deg > tgt_tmp) { tone(PZO_PIN, 1000); }
- else { noTone(PZO_PIN); }
- }
- // Read mode change button
- void read_button() {
- if (analogRead(BTN_PIN) >= 512 && !btn_dwn) {
- if (lcd_state == 0) { lcd_state = 1; btn_dwn = true; clear_lcd(); }
- else if (lcd_state == 1) { lcd_state = 0; btn_dwn = true; }
- } else {
- btn_dwn = false;
- }
- }
Advertisement
RAW Paste Data
Copied
Advertisement