Guest User

FrostCube 2.0

a guest
Apr 27th, 2016
109
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Encoder.h>
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4. #include <TwoDigitsDisplay.h>
  5. #include <SimplePwmFrequency.h>
  6.  
  7. // Defines --------------------------------------------------------------------
  8.  
  9. #define PIN_DISPLAY_DATA      4
  10. #define PIN_DISPLAY_WRITE_0   5
  11. #define PIN_DISPLAY_WRITE_1   6
  12. #define PIN_DISPLAY_ADDRESS_0 7
  13. #define PIN_DISPLAY_ADDRESS_1 8
  14. #define PIN_DISPLAY_ADDRESS_2 9
  15. #define PIN_PWM_FAN 10
  16. #define PIN_PWM_PELTIER 11
  17. #define PIN_ONE_WIRE_BUS 12
  18. #define PIN_STATUS_LED 13
  19. #define PIN_FEEDBACK_VOLTAGE A0
  20.  
  21. #define TEMPERATURE_PRECISION 12
  22. #define ADDRESS_SENSOR { 0x28, 0xFF, 0x2B, 0x43, 0xA4, 0x15, 0x01, 0x8D }
  23.  
  24. #define FAN_MIN_PERCENTAGE 0.1
  25. #define FAN_MAX_PERCENTAGE 0.5
  26.  
  27. #define ASCII_SYMBOL_DEGREE 176
  28.  
  29. #define PELTIER_RESISTANCE 0.8
  30.  
  31. // Global object instances ----------------------------------------------------
  32.  
  33. OneWire oneWire(PIN_ONE_WIRE_BUS);
  34. DallasTemperature temperatureSensors(&oneWire);
  35. DeviceAddress deviceSensor = ADDRESS_SENSOR;
  36.  
  37. TwoDigitsDisplay frontDisplay(
  38.   PIN_DISPLAY_ADDRESS_0,
  39.   PIN_DISPLAY_ADDRESS_1,
  40.   PIN_DISPLAY_ADDRESS_2,
  41.   PIN_DISPLAY_WRITE_0,
  42.   PIN_DISPLAY_WRITE_1,
  43.   PIN_DISPLAY_DATA
  44.   );
  45.  
  46. Encoder rotaryEncoder(2, 3);
  47.  
  48. // Global variables -----------------------------------------------------------
  49.  
  50. float targetTemperature = 10.0;
  51. float targetTemperatureMargin = 3.0;
  52.  
  53. long long displayTargetTemperatureTime = 0;
  54. long long lastTemperatureRequestTime = 0;
  55.  
  56. // Setup function -------------------------------------------------------------
  57.  
  58. void setup() {
  59.   Serial.begin(9600);
  60.  
  61.   pinMode(PIN_PWM_FAN, OUTPUT);
  62.   pinMode(PIN_PWM_PELTIER, OUTPUT);
  63.   pinMode(PIN_STATUS_LED, OUTPUT);
  64.  
  65.   analogWrite(PIN_PWM_FAN, 60);
  66.   digitalWrite(PIN_PWM_PELTIER, LOW);
  67.  
  68.   setPwmFrequency(PIN_PWM_FAN, 1);
  69.   setPwmFrequency(PIN_PWM_PELTIER, 1);
  70.  
  71.   rotaryEncoder.write(0);
  72.  
  73.   temperatureSensors.begin();
  74.   temperatureSensors.setResolution(deviceSensor, TEMPERATURE_PRECISION);
  75.  
  76.   frontDisplay.setValue(0);
  77. }
  78.  
  79. // Update peltier duty based on current temperature ---------------------------
  80.  
  81. void recalculatePwmDuties(float currentTemperature) {
  82.   float temperatureThresholdLow =  targetTemperature - targetTemperatureMargin;
  83.   float temperatureThresholdHigh = targetTemperature + targetTemperatureMargin;
  84.  
  85.   float temperatureThresholdPercentage = (currentTemperature - temperatureThresholdLow) / (temperatureThresholdHigh - temperatureThresholdLow);
  86.  
  87.   if (temperatureThresholdPercentage < 0)
  88.     temperatureThresholdPercentage = 0;
  89.   else if (temperatureThresholdPercentage > 1)
  90.     temperatureThresholdPercentage = 1;
  91.  
  92.   float peltierDutyPercentage = pow(temperatureThresholdPercentage, 0.7);
  93.   float fanDutyPercentage = FAN_MIN_PERCENTAGE + (FAN_MAX_PERCENTAGE - FAN_MIN_PERCENTAGE) * peltierDutyPercentage;
  94.  
  95.   float feedbackValue = analogRead(PIN_FEEDBACK_VOLTAGE);
  96.   float feedbackVolts = (1.0 - (feedbackValue / 1023)) * 12.0;
  97.   float feedbackAmps = feedbackVolts / PELTIER_RESISTANCE;
  98.  
  99.   analogWrite(PIN_PWM_PELTIER, 255 * peltierDutyPercentage);
  100.   analogWrite(PIN_PWM_FAN, 255 * fanDutyPercentage);
  101.  
  102.   Serial.print("Current: ");
  103.   Serial.print(currentTemperature);
  104.   Serial.write(ASCII_SYMBOL_DEGREE);
  105.   Serial.print("C");
  106.  
  107.   Serial.print(", Target: ");
  108.   Serial.print(targetTemperature);
  109.   Serial.write(ASCII_SYMBOL_DEGREE);
  110.   Serial.print("C");
  111.  
  112.   Serial.print(", Peltier: ");
  113.   Serial.print(100 * peltierDutyPercentage);
  114.   Serial.print("%");
  115.  
  116.   Serial.print(", Fan: ");
  117.   Serial.print(100 * fanDutyPercentage);
  118.   Serial.print("%");
  119.  
  120.   Serial.print(", Volts: ");
  121.   Serial.print(feedbackVolts);
  122.   Serial.print("V");
  123.  
  124.   Serial.print(", Power: ");
  125.   Serial.print(feedbackVolts * feedbackAmps);
  126.   Serial.println("W");
  127. }
  128.  
  129. // Show current or target temperature -----------------------------------------
  130.  
  131. void displayTargetTemperature(float temperature) {
  132.   displayTargetTemperatureTime = millis();
  133.   frontDisplay.setValue(round(targetTemperature));
  134.   digitalWrite(PIN_STATUS_LED, HIGH);
  135. }
  136.  
  137. void displayCurrentTemperature(float temperature) {
  138.   if (displayTargetTemperatureTime == 0 || abs(millis() - displayTargetTemperatureTime) > 2000) {
  139.     frontDisplay.setValue(round(temperature));
  140.     digitalWrite(PIN_STATUS_LED, LOW);
  141.   }
  142. }
  143.  
  144. // Loop function --------------------------------------------------------------
  145.  
  146. void loop() {
  147.   delay(10);
  148.  
  149.   // Poll temperature sensor data -----------------------------------------------
  150.  
  151.   if (lastTemperatureRequestTime == 0 || abs(millis() - lastTemperatureRequestTime) > 2000) {
  152.     lastTemperatureRequestTime = millis();
  153.  
  154.     if (temperatureSensors.isConnected(deviceSensor)) {
  155.       temperatureSensors.requestTemperaturesByAddress(deviceSensor);
  156.       float newTemperature = temperatureSensors.getTempC(deviceSensor);
  157.  
  158.       // Check validity
  159.       if (newTemperature > -20 && newTemperature < 60) {
  160.         recalculatePwmDuties(newTemperature);
  161.         displayCurrentTemperature(newTemperature);
  162.       }
  163.     }
  164.   }
  165.  
  166.   // Poll rotary encoder data ---------------------------------------------------
  167.  
  168.   long encoderDelta = rotaryEncoder.read();
  169.   rotaryEncoder.write(0);
  170.  
  171.   if (encoderDelta != 0) {
  172.     targetTemperature += (1.0 / 4 * encoderDelta); // 4 encoder steps per tick
  173.  
  174.     if (targetTemperature < 0)
  175.       targetTemperature = 0;
  176.  
  177.     if (targetTemperature > 20)
  178.       targetTemperature = 20;
  179.  
  180.     displayTargetTemperature(targetTemperature);
  181.   }
  182. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×