Temp Controller

Discussion in 'The Projects Forum' started by james211, Oct 6, 2014.

  1. james211

    Thread Starter Member

    May 29, 2012
    210
    2
    So I've posted about a similar project before, but have moved on and need a bit of advice. I have an arduino UNO, a DS18B20 probe, an LCD and an MDFLY 4CH Relay Control Board - Optical-Coupler Isolated. I have the programming now so the device will read which relay is on or off, the temp set so the the corresponding relay will turn ON or OFF at above or below a certain temp, but I cannot get them turn off in the middle ground.

    Basically, I want the heater (relay2) to turn ON at 77 and turn OFF at 78 and the fan (relay1)to turn ON at 80 and OFF at 79. Right now the heater stays on until the fan turns on at 80.

    Here is the code:
    Code (Text):
    1.  
    2. #include <OneWire.h>
    3. #include <Wire.h>
    4. #include <DallasTemperature.h>
    5. #include <LiquidCrystal_I2C.h>
    6.  
    7. //RGBLED
    8. int bluePin1 = 10;                        //Digital Pin for blue LED1 in RGB-LED
    9. int redPin1 = 12;                         //Digital Pin for red LED1 in RGB-LED
    10.  
    11. int redIn1 = 0;
    12. int blueIn1 = 2;
    13.  
    14. int redVal1;
    15. int blueVal1;
    16.  
    17. //Relays
    18. int relPin1 = 2;                        //Digital Pin Relay1
    19. int relPin2 = 3;                        //Digital Pin Relay2
    20.  
    21. //Heating Status
    22. int heat1Val;
    23. #define BACKLIGHT_PIN 13
    24. #define ONE_WIRE_BUS 7
    25. LiquidCrystal_I2C lcd(0x20,20,4);  // set the LCD address to 0x20 for a 20 chars and 4 line display
    26. OneWire oneWire(ONE_WIRE_BUS);
    27. DallasTemperature sensors(&oneWire);
    28.  
    29. void setup() {
    30.   Serial.begin(9600);
    31.   sensors.begin();
    32.   lcd.init();                      // initialize the lcd
    33.   lcd.backlight();
    34.   pinMode(relPin1, OUTPUT);      //Output Mode for Relay1
    35.   pinMode(relPin2, OUTPUT);      //Output Mode for Relay2
    36. }
    37.  
    38. void loop() {
    39.   sensors.requestTemperatures();
    40.   float temperature1 = sensors.getTempFByIndex(0);
    41.   lcd.setCursor(0, 0);
    42.   lcd.print("Johnson's Reef");
    43.   lcd.setCursor(0, 1);
    44.   lcd.print("Aquarium: ");
    45.   lcd.print(sensors.getTempFByIndex(0));
    46.   lcd.print(" F ");
    47.  
    48.   //HEATER
    49.   if(temperature1 < 77)
    50.   {
    51.     redVal1 = 0;
    52.     blueVal1 = 5;
    53.     digitalWrite(relPin1, LOW);  //FAN
    54.     digitalWrite(relPin2, HIGH); //HEATER
    55.     lcd.setCursor(0, 2);
    56.     lcd.print("Heater:   ON ");
    57.     lcd.setCursor(0, 3);
    58.     lcd.print("Fan:      OFF ");
    59.   }
    60.   //FAN
    61.   else if(temperature1 > 80)
    62.   {
    63.     redVal1 = 150;
    64.     blueVal1 = 0;
    65.     digitalWrite(relPin1, HIGH);  //FAN
    66.     digitalWrite(relPin2, LOW);  //HEATER
    67.     lcd.setCursor(0, 2);
    68.     lcd.print("Heater:   OFF ");
    69.     lcd.setCursor(0, 3);
    70.     lcd.print("Fan:      ON ");
    71.   }
    72.  
    73.  
    74.   {
    75.     analogWrite(redPin1, redVal1);
    76.     analogWrite(bluePin1, blueVal1);
    77.  
    78.   }
    79. }
    80.  

     
  2. james211

    Thread Starter Member

    May 29, 2012
    210
    2
    Ok, I added some additional code at the bottom and it seems to work now. Is there anything anyone can see that I should change for any reason?

    Code (Text):
    1.  
    2. #include <OneWire.h>
    3. #include <Wire.h>
    4. #include <DallasTemperature.h>
    5. #include <LiquidCrystal_I2C.h>
    6.  
    7. //RGBLED
    8. int bluePin1 = 10;  //Digital Pin for blue LED1 in RGB-LED
    9. int redPin1 = 12;  //Digital Pin for red LED1 in RGB-LED
    10.  
    11. int redIn1 = 0;
    12. int blueIn1 = 2;
    13.  
    14. int redVal1;
    15. int blueVal1;
    16.  
    17. //Relays
    18. int relPin1 = 2;  //Digital Pin Relay1
    19. int relPin2 = 3;  //Digital Pin Relay2
    20.  
    21. //Heating Status
    22. int heat1Val;
    23. #define BACKLIGHT_PIN 13
    24. #define ONE_WIRE_BUS 7
    25. LiquidCrystal_I2C lcd(0x20,20,4);  // set the LCD address to 0x20 for a 20 chars and 4 line display
    26. OneWire oneWire(ONE_WIRE_BUS);
    27. DallasTemperature sensors(&oneWire);
    28.  
    29. void setup() {
    30.   Serial.begin(9600);
    31.   sensors.begin();
    32.   lcd.init();  // initialize the lcd
    33.   lcd.backlight();
    34.   pinMode(relPin1, OUTPUT);  //Output Mode for Relay1
    35.   pinMode(relPin2, OUTPUT);  //Output Mode for Relay2
    36. }
    37.  
    38. void loop() {
    39.   sensors.requestTemperatures();
    40.   float temperature1 = sensors.getTempFByIndex(0);
    41.   lcd.setCursor(0, 0);
    42.   lcd.print("Johnson's Reef");
    43.   lcd.setCursor(0, 1);
    44.   lcd.print("Aquarium: ");
    45.   lcd.print(sensors.getTempFByIndex(0));
    46.   lcd.print(" F ");
    47.  
    48.   //HEATER
    49.   if(temperature1 < 77)
    50.   {
    51.   redVal1 = 0;
    52.   blueVal1 = 5;
    53.   digitalWrite(relPin1, LOW);  //FAN
    54.   digitalWrite(relPin2, HIGH); //HEATER
    55.   lcd.setCursor(0, 2);
    56.   lcd.print("Heater:  ON ");
    57.   lcd.setCursor(0, 3);
    58.   lcd.print("Fan:  OFF ");
    59.   delay(5000);
    60.   }
    61.   //FAN
    62.   else if(temperature1 > 80)
    63.   {
    64.   redVal1 = 150;
    65.   blueVal1 = 0;
    66.   digitalWrite(relPin1, HIGH);  //FAN
    67.   digitalWrite(relPin2, LOW);  //HEATER
    68.   lcd.setCursor(0, 2);
    69.   lcd.print("Heater:  OFF ");
    70.   lcd.setCursor(0, 3);
    71.   lcd.print("Fan:  ON ");
    72.   delay(5000);
    73.   }
    74.   //HEATER
    75.   else if(temperature1 = 78)
    76.   {
    77.   redVal1 = 0;
    78.   blueVal1 = 0;
    79.   digitalWrite(relPin1, LOW);  //FAN
    80.   digitalWrite(relPin2, LOW);  //HEATER
    81.   lcd.setCursor(0, 2);
    82.   lcd.print("Heater:  OFF ");
    83.   lcd.setCursor(0, 3);
    84.   lcd.print("Fan:  OFF ");
    85.   delay(5000);
    86.   }
    87.   //FAN
    88.   else if(temperature1 = 79)
    89.   {
    90.   redVal1 = 0;
    91.   blueVal1 = 0;
    92.   digitalWrite(relPin1, LOW);  //FAN
    93.   digitalWrite(relPin2, LOW);  //HEATER
    94.   lcd.setCursor(0, 2);
    95.   lcd.print("Heater:  OFF ");
    96.   lcd.setCursor(0, 3);
    97.   lcd.print("Fan:  OFF ");
    98.   delay(5000);
    99.   }
    100.   {
    101.   analogWrite(redPin1, redVal1);
    102.   analogWrite(bluePin1, blueVal1);
    103.  
    104.   }
    105. }
    106.  
    107.  
     
    Last edited: Oct 6, 2014
Loading...