if statement

Hi guys. I need a bit more guidance with my code. I feel Im close or closer but maybe along way off I dont know.
I need relay 1 to come on if the temp goes above 27 and relay 2 on if it drops below 25. I know the 'if' statement in my code is wrong, but I left it there just to show the intent.
Any help would be appreciated.

// Libraries
#include <Wire.h> 
#include <OneWire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
#include <DallasTemperature.h> //will find addresses of sensors automatically
const char degree = 223;   //This will give us the degree symbol after the temp 


// Data wire from DS18B20 to Arduino Digital PIn 2
#define ONE_WIRE_BUS 2
//Relays
int relay_1 = 22;       //Digital Pin for Relay1
int relay_2 = 23;       //Digital Pin for Relay2
int relay_3 = 24;       //Digital Pin for Relay3



float high_temp = 28.00;
float low_temp = 25.00;




// Setup oneWire instance to communicate with devices
OneWire oneWire(ONE_WIRE_BUS);

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



void setup()
{
  
 //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(relay_1, LOW);
  digitalWrite(relay_2, LOW);
  digitalWrite(relay_3, LOW);
   
  
//---( THEN set pins as outputs )----  
  pinMode(relay_1, OUTPUT);   
  pinMode(relay_2, OUTPUT);  
  pinMode(relay_3, OUTPUT);  

  
  
  
  Serial.begin(9600);
  // set up the LCD's number of rows and columns:
  // initialize the Relay pin as an output:
  lcd.begin(16, 2);
  lcd.print ("Aquarium");
  lcd.setCursor(2,1);
  lcd.print ("Controller");
  delay(2000);
  lcd.clear();
  lcd.setCursor (2,0);
  lcd.print ("Target Temp:");
  lcd.setCursor (6,1);
  lcd.print ("27");
  lcd.print (degree);
  lcd.print ("C");
  delay(2000);
  lcd.clear();
  // Start the OneWire library
  sensors.begin();
}

void readtemp(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  sensors.requestTemperatures(); // Get temps
  lcd.setCursor ( 0, 0 ); //Print Display temp on top line
  lcd.print ("Display: ");
  lcd.print(sensors.getTempCByIndex(1)); //Use this if you want C
  //lcd.print(sensors.getTempCByIndex(1)* 9/5 * .54); //convert to F
  lcd.print (degree); //display degree symbol after temp value
  //lcd.print ("f"); //farenheit
  lcd.setCursor ( 0, 1 ); //Print Room temp on bottom line
  lcd.print ("Room: ");
  lcd.print(sensors.getTempCByIndex(0)); //Use this if you want C
  //lcd.print(sensors.getTempCByIndex(0)* 9/5 * .54); //convert to F
  lcd.print (degree);
  //lcd.print ("c");
}
void loop(){
   
    
  lcd.setCursor ( 15, 0 ); //symbol "*" flashes on top line as temps are read
  lcd.print("*");
  readtemp();
  delay (500);//reads temp every .5 second
  lcd.setCursor ( 15, 0 );
  lcd.print(" ");
  delay (500);
  
  if(degree >28, relay_1, HIGH);
  if(degree <25, relay_2, HIGH);
  
  
      
   
}
if(degree >28, relay_1, HIGH);

You forgot the "digitalWrite"

AWOL:

if(degree >28, relay_1, HIGH);

You forgot the "digitalWrite"

Oh yeah, thanks AWOL.
Im not near my board, for awhile. Is this right

if(degree>28)
DigitalWrite(relay1, HIGH)
if(degree>28)
DigitalWrite(relay1, HIGH)

Well it needs a ; on the end thusly, and a small "d"

if(degree>28)
digitalWrite(relay1, HIGH);

And don't forget that if there's more than one statement in the "leg" of the if, you need braces. Personally I think it prudent to use braces even if there's only one statement. That way, if you later add another statement, the braces are already there and won't get left off.

if(degree>28)
{
digitalWrite(relay1, HIGH);
}
if(degree>28)
{
digitalWrite(relay1, HIGH);
digitalWrite(someOtherPin, LOW); // need {} if have >1 statement
}

Thanks JimboZA, yep I shouldnt paraphrase.

I've tried it and Im still having issues. The code while it uploaded, didnt act as it should. The relay didnt trip. Changing it to temp just gives the "not declared in this scope"
What should I swap it to.

Chris

// Libraries
#include <Wire.h> 
#include <OneWire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
#include <DallasTemperature.h> //will find addresses of sensors automatically
const char degree = 223;   //This will give us the degree symbol after the temp 


// Data wire from DS18B20 to Arduino Digital PIn 2
#define ONE_WIRE_BUS 2
//Relays
int relay_1 = 22;       //Digital Pin for Relay1
int relay_2 = 23;       //Digital Pin for Relay2
int relay_3 = 24;       //Digital Pin for Relay3



float high_temp = 28.00;
float norm_temp = 26.00;




// Setup oneWire instance to communicate with devices
OneWire oneWire(ONE_WIRE_BUS);

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

int n = 1;

void setup()
{
  
 //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(relay_1, HIGH);
  digitalWrite(relay_2, HIGH);
  digitalWrite(relay_3, HIGH);
   
  
//---( THEN set pins as outputs )----  
  pinMode(relay_1, OUTPUT);   
  pinMode(relay_2, OUTPUT);  
  pinMode(relay_3, OUTPUT);  

  
  
  
  Serial.begin(9600);
  // set up the LCD's number of rows and columns:
  // initialize the Relay pin as an output:
  lcd.begin(16, 2);
  lcd.print ("Spiders");
  lcd.setCursor(2,1);
  lcd.print ("Controller");
  delay(2000);
  lcd.clear();
  lcd.setCursor (2,0);
  lcd.print ("Target Temp:");
  lcd.setCursor (6,1);
  lcd.print ("27");
  lcd.print (degree);
  lcd.print ("C");
  delay(2000);
  lcd.clear();
  // Start the OneWire library
  sensors.begin();
}

void readtemp(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  sensors.requestTemperatures(); // Get temps
  lcd.setCursor ( 0, 0 ); //Print Display temp on top line
  lcd.print ("Display: ");
  lcd.print(sensors.getTempCByIndex(1)); //Use this if you want C
  //lcd.print(sensors.getTempCByIndex(1)* 9/5 * .54); //convert to F
  lcd.print (degree); //display degree symbol after temp value
  //lcd.print ("f"); //farenheit
  lcd.setCursor ( 0, 1 ); //Print Room temp on bottom line
  lcd.print ("Room: ");
  lcd.print(sensors.getTempCByIndex(0)); //Use this if you want C
  //lcd.print(sensors.getTempCByIndex(0)* 9/5 * .54); //convert to F
  lcd.print (degree);
  //lcd.print ("c");
}
void loop(){
   
    
  lcd.setCursor ( 15, 0 ); //symbol "*" flashes on top line as temps are read
  lcd.print("*");
  readtemp();
  delay (500);//reads temp every .5 second
  lcd.setCursor ( 15, 0 );
  lcd.print(" ");
  delay (500);
  
  if(temp>28)
{
digitalWrite(relay_1, HIGH);
}
  if(temp<27)
{
digitalWrite(relay_1, LOW); 
}
  
      
   
}

The code while it uploaded, didnt act as it should.

You can be 99.9999% certain that the code did what it should, i.e. what it was told to do.

const char degree = 223;
if(degree>28)
{
digitalWrite(relay1, HIGH);
digitalWrite(someOtherPin, LOW); // need {} if have >1 statement
}

Do you see the problem?

I was just about to say same as AWOL said: degree is always 223.

But if you change it to temp, you need to a) declare temp somewhere like right at the top, but then b) when you read the temperature you need to put the value you read, into the variable called temp.

AWOL:

The code while it uploaded, didnt act as it should.

You can be 99.9999% certain that the code did what it should, i.e. what it was told to do.

const char degree = 223;
if(degree>28)

{
digitalWrite(relay1, HIGH);
digitalWrite(someOtherPin, LOW); // need {} if have >1 statement
}



Do you see the problem?

I thought this just gave me the the degree symbol

Chris

Maybe it does, but then it's meaningless to check if degree > 28 since 223 is always > 28.

That's why I thought you changed to checking if temp > 28, but that only works if you declare temp and then stick the read value into the variable called temp.

Ahh, good point

Chris

I thought I was close, but I just cant get the final piece to the puzzle. Being my first sketch, its taken me weeks to get to this stage.
Like most at some stage I guess, Im waking at night cause Im coding in my sleep. :~

Chris

Presumably this sensors.requestTemperatures(); // Get temps
puts the temperature you need somewhere where you can request it.
I'd look into that aspect of the Dallas library.

I've got both of those in my sketch already

// Libraries
#include <Wire.h> 
#include <OneWire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
#include <DallasTemperature.h> //will find addresses of sensors automatically
void readtemp(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  sensors.requestTemperatures(); // Get temps
  lcd.setCursor ( 0, 0 ); //Print Display temp on top line
  lcd.print ("Display: ");

Chris

Yes, but where to you get the temperature into a variable that you can display and/or compare?
(I'm sorry, I don't have this library, so I truly don't know)

You have this in readTemp that reads the temperature to show on your LCD:

sensors.getTempCByIndex(0)

Declare your temp variable in loop and use the same function again to assign the temperature to it:

temp=sensors.getTempCByIndex(0);

Thanks Wildbill, I'll give it a go in the morning. I appreciate the help

Chris

Yep, perfect. Just a couple of words out of place makes all the difference. Bit like "I do" ]:smiley:

Chris