SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By limelynx
#194455
Hi all. I am building a control for a freezer. I need to start the freezer at the specified temp and shut off when that is satisfied. (I have that) What I'm having trouble with is, I need to have the compressor stay off for 3 minutes before starting again. This is a single stage freezer getting down to -50 F. It gets above the specified temp before 3 minutes and causes the compressor to attempt to come on and lock up. What I need, I think, is a way to override the if statement reading the probe, so the loop will not run until the 3 minutes have elapsed, without stopping the display from reading the temp. Thanks for any help.
Code: Select all
#include <OneWire.h>
#include <DallasTemperature.h>
 
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 3
#define ct 5
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int offtime=1500;

void setup(void)
  { pinMode(ct, OUTPUT);
  // start serial port
  Serial.begin(9600);
  Serial.println("        ColdTrap");

  elapsedMillis elapsedTime;    // used by elapsedmilis example
                              // Globally scoped - see comment above

unsigned long off = millis();  // used by millis() example...

unsigned int interval = 1000;

  // Start up the library
  sensors.begin();
  }
 
 
void loop(void)
  {   delay (offtime);
     


  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print(" ");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println(" ");


  Serial.print("         ");
  Serial.print(sensors.getTempFByIndex(0)); // Why "byIndex"?
    // You can have more than one IC on the same bus.
    // 0 refers to the first IC on the wire
 {
     
     
   
 { if(sensors.getTempFByIndex(0) >= -50)
      digitalWrite (ct, HIGH);
   else
      digitalWrite (ct, LOW);
right now everything works but no time delay
again thanks for any help, i've been trying to get this for a few weeks.
not sure how many variations of delay,while loops and "and" functions I'v tried
#194465
You could try setting a up state variable and a time stamp variable (unsigned long variable based on current millis()) once the temperature is low enough. Then, check (in a While loop) if the state variable is set (ie less than 50 degrees happened), check the time through the loop if current millis() minus the time stamp variable is 3 minutes or more. If that happens in the while loop, clear the state variable, then continue outside the while loop checking temperatures and turn on compressor if required.
#194467
Maybe this would work.
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 3
#define ct 5
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

unsigned long elaped_time_start = 0;
int timeout_status=LOW;
int flag=LOW;

void setup(void)
{ pinMode(ct, OUTPUT);
// start serial port
Serial.begin(9600);
Serial.println(" ColdTrap");
// Start up the library
sensors.begin();
}


void loop(void){

// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print(" ");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println(" ");
Serial.print(" ");
Serial.print(sensors.getTempFByIndex(0)); // Why "byIndex"?
// You can have more than one IC on the same bus.
// 0 refers to the first IC on the wire

if(sensors.getTempFByIndex(0) >= -50){
if (timeout_status==LOW){
digitalWrite (ct, HIGH); // compressor on
flag=LOW; // keep track of first time cut out
}
else {
digitalWrite (ct, LOW); // compressor off
if (flag == LOW){ // first time compressor cut out since compressor was on
elaped_time_start = millis();
flag=HIGH; // block resetting of elaped_time_start on future passes
}
else{ // check if timeout over
if ((millis() - elaped_time_start >= 180,000)){
timeout_status=LOW; // timeout over
}
else{
timeout_status=HIGH; // not over
}
}

}}}
By limelynx
#194495
Wow jbike, thank you. I am impressed you took the time to actually write out some code for me. I don't have time this morning, but when I can I will try this. I am pretty new at this and still learning. Thank you very much.
#194497
Slight error in my code. Corrected below. To test code, you can substitute a pot for the sensor and make the ct pin 13 (has the built in led) to quickly test whether it works or not.
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 3
#define ct 5
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

unsigned long elaped_time_start = 0;
int timeout_status=LOW;
int flag=LOW;

void setup(void)
{ pinMode(ct, OUTPUT);
// start serial port
Serial.begin(9600);
Serial.println(" ColdTrap");
// Start up the library
sensors.begin();
}


void loop(void){

// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print(" ");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println(" ");
Serial.print(" ");
Serial.print(sensors.getTempFByIndex(0)); // Why "byIndex"?
// You can have more than one IC on the same bus.
// 0 refers to the first IC on the wire

if(sensors.getTempFByIndex(0) >= -50 && timeout_status==LOW){

digitalWrite (ct, HIGH); // compressor on
flag=LOW; // keep track of first time cut out
}
else {
digitalWrite (ct, LOW); // compressor off
if (flag == LOW){ // first time compressor cut out since compressor was on
elaped_time_start = millis();
flag=HIGH; // block resetting of elaped_time_start on future passes
}
// check if timeout over
if ((millis() - elaped_time_start >= 180000)){ // 3 min. delay
timeout_status=LOW; // timeout over
}
else{
timeout_status=HIGH; // not over
}
}}
By limelynx
#194532
Thank You, Thank You, Thank You, jbike. I finally got a chance to try this tonight, and it works perfectly. I had written some code that worked for the delay, but started the delay, only after the temp threshold was met. It didn't matter how much time had passed, it would add the delay after 5 or 10 mins. This works great. Again thank you very much.