#include #include #define ONE_WIRE_BUS dD2 #define PWM_HEATER dD5 #define TEMPERATURE_PRECISION 12 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); DeviceAddress outsideThermometer; float targetC = 41.75; void setup(void) { Serial.begin(57600); Serial.println("Dallas Temperature IC Control Library Demo"); sensors.begin(); Serial.print("Locating devices... "); Serial.print("Found "); Serial.print(sensors.getDeviceCount(), DEC); Serial.println(" devices."); if (!sensors.getAddress(outsideThermometer, 0)) Serial.println("Unable to find address for Device 1"); Serial.print("Device 0 Address: "); printAddress(outsideThermometer); Serial.println(); sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION); Serial.print("Device 0 Resolution: "); Serial.print(sensors.getResolution(outsideThermometer), DEC); Serial.println(); pinMode(PWM_HEATER, OUTPUT); } void printAddress(DeviceAddress deviceAddress) { for (uint8_t i = 0; i < 8; i++) { if (deviceAddress[i] < 16) Serial.print("0"); Serial.print(deviceAddress[i], HEX); } } float printTemperature(DeviceAddress deviceAddress) { float tempC = sensors.getTempC(deviceAddress); Serial.print(tempC, 5); return tempC; } void printResolution(DeviceAddress deviceAddress) { Serial.print("Resolution: "); Serial.print(sensors.getResolution(deviceAddress)); Serial.println(); } float printData(DeviceAddress deviceAddress) { float tempC = printTemperature(deviceAddress); return tempC; } float P = 0; float I = 0; float D = 0; int PID(float tempC) { if (tempC > targetC) return 0; else return 255; } void loop(void) { int v; sensors.requestTemperatures(); float tempC = printData(outsideThermometer); Serial.print(" "); Serial.println(10 * (tempC - targetC), 5); v = PID(tempC); analogWrite(PWM_HEATER, v); }