Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Defining pins
- 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 PZO_PIN = 7; // Pin for the piezo buzzer
- const int TMP_PIN = A0; // Pin for the thermistor
- const int POT_PIN = A2; // Pin for the temperature potentiometer
- // 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 = 30; // 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
- int pot_alg; // Variable for the analog of the potentiometer
- // 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 < 30) {//35) {
- out = 0;
- } else if (x >= 35 && x<=40 && out < h/5) {
- out = h/5;
- } else if (x > m) {
- out = h;
- }
- //Serial.println(out);
- return int(out);
- }
- void setup() {
- pinMode(CLD_LED, OUTPUT);
- pinMode(HOT_LED, OUTPUT);
- pinMode(FAN_PIN, OUTPUT);
- pinMode(PZO_PIN, OUTPUT);
- pinMode(TMP_PIN, INPUT);
- pinMode(POT_PIN, INPUT);
- Serial.begin(9600);
- }
- void loop() {
- // Running functions
- potentiometer_target(); // Edits: tgt_tmp
- read_temp(); // Output: tmp_deg
- handle_leds();
- fan_control();
- handle_piezo();
- Serial.print(tmp_deg);
- Serial.print(" / ");
- Serial.println(tgt_tmp);
- }
- // 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
- }
- // Handle LEDs
- void handle_leds() {
- // 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()); }
- // Handles the piezo buzzer
- void handle_piezo() {
- if (tmp_deg > tgt_tmp) { tone(PZO_PIN, 1000); }
- else { noTone(PZO_PIN); }
- }
- // Reads the potentiometer and sets target temp accordingly
- void potentiometer_target() {
- pot_alg = analogRead(POT_PIN);
- tgt_tmp = map(pot_alg, 1023, 0, 30, 100); // Maps the analog of the potentiometer between 35 and 100
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement