Pin stays low

In the code pasted below My pump2 stays low when its suppose to be high. I’m using octo coupled relays and when the pin is low the relay is on when high off. but stays low leaving the relay on. heres the code.

/*
*/

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include "LiquidCrystal.h"
LiquidCrystal lcd(0);

#define ONE_WIRE_BUS 3 //pin 3 for sensons signal


OneWire one_wire(ONE_WIRE_BUS);
DallasTemperature sensors(&one_wire);
DeviceAddress tank_sensor = {0x28, 0xFF, 0x3F, 0x12, 0x69, 0x14, 0x04, 0x01 };
DeviceAddress collector1_sensor = {0x28, 0xFF, 0x83, 0x32, 0x64, 0x14, 0x03, 0x8C };
DeviceAddress collector2_sensor ={0x28, 0xFF, 0xC0, 0x0A, 0x64, 0x14, 0x03, 0x7F };

int pump1_pin = 12;
boolean pump1_on = false;

int pump2_pin = 11;
boolean pump2_on = false;

float tank_max_temp = 110;      
float collector_min_temp = 30;  
float on_threshold = 5;        
float off_threshold = 7;       
int interval = 5000;           


void setup()
{
 Serial.begin(9600);
 sensors.begin();
 pinMode(pump1_pin, OUTPUT);
 pinMode(pump2_pin, OUTPUT);
 lcd.begin(16, 4);
 
 lcd.setCursor(0, 0);
 lcd.print("Waiting for");
 lcd.setCursor(0, 1);
 lcd.print("valid data ...");
}

void pump1() {
 pump1_on = true;
 digitalWrite(pump1_pin, LOW);
 
}

void pump2() {
 pump2_on = true;
 digitalWrite(pump2_pin, LOW);
 
} 

void no_pump1() {
 pump1_on = false;
 digitalWrite(pump1_pin, HIGH);
 
}

void no_pump2() {
 pump2_on = false;
 digitalWrite(pump2_pin, HIGH);
 
}

void print_temps(float tank_temp, float collector1_temp, float collector2_temp) {
 Serial.print(tank_temp);
 Serial.print(", ");
 Serial.println(collector1_temp);
 Serial.print(",");
 Serial.print(collector2_temp);
}

void lcd_print_temps(float tank_temp, float collector1_temp, float collector2_temp ) {
 lcd.clear();
 lcd.setCursor(0, 0);
 lcd.print("Coll-1: ");
 lcd.print(collector1_temp);
 lcd.setCursor(0, 1);
 lcd.print("Coll-2: ");
 lcd.print(collector2_temp);
 lcd.setCursor(0,2);
 lcd.print("Tank-1: ");
 lcd.print(tank_temp);
}


void loop()
{
 sensors.requestTemperatures();
 float tank_temp = sensors.getTempC(tank_sensor);
 float collector1_temp = sensors.getTempC(collector1_sensor);
 float collector2_temp = sensors.getTempC(collector2_sensor);

 print_temps(tank_temp, collector1_temp, collector2_temp);
 lcd_print_temps(tank_temp, collector1_temp, collector2_temp);

 
 if (tank_temp < -50 || collector1_temp < -50 || collector2_temp < -50 ) {
   
 }

 else {

   
   if (tank_temp >= tank_max_temp) {
     
     no_pump1();
     
   }
   
   if (tank_temp >= tank_max_temp) {
     no_pump2();
   }

   
   else if (collector1_temp <= collector_min_temp) {
     
     no_pump1();
     
   }
   else if (collector2_temp <= collector_min_temp) {
     no_pump2();
   }

   
   else if (collector1_temp > tank_temp + on_threshold) {
     
     pump1();
   }
   
   else if (collector2_temp > tank_temp + on_threshold) {
     pump2();
   }

   
   else if (collector1_temp < tank_temp + off_threshold) {
     
     no_pump1();
   }
   
   else if (collector2_temp < tank_temp + off_threshold) {
     no_pump2();
   }

   
   else {
   }
 }

 delay(interval);
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

In the code pasted below My pump2 stays low when its suppose to be high.

Only you can see the data read from the sensors.

float tank_max_temp = 110;      
float collector_min_temp = 30;  
float on_threshold = 5;        
float off_threshold = 7;

Why are these floats? If they really should be, then the initializers should be, too.

PaulS:
Only you can see the data read from the sensors.

float tank_max_temp = 110;      

float collector_min_temp = 30; 
float on_threshold = 5;       
float off_threshold = 7;



Why are these floats? If they really should be, then the initializers should be, too.

Why? The compiler can't assign an int to a float? Can you show some code that illustrates this as a problem?

How would that be related to the OP's problem of the pin staying low?

Why? The compiler can't assign an int to a float?

Yes, it can. It's smarter than you are.

But, assigning 5.0 shows that you KNOW that the variable should be a float.

How would that be related to the OP's problem of the pin staying low?

Who said it was directly related? It's a potential problem area. That is ALL that can be implied from my post.

PaulS:
It's a potential problem area.

What problem? The compiler will happily convert the int to a float. There should be no side effects or errors.

Now going the other way might be a problem:

int someVar = 5.5;

won't work out.

Hi Adogg85 -

Have a look on the modified code submitted here.
May I ask where you use the boolean value pumpx_on ?
If you dont use, there is no reason to have the subroutines for the pumps.
You can then instead directly write to the pump output in your if compare list.

/*
*/

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include "LiquidCrystal.h"
LiquidCrystal lcd(0);

#define ONE_WIRE_BUS 3 // pin 3 for sensors signal

OneWire one_wire(ONE_WIRE_BUS);
DallasTemperature sensors(&one_wire);
DeviceAddress tank_sensor       = {0x28, 0xFF, 0x3F, 0x12, 0x69, 0x14, 0x04, 0x01 };
DeviceAddress collector1_sensor = {0x28, 0xFF, 0x83, 0x32, 0x64, 0x14, 0x03, 0x8C };
DeviceAddress collector2_sensor = {0x28, 0xFF, 0xC0, 0x0A, 0x64, 0x14, 0x03, 0x7F };

// constants setting working conditions
const byte pump1_pin            = 12;    // define pump1 output pin no
const byte pump2_pin            = 11;    // define pump2 output pin no
const float tank_max_temp       = 110;
const float collector_min_temp  = 30;
const float on_threshold        = 5;
const float off_threshold       = 7;
const int interval              = 5000;

// working global variables used by program
float tank_temp                 = 0:
float collector1_temp           = 0;
float collector2_temp           = 0;
boolean pump2_on                = false;
boolean pump1_on                = false;

void setup()                      // initialise pump controller
{
  Serial.begin(9600);
  sensors.begin();
  pinMode(pump1_pin, OUTPUT);
  pinMode(pump2_pin, OUTPUT);
  lcd.begin(16, 4);
  lcd.setCursor(0, 0);
  lcd.print("Waiting for");
  lcd.setCursor(0, 1);
  lcd.print("valid data ...");
}

void pump1()                      // start pump1
{
  pump1_on = true;
  digitalWrite(pump1_pin, LOW);
}

void pump2()                      // start pump2
{
  pump2_on = true;
  digitalWrite(pump2_pin, LOW);
}

void no_pump1()                   // stop pump1
{
  pump1_on = false;
  digitalWrite(pump1_pin, HIGH);
}

void no_pump2()                   // stop pump2
{
  pump2_on = false;
  digitalWrite(pump2_pin, HIGH);
}

void print_temps()                // print temperatures on serial monitor
{
  Serial.print(tank_temp);
  Serial.print(", ");
  Serial.println(collector1_temp);
  Serial.print(",");
  Serial.print(collector2_temp);
}

void lcd_print_temps()            // print temperatures on LCD display
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Coll-1: ");
  lcd.print(collector1_temp);
  lcd.setCursor(0, 1);
  lcd.print("Coll-2: ");
  lcd.print(collector2_temp);
  lcd.setCursor(0, 2);
  lcd.print("Tank-1: ");
  lcd.print(tank_temp);
}

void loop()
{
  sensors.requestTemperatures();                                             // read sensor values
  tank_temp = sensors.getTempC(tank_sensor);
  collector1_temp = sensors.getTempC(collector1_sensor);
  collector2_temp = sensors.getTempC(collector2_sensor);

  print_temps();                                                             // print sensor values
  lcd_print_temps();
 
  if (tank_temp >= -50 && collector1_temp >= -50 && collector2_temp >= -50   // if no sensors are below -50°
  {
    if (tank_temp >= tank_max_temp)                                          // if tank sensor >= max, then
    {
      no_pump1();                                                            // stop pump1
      no_pump2();                                                            // and stop pump2
    }
    else if (collector1_temp <= collector_min_temp) no_pump1();              // if collector1 <= min, then stop pump1
    else if (collector2_temp <= collector_min_temp) no_pump2();              // if collector2 <= min, then stop pump2
    else if (collector1_temp > tank_temp + on_threshold) pump1();            // if collector1 is above treshold, then start pump1
    else if (collector2_temp > tank_temp + on_threshold) pump2();            // if collector2 is above treshold, then start pump2
    else if (collector1_temp < tank_temp + off_threshold) no_pump1();        // if collector1 is under treshold, then stop pump1
    else if (collector2_temp < tank_temp + off_threshold) no_pump2();        // if collector2 is under treshold, then stop pump2
  }
  delay(interval);
}

Delta_G:
Why? The compiler can’t assign an int to a float? Can you show some code that illustrates this as a problem?

Well there are many good reasons. The principle of “say what you mean” is certainly sound.

Here is the most common problem. The programmer declares:

float pie = 1;

The value of pie is 1.0 and everything is good.

… next day, decides it should be half that value and changes it to

float pie = 1/2;

Oops! Why is pie now 0.0? I expected 0.5! Help!

Solution. 1 and 2 are integers by default. The result of the integer division is 0. So use:

float pie = 1.0;

because later when you change it to:

float pie = 1.0/2;

It will work.

Yes, it can. It's smarter than you are.

if ( (Yes, it can. It's smarter than you are.)==(a personal attack) || (against the rules/warnings/advice of this forum))

((The person who wrote this should behave himself);)

http://forum.arduino.cc/index.php?topic=149014.0

Please post technical questions on the forum, not by personal message. Thanks!

More info: Gammon Forum : Electronics