Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Thermostat with Real Time Clock Module and web support using second Arduino with ethernet shield.
- This sketch takes temp readings from three temp sensors and a door sensor and decides when to turn the heat on or off.
- steve.a.mccluskey@gmail.com
- This circuit uses a 2.04v analog reference by connecting the 3.3v pin to AREF through a 20k resistor. The internal 32k resistor creates a voltage divider.
- The TMP36 temp sensor puts out 1750mV @ 125 degrees Celsius and 100mV @ -40. Since our voltage tops out at 1.75V, using a lower analog reference voltage
- provides more resolution at the ADC.
- To convert an anlog reading to C, we have to do some math:
- 0-2.04V over 1024 steps equals 1.992mV per step. At -40C, we should see 100mv, so we divide 100mV by 1.992 to get 50. This means at -40C, we
- should see 50 as an analog reading. At 125C the sensor puts out 1750mV, so 1750mV / 1.992 = 878. We then map an analog range of 50 to 878 to a
- Celsius range of -40 to 125: map(analogReading, 50, 878, -40, 125). See revision 2.8 for updated floating point math.
- Revisions:
- V2.2: Web support using second arduino with ethernet shield. Master sends single char commands over serial and thermostat responds accordingly.
- V2.3: Added door and heater state to web page.
- V2.4: Added EEPROM functionality to save settings to resume on startup.
- V2.5: Added code to handle if one of the sensors is unplugged on the web page, 02/18/14.
- V2.6: Rewrote button polling, bottom line display and ReadTimeDate() with switch/cases; consolidated door logic, rewrote average functions. 02/19/14.
- V2.7: Added functionality to display negative temps down to -40. 02/20/14.
- V2.8: Removed averageC(), removed unneccesary lcd.setCursor()'s, fixed average() to correctly return negative temps, changed tempAvgC calculation,
- sensorInterval variable added, C/F mode saved to EEPROM, tempHold limited to 12F minimum, getTemp() analog reading to tempC now uses floating point math and rounding. 03/02/14.
- Changed average() to use floating point math as well. 03/03/14.
- V2.9: Changed how often ReadTimeDate() is called. 03/05/14.
- V3.0: Added daylight savings time option menu and functionality. 02/23/15.
- V3.1: Added doorState variable and only reads door once per loop instead of several times. 02/25/15.
- V3.2: Changed ReadTimeDate() to only initialize one string instead of three. Added heaterDelay variable. 02/27/15.
- V4.0: Added One-Wire Temp sensor support for outdoor temps and getOneWireTemp(). Changed temp variables to temp1, temp2, temp3 from tempNear/Far/Out.
- Fixed closing parenthesis typo in getTemp(). Changed const uint8_t's to #define in pin declarations. Limited data[] to two slots in getOneWireTemp. 03/02/14.
- Pin layout as follows:
- 0 : Hardware serial RX.
- 1 : Hardware serial TX.
- 2 : OneWire bus.
- 3 ~: Green LED.
- 4 :
- 5 ~: Yellow LED.
- 6 ~: Red LED.
- 7 : Door sensor.
- 8 : RTC CS.
- 9 ~: Heater relay.
- 10~:
- 11~: RTC MOSI.
- 12 : RTC MISO.
- 13 : RTC CLK
- A0 : Sensor 1.
- A1 : Sensor 2.
- A2 : Sensor 3.
- A3 :
- A4 : LCD shield.
- A5 : LCD shield.
- Sketch outline:
- Libraries.
- Pin Declarations.
- Global Variables.
- setup().
- loop() {
- Static variables.
- Poll buttons.
- Poll sensors.
- Print avg/hold to first line.
- Print menu to second line.
- Door closed: switch/case for second line.
- Door open: door open.
- Heater on/off logic.
- }
- getTemp().
- getOneWireTemp().
- heaterOn().
- heaterOff().
- clearLine().
- average().
- RTC_init().
- ReadTimeDate().
- serialEvent().
- */
- #include <Wire.h>
- #include <Adafruit_MCP23017.h>
- #include <Adafruit_RGBLCDShield.h>
- #include <SPI.h>
- #include <EEPROM.h>
- #include <OneWire.h>
- #define sensorInterval 150 //interval between temp sensor readings.
- #define heaterDelay 120000 //heater on or off for at least 2 min
- Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
- //pin declarations:
- #define oneWireBus 2 //OneWire bus.
- #define greenLed 3
- #define yellowLed 5
- #define redLed 6
- #define door 7
- #define cs 8
- #define relay 9
- #define tempPin1 0
- #define tempPin2 1
- #define tempPin3 2
- OneWire oneWire(oneWireBus);
- byte addr[8]; //OneWire temp sensor address.
- //global variables:
- boolean celsius = EEPROM.read(5);
- boolean doorLineCleared = false;
- boolean daylightSavings = EEPROM.read(6);
- uint8_t tempHold = EEPROM.read(0); // read saved tempHold value from EEPROM.
- uint8_t tempReduction = EEPROM.read(1);
- uint8_t menu = EEPROM.read(4);
- int8_t temp1, temp2, temp3, tempOneWire, tempAvg; // variables to store temps.
- unsigned long heaterOnTime = 0, heaterOffTime = 0, currentTime = 0, lastReadingTime = 0, lastClockCheck = 0;
- uint8_t heaterState = 0;
- uint8_t doorState = 0;
- uint8_t reading = 0; //columns in matrix.
- uint16_t val[3][10] = { //array to store analog reading values.
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0 ,0, 0, 0, 0, 0}
- }; //end val.
- uint8_t hour;
- uint8_t eveningHour = EEPROM.read(3);
- uint8_t morningHour = EEPROM.read(2);
- void setup() {
- analogReference(EXTERNAL); //AREF = 2.04v.
- pinMode(redLed, OUTPUT);
- pinMode(greenLed, OUTPUT);
- pinMode(yellowLed, OUTPUT);
- pinMode(door, INPUT_PULLUP); //door pin is pulled to ground while door is closed.
- pinMode(relay, OUTPUT);
- pinMode(cs, OUTPUT);
- Serial.begin(19200);
- lcd.begin(16, 2);
- lcd.setCursor(0, 0);
- lcd.print(F("RTC Thermostat"));
- lcd.setCursor(0, 1);
- lcd.print(F("V. 4.0"));
- RTC_init();
- delay(1000);
- lcd.clear();
- oneWire.search(addr); //get connected device's address.
- } //end setup().
- void loop() {
- uint8_t buttons = lcd.readButtons(); //MUST be in main loop. will not work if global.
- if (digitalRead(door) == HIGH) { //only read once per loop.
- doorState = 1;
- }
- else {
- doorState = 0;
- }
- currentTime = millis();
- String timeString;
- int8_t tempAvgC, temp1C, temp2C, temp3C, tempOneWireC;
- float convF = (5.0 * (tempHold - 32)) / 9.0;
- int8_t tempHoldC = round(convF);
- int8_t tempReductionC = tempReduction / 4;
- if (buttons) { //poll the buttons.
- if (buttons & BUTTON_SELECT) {
- celsius = !celsius;
- EEPROM.write(5, celsius); //only write to EEPROM when something is changed to reduce write cycles.
- } //end if.
- else if (buttons & BUTTON_UP) {
- switch (menu) { //update variables depending on which menu is selected.
- case 1:
- tempHold += 2;
- if (tempHold > 80) {
- tempHold = 80;
- } //end if.
- EEPROM.write(0, tempHold);
- break; //end case 1.
- case 2:
- tempReduction += 2;
- if (tempReduction > 12) {
- tempReduction = 12;
- } //end if.
- EEPROM.write(1, tempReduction);
- break; //end case 2.
- case 3:
- eveningHour ++;
- if (eveningHour > 24) {
- eveningHour = 24;
- } //end if.
- EEPROM.write(3, eveningHour);
- break; //end case 3.
- case 4:
- morningHour ++;
- if (morningHour > 11) {
- morningHour = 11;
- } //end if.
- EEPROM.write(2, morningHour);
- break; //end case 4.
- case 5:
- break; //end case 5.
- case 6:
- break; //end case 6.
- case 7:
- daylightSavings = !daylightSavings;
- EEPROM.write(6, daylightSavings);
- break; //end case 7.
- case 8:
- break; //end case 8.
- default:
- break;
- } //end switch.
- } //end if button_up.
- else if (buttons & BUTTON_DOWN) {
- switch (menu) {
- case 1:
- tempHold -= 2;
- if (tempHold < 12) {
- tempHold = 12;
- } //end if.
- EEPROM.write(0, tempHold);
- break; //end case 1.
- case 2:
- tempReduction -= 2;
- if (tempReduction < 0) {
- tempReduction = 0;
- } //end if.
- EEPROM.write(1, tempReduction);
- break; //end case 2.
- case 3:
- eveningHour --;
- if (eveningHour < 16) {
- eveningHour = 16;
- } //end if.
- EEPROM.write(3, eveningHour);
- break; //end case 3.
- case 4:
- morningHour --;
- if (morningHour < 3) {
- morningHour = 3;
- } //end if.
- EEPROM.write(2, morningHour);
- break; //end case 4.
- case 5:
- break; //end case 5.
- case 6:
- break; //end case 6.
- case 7:
- daylightSavings = !daylightSavings;
- EEPROM.write(6, daylightSavings);
- break; //end case 7.
- case 8:
- break;
- default:
- break;
- } //end switch
- } //end if button_down.
- else if (buttons & BUTTON_RIGHT) {
- menu ++;
- clearLine(); //clears 2nd line only when menu number is changed.
- if (menu > 8) {
- menu = 1;
- } //end if.
- EEPROM.write(4, menu);
- } //end if.
- else if (buttons & BUTTON_LEFT) {
- menu --;
- clearLine();
- if (menu < 1) {
- menu = 8;
- } //end if.
- EEPROM.write(4, menu);
- } //end if.
- } // end if buttons.
- if (currentTime - lastReadingTime > sensorInterval) { //only poll sensors every 150ms.
- timeString = ReadTimeDate(2);
- hour = timeString.toInt(); //gets hour of day.
- if (!celsius) {
- temp1 = getTemp(tempPin1); //poll the sensors.
- temp2 = getTemp(tempPin2);
- temp3 = getTemp(tempPin3);
- tempAvg = average(temp1, temp2, temp3);
- tempOneWire = getOneWireTemp();
- } //end if.
- else {
- temp1C = getTemp(tempPin1);
- temp2C = getTemp(tempPin2);
- temp3C = getTemp(tempPin3);
- tempAvgC = average(temp1C, temp2C, temp3C);
- tempOneWireC = getOneWireTemp();
- } //end else.
- lastReadingTime = currentTime;
- reading ++; //next column number in the matrix.
- if (reading > 9) { //reset reading counter every 10 readings.
- reading = 0;
- } //end if.
- } //end if.
- //print avg to screen:
- lcd.setCursor(0, 0);
- lcd.print(F("Avg:"));
- if (!celsius) {
- if (tempAvg >= -40 && tempAvg < 0) { //displays negative temps.
- lcd.print(tempAvg);
- lcd.print(F("F"));
- } //end if.
- else if (tempAvg >= 0) { //displays positive temps and clears last digit.
- lcd.print(tempAvg);
- lcd.print(F("F "));
- } //end else if.
- else {
- lcd.print(F("--F ")); //displays "--" if all 3 sensors are unplugged.
- } //end else.
- } //end if !celsius.
- else {
- if (tempAvgC >= -40 && tempAvgC < 0) {
- lcd.print(tempAvgC);
- lcd.print(F("C"));
- } //end if.
- else if (tempAvgC >= 0) {
- lcd.print(tempAvgC);
- lcd.print(F("C "));
- } //end else if.
- else {
- lcd.print(F("--C "));
- } //end else.
- } //end else.
- //print hold temp to screen:
- lcd.setCursor(8, 0);
- lcd.print(F("Hold:"));
- lcd.setCursor(13, 0);
- if (hour < morningHour || hour >= eveningHour) { //displays night temp hold at night.
- if (!celsius) {
- lcd.print(tempHold - tempReduction);
- lcd.print(F("*")); //star indicates night time temp reduction.
- } //end if.
- else {
- lcd.print(tempHoldC - tempReductionC);
- lcd.print(F("*"));
- } //end else.
- } //end if.
- else if (hour >= morningHour && hour < eveningHour) { //displays regular temp hold during day.
- if (!celsius) {
- lcd.print(tempHold);
- lcd.print(F("F"));
- } //end if.
- else {
- lcd.print(tempHoldC);
- lcd.print(F("C"));
- } //end else.
- } //end else if.
- lcd.setCursor(0, 1);
- lcd.print(F("M")); //displays menu number.
- lcd.print(menu);
- //bottom line display:
- if (doorState == 0) {
- analogWrite(redLed, 0);
- digitalWrite(greenLed, HIGH); //green led indicates system normal.
- if (!doorLineCleared) { //only clears line once when door is shut.
- clearLine();
- doorLineCleared = true;
- } //end if.
- switch (menu) {
- case 1: //displays sensors to screen and allows changing of temp hold.
- lcd.setCursor(2, 1);
- if (!celsius) { //sensor one:
- if (temp1 < -40) { //displays "--" if tempNear returns null and clears leading minus sign.
- lcd.print(F(" --"));
- } //end if.
- else if (temp1 >= -40 && temp1 < 0) { //negative temps.
- lcd.print(temp1);
- } //end else if.
- else {
- lcd.print(F(" ")); //positive temps and clears minus sign.
- lcd.print(temp1);
- } //end else.
- lcd.print(F("F "));
- } //end if.
- else {
- if (temp1C < -40) {
- lcd.print(F(" --"));
- } //end if.
- else if (temp1C >= 40 && temp1C < 0) {
- lcd.print(temp1C);
- } //end else if.
- else {
- lcd.print(F(" "));
- lcd.print(temp1C);
- } // end else.
- lcd.print(F("C "));
- } //end else.
- lcd.setCursor(7, 1);
- if (!celsius) { //sensor 2:
- if (temp2 < -40) {
- lcd.print(F(" --"));
- } //end if.
- else if (temp2 >= -40 && temp2 < 0) {
- lcd.print(temp2);
- } //end else if.
- else {
- lcd.print(F(" "));
- lcd.print(temp2);
- } //end else.
- lcd.print(F("F "));
- } //end if.
- else {
- if (temp2C < -40) {
- lcd.print(F(" --"));
- } //end if.
- else if (temp2C >= 40 && temp2C < 0) {
- lcd.print(temp2C);
- } //end else if.
- else {
- lcd.print(F(" "));
- lcd.print(temp2C);
- } // end else.
- lcd.print(F("C "));
- } //end else.
- lcd.setCursor(12, 1);
- if (!celsius) { //sensor 3:
- if (temp3 < -40) {
- lcd.print(F(" --"));
- } //end if.
- else if (temp3 >= -40 && temp3 < 0) {
- lcd.print(temp3);
- } //end else if.
- else {
- lcd.print(F(" "));
- lcd.print(temp3);
- } //end else.
- lcd.print(F("F "));
- } //end if.
- else {
- if (temp3C < -40) {
- lcd.print(F(" --"));
- } //end if.
- else if (temp3C >= 40 && temp3C < 0) {
- lcd.print(temp3C);
- } //end else if.
- else {
- lcd.print(F(" "));
- lcd.print(temp3C);
- } // end else.
- lcd.print(F("C "));
- } //end else.
- break; //end menu 1.
- case 2: //displays temp drop amount.
- lcd.setCursor(3, 1);
- lcd.print(F("Drop Temp:"));
- if (!celsius) {
- if (tempReduction < 10) {
- lcd.print(F(" "));
- } //end if.
- lcd.print(tempReduction);
- lcd.print(F("F"));
- } //end if.
- else {
- if (tempReductionC < 10) {
- lcd.print(F(" "));
- } //end if.
- lcd.print(tempReductionC);
- lcd.print(F("C"));
- } //end else.
- break; //end menu 2.
- case 3: //displays PM time to drop temp:
- lcd.setCursor(3, 1);
- lcd.print(F("Heat Dn: "));
- if (eveningHour < 22) {
- lcd.print(F(" "));
- } //end if.
- lcd.print(eveningHour - 12);
- lcd.print(F("pm"));
- break; //end menu 3.
- case 4: //displays AM time to raise temp:
- lcd.setCursor(3, 1);
- lcd.print(F("Heat Up: "));
- if (morningHour < 10) {
- lcd.print(F(" "));
- } //end if.
- lcd.print(morningHour);
- lcd.print(F("am"));
- break; //end menu 4.
- case 5: //display time to 2nd row:
- lcd.setCursor(4, 1);
- lcd.print(F("Tm: "));
- if (currentTime - lastClockCheck > sensorInterval) {
- lcd.print(ReadTimeDate(1)); //gets time only.
- lastClockCheck = currentTime;
- } //end if.
- break; //end menu 5.
- case 6: //display date to 2nd row:
- lcd.setCursor(4, 1);
- lcd.print(F("Dt: "));
- if (currentTime - lastClockCheck > sensorInterval) {
- lcd.print(ReadTimeDate(0)); //gets date only.
- lastClockCheck = currentTime;
- } //end if.
- break; //end menu 6.
- case 7: //daylight savings time enable/disable:
- lcd.setCursor(4, 1);
- lcd.print(F("DST:"));
- if (daylightSavings) {
- lcd.print(F("Active "));
- } //end if.
- else {
- lcd.print(F("Inactive"));
- } //end else.
- break; //end menu 7.
- case 8: //prints outside temp to screen.
- lcd.setCursor(4, 1);
- lcd.print(F("Out: "));
- if (!celsius) {
- if (tempOneWire > -51) {
- lcd.print(tempOneWire);
- } //end if.
- else {
- lcd.print("--");
- } //end else.
- lcd.print(F("F "));
- } //end if celsius.
- else {
- if (tempOneWireC > -51) {
- lcd.print(tempOneWireC);
- } //end if.
- else {
- lcd.print("--");
- } //end else.
- lcd.print(F("C "));
- } // else.
- break; //end case 8.
- } //end switch menu.
- } //end if door closed.
- else {
- doorLineCleared = false;
- lcd.setCursor(2, 1);
- lcd.print(F(" Door open! "));
- digitalWrite(greenLed, LOW);
- digitalWrite(yellowLed, LOW);
- digitalWrite(relay, LOW);
- heaterState = 0;
- analogWrite(redLed, 180);
- heaterOffTime = currentTime;
- } //end else if.
- //decide when to turn heater on or off:
- if (hour >= morningHour && hour < eveningHour) { //day time
- if (((!celsius && tempAvg > -41 && tempAvg < tempHold) ||
- (celsius && tempAvgC > -41 && tempAvgC < tempHoldC)) && doorState == 0) {
- heaterOn();
- } //end if.
- else {
- heaterOff();
- } //end else.
- } //end if day time.
- else if (hour < morningHour || hour >= eveningHour) { //night time
- if (((!celsius && tempAvg > -41 && tempAvg < (tempHold - tempReduction)) ||
- (celsius && tempAvgC > -41 && tempAvgC < (tempHoldC - tempReductionC))) && doorState == 0) {
- heaterOn();
- } //end if.
- else {
- heaterOff();
- } //end else.
- } //end if night time.
- } //end loop().
- /* getTemp(): each time the function is called, a new reading is taken and stored to the 3x10 matrix. each row is then averaged and
- returned as temp for each sensor. AREF = 2.04V.
- */
- int8_t getTemp(uint8_t pin) {
- val[pin][reading] = analogRead(pin);
- delay(2); //delay between each analog pin is needed to reduce cross interferance between readings.
- if (val[pin][reading] < 50) { //sensor unplugged.
- return -41;
- } //end if.
- uint16_t sum = 0;
- for (uint8_t i = 0; i <= 9; i ++) { //adds up all the values in the row.
- sum += val[pin][i];
- } //end for.
- sum /= 10;
- float tempC = ((sum - 50.0) * (165.0) / (828.0)) - 40.0; //convert analog reading to celsius using floating point math instead of map(sum, 50, 878, -40, 125).
- if (celsius) {
- return round(tempC);
- } //end if.
- else {
- return round((1.8 * tempC) + 32.0);
- } // end else.
- } //end getTemp().
- int8_t getOneWireTemp() {
- byte data[2]; //array to store data retrieved from sensor.
- oneWire.reset();
- oneWire.select(addr);
- oneWire.write(0x44, 1); //start conversion.
- oneWire.reset();
- oneWire.select(addr);
- oneWire.write(0xBE); //read scratchpad.
- for (byte i = 0; i < 3; i ++) {
- data[i] = oneWire.read(); //collect data.
- } //end for.
- int16_t raw = (data[1] << 8) | data[0]; //convert raw data to C.
- if (celsius) {
- return round((float)raw / 16.0);
- } //end if.
- else {
- return round(((float)raw / 16.0) * 1.8 + 32.0);
- } //end else.
- } //end getOneWireTemp().
- void heaterOn() {
- if (currentTime - heaterOffTime > heaterDelay) { //check to see if heater has been off for at least 2 min.
- digitalWrite(relay, HIGH);
- analogWrite(yellowLed, 180);
- heaterOnTime = currentTime;
- heaterState = 1;
- } //end if.
- } //end heaterOn().
- void heaterOff() {
- if (currentTime - heaterOnTime > heaterDelay) { //check to see if heater has been on for at least 2 min.
- digitalWrite(relay, LOW);
- digitalWrite(yellowLed, LOW);
- heaterOffTime = currentTime;
- heaterState = 0;
- } //end if.
- } //end heaterOff().
- void clearLine() { //clears 2nd line past M#.
- lcd.setCursor(2, 1);
- lcd.print(F(" "));
- } //end clearLine().
- int8_t average(int8_t temp_1, int8_t temp_2, int8_t temp_3) { //averages 3 temps and knows how depending on which sensors are plugged in.
- uint8_t count = 0;
- float avg = 0.0;
- if (temp_1 > -41) { //sensor connected.
- count ++;
- avg += temp_1;
- } //end if.
- if (temp_2 > -41) {
- count ++;
- avg += temp_2;
- } //end if.
- if (temp_3 > -41) {
- count ++;
- avg += temp_3;
- } //end if.
- if (count != 0) {
- return round(avg / count);
- } //end if.
- else {
- return -41; //all three sensors unplugged.
- } //end else.
- } //end average().
- void RTC_init() { //start the RTC chip.
- SPI.begin();
- SPI.setBitOrder(MSBFIRST);
- SPI.setDataMode(SPI_MODE1); // both mode 1 & 3 should work.
- digitalWrite(cs, LOW); //set control register.
- SPI.transfer(0x8E);
- SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled.
- digitalWrite(cs, HIGH);
- delay(10);
- } //end RTC_init().
- String ReadTimeDate(uint8_t mode) { //gets time and date.
- String dateTime;
- uint8_t TimeDate [7]; //second,minute,hour,null,day,month,year
- for (uint8_t i = 0; i <= 6; i ++) {
- if (i == 3) {
- i ++;
- } //end if.
- digitalWrite(cs, LOW);
- SPI.transfer(i + 0x00);
- unsigned int n = SPI.transfer(0x00);
- digitalWrite(cs, HIGH);
- int a = n & B00001111;
- if (i == 2) {
- int b = (n & B00110000) >> 4; //24 hour mode
- if (b == B00000010) {
- b = 20;
- } //end if.
- else if (b == B00000001) {
- b = 10;
- } //end else.
- TimeDate[i] = a + b;
- } //end if.
- else if (i == 4) {
- int b = (n & B00110000) >> 4;
- TimeDate[i] = a + b * 10;
- } //end else.
- else if (i == 5) {
- int b = (n & B00010000) >> 4;
- TimeDate[i] = a + b * 10;
- } //end else.
- else if (i == 6) {
- int b = (n & B11110000) >> 4;
- TimeDate[i] = a + b * 10;
- } //end else.
- else {
- int b = (n & B01110000) >> 4;
- TimeDate[i] = a + b * 10;
- } //end else.
- } //end for.
- if (daylightSavings) { //reduce hour slot by 1.
- TimeDate[2] -= 1;
- } //end if.
- switch (mode) {
- case 0: //returns date only.
- if (TimeDate[5] < 10) {
- dateTime.concat("0"); //leading zero.
- } //end if.
- dateTime.concat(TimeDate[5]);
- dateTime.concat("/");
- if (TimeDate[4] < 10) {
- dateTime.concat("0");
- } //end if.
- dateTime.concat(TimeDate[4]);
- dateTime.concat("/");
- dateTime.concat(TimeDate[6]);
- return dateTime;
- break; //end mode 0.
- case 1: //returns time only.
- if (TimeDate[2] < 10) {
- dateTime.concat("0"); //leading zero
- } //end if.
- dateTime.concat(TimeDate[2]);
- dateTime.concat(":");
- if (TimeDate[1] < 10) {
- dateTime.concat("0");
- } //end if.
- dateTime.concat(TimeDate[1]);
- dateTime.concat(":");
- if (TimeDate[0] < 10) {
- dateTime.concat("0");
- } //end if.
- dateTime.concat(TimeDate[0]);
- return dateTime;
- break; //end mode 1.
- case 2: //returns hour of day only.
- dateTime.concat(TimeDate[2]);
- return dateTime;
- break; //end mode 2.
- } //end switch.
- } //end ReadTimeDate().
- void serialEvent() { //called any time serial data is available.
- char c = Serial.read();
- switch (c) {
- case '*': //all values requested. sends string of data to be served by other arduino. IE " 64h63a17j9e7m8t0d1f61x62y65z58w* "
- Serial.print(tempHold);
- Serial.print(F("h")); //indicates end of tempHold.
- if (tempAvg > -41) {
- Serial.print(tempAvg);
- } //end if.
- else {
- Serial.print(F("--"));
- } //end else.
- Serial.print(F("a"));
- Serial.print(hour);
- Serial.print(F("j"));
- Serial.print(eveningHour);
- Serial.print(F("e"));
- Serial.print(morningHour);
- Serial.print(F("m"));
- Serial.print(tempReduction);
- Serial.print(F("t"));
- Serial.print(doorState);
- Serial.print(F("d"));
- Serial.print(heaterState);
- Serial.print(F("f"));
- if (temp1 > -41) {
- Serial.print(temp1);
- } //end if.
- else {
- Serial.print(F("--"));
- } //end else.
- Serial.print(F("x"));
- if (temp2 > -41) {
- Serial.print(temp2);
- } //end if.
- else {
- Serial.print(F("--"));
- } //end else.
- Serial.print(F("y"));
- if (temp3 > -41) {
- Serial.print(temp3);
- } //end if.
- else {
- Serial.print(F("--"));
- } //end else.
- Serial.print(F("z"));
- if (tempOneWire > -51) {
- Serial.print(tempOneWire);
- }
- else {
- Serial.print(F("--"));
- }
- Serial.print(F("w"));
- Serial.println(F("*")); //end of data character.
- break; //end case '*'.
- case 'h': //increase hold temp.
- tempHold += 2;
- if (tempHold > 80) {
- tempHold = 80;
- } //end if.
- EEPROM.write(0, tempHold);
- break; //end case 'h'.
- case 'n': //decrease hold temp.
- tempHold -= 2;
- if (tempHold < 12) {
- tempHold = 12;
- } //end if.
- EEPROM.write(0, tempHold);
- break; //end case 'n'.
- case 'e': //heat off later.
- eveningHour ++;
- if (eveningHour > 24) {
- eveningHour = 24;
- } //end if.
- EEPROM.write(3, eveningHour);
- break; //end case 'e'.
- case 'd': //heat off earlier.
- eveningHour --;
- if (eveningHour < 16) {
- eveningHour = 16;
- } //end if.
- EEPROM.write(3, eveningHour);
- break; //end case 'd'.
- case 'm': //heat on later.
- morningHour ++;
- if (morningHour > 11) {
- morningHour = 11;
- } //end if.
- EEPROM.write(2, morningHour);
- break; //end case 'm'.
- case 'j': //heat on earlier.
- morningHour --;
- if (morningHour < 3) {
- morningHour = 3;
- } //end if.
- EEPROM.write(2, morningHour);
- break; //end case 'j'.
- case 't': //reduce temp more.
- tempReduction += 2;
- if (tempReduction > 12) {
- tempReduction = 12;
- } //end if.
- EEPROM.write(1, tempReduction);
- break; //end case 't'.
- case 'g': //reduce temp less.
- tempReduction -= 2;
- if (tempReduction < 0) {
- tempReduction = 0;
- } //end if.
- EEPROM.write(1, tempReduction);
- break; //end case 'g'.
- } //end switch.
- } //end serialEvent().
- /*
- void printResults() { //for debugging.
- Serial.println(ReadTimeDate(0));
- Serial.println(ReadTimeDate(1));
- Serial.print(F("Menu #: "));
- Serial.println(menu);
- Serial.println();
- Serial.print(F("Hold temp: "));
- Serial.println(tempHold);
- Serial.println();
- Serial.print(F("Average temp: "));
- Serial.println(tempAvg);
- Serial.println();
- Serial.println(F("Sensor one: "));
- for (uint8_t i = 0; i <= 9; i ++) {
- Serial.print(val[0][i]);
- Serial.print(F(" "));
- } //end for.
- Serial.println();
- Serial.println(F("Sensor two: "));
- for (uint8_t i = 0; i <= 9; i ++) {
- Serial.print(val[1][i]);
- Serial.print(F(" "));
- } //end for.
- Serial.println();
- Serial.println(F("Sensor three: "));
- for (uint8_t i = 0; i <= 9; i ++) {
- Serial.print(val[2][i]);
- Serial.print(F(" "));
- } //end for.
- Serial.println();
- } //end printResults().*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement