having problems with the "if" statement

i am a noob to code and have been trying to write a code for a aquarium controller. so far i think things are going ok but i cant get the lights to turn on at a set time. it has somethig to do with the “if” statement as far as i can tell but i am not sure.here is a list of what i have and the code i have so far.

mega 2560

tiny rtc 12c (like this http://www.rcmodelpart.com/i2c-ds1307-r … _p314.html)

an 8 relay module (http://www.sainsmart.com/8-channel-dc-5 … logic.html)

2 pc fans

float switch

code:
//waysifs aquarium
#include <DS1307.h>
#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527

/*
pin layout


Digital Pin rx0 0 =
Digital Pin tx0 1 =
Digital Pin 2 = relay 1 (dusk/dawn white leds)
Digital Pin 3 = relay 2 (daylight leds) 
Digital Pin 4 = relay 3 (moonlight leds)
Digital Pin 5 = relay 4 (auto topoff pump)
Digital Pin 6 = relay 5 () 
Digital Pin 7 = relay 6 ()
Digital Pin 8 = relay 7 ()
Digital Pin 9 = relay 8 (cfl day lights)
Digital Pin 10 = hood fan
Digital Pin 11 = sump fan
Digital Pin 12 = auto topoff sensor (ato)
Digital Pin 20SDA = rtc SDA
Digital Pin 21SCL = rtc SCL 
*/

int hood_fan = 10; //hod fan pin 10
int sump_fan = 12; // sump fan pin 12
int dusk_dawn = 2;  //dusk/dawn pin 2
int day_light = 3;  //daylights pin 3
int cfl_light = 9; // cfl lights pin 9
int moon_light = 4;  //moon lights pin 4
int ato_pump = 5;  //auto top off pump pin 5
int dusk_dawn_on_time = 8;  //Turn dusk_dawn lights on at this time military time
int dusk_dawn_off_time = 20; //Turn dusk-dawn lights off at this time
int day_light_on_time = 9; // turn day lights on at this time
int day_lights_off_time = 20; //turn day lights off at this time
int cfl_light_on_time = 10; // turn cfls on at this time
int cfl_light_off_time = 19; //turn cfls off at this time
int moonl_on_time = 20;  //Turn on moon lights at this time
int moonl_off_time = 90;  //Turn off moon lights at this time
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
int High = 0;
int Low = 10000;

void setup(){
  Wire.begin();
  Serial.begin(9600);
  


pinMode (dusk_dawn, OUTPUT); //pin 2 output
pinMode (day_light, OUTPUT); //pin 3 output
pinMode (cfl_light, OUTPUT); //pin 9 output
pinMode (moon_light, OUTPUT); //pin 4 output
pinMode (ato_pump, OUTPUT); //pin 5 output
pinMode (hood_fan, OUTPUT); //pin 10 output
pinMode (sump_fan, OUTPUT); // pin 11 output
//pinMode (ato_sensor, INPUT); // pin 12 float sensor

}



void loop (){
 printDate();
  delay(1000);
}

void setDateTime(){

  byte second =      45; //0-59
  byte minute =      40; //0-59
  byte hour =        0; //0-23
  byte weekDay =     2; //1-7
  byte monthDay =    1; //1-31
  byte month =       3; //1-12
  byte year  =       11; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start 

  Wire.endTransmission();

}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);


  //print the date EG   3/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);
}

//dusk_dawn lights

 if (dusk_dawn_on_time) >= hour && <= (dusk_dawn_off_time){
digitalWrite(dusk_dawn, High);
 }
else {
 digitalWrite(dusk_dawn, Low);
}

//day_lights

if(day_light_on_time) >= hour && <= (day_light_off_time){
digitalWrite(day_light, High)
}
else {
  digitalWrite(day_light, Low)
}

//cfl_light
if (cfl_light_on_time >= hour && <= cfl_light_off_time){
  digitalWrite(cfl_light, High)
}
else{
  digitalWrite(cfl_light, Low)
}
//moon_light

//ato pump

//hood fan

any help would be greatly appreciated

cheers

waysif

//sump fan

Please use the code tags "```


You should consult a C programming guide. You are missing lots of semicolons ";"
if(day_light_on_time) >= hour && <= (day_light_off_time){
 digitalWrite(day_light, High);
}
else {
 digitalWrite(day_light, Low);
if (cfl_light_on_time >= hour && <= cfl_light_off_time){
 digitalWrite(cfl_light, High);
  }
else{
 digitalWrite(cfl_light, Low);

After glancing at it, these are the ones that you are missing semicolons. I can’t believe that your code even compiled.

Also, in the IDE, click tools>Auto Format to properly indent your code. Indentation is how people read your code. Right now your code looks like a long paragraph without spaces or punctuation. That is why we use “code” tags.

click to enlarge

thank you for the help and i have gone through and put the “;” in i did not realize i had missed them. after going through and making sure they were all in there i tried to verify the code but it comes up error: expected unqualified-id before ‘if’ and the same thing before ‘else’ and i cant figure out why? i have just started writing code a week ago and starting to think i have bit off more then i can chew so if anyone could help steer me in the right direction it would be much appreciated.

//waysifs aquarium
#include <DS1307.h>
#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527

/*
pin layout


Digital Pin rx0 0 =
Digital Pin tx0 1 =
Digital Pin 2 = relay 1 (dusk/dawn white leds)
Digital Pin 3 = relay 2 (daylight leds) 
Digital Pin 4 = relay 3 (moonlight leds)
Digital Pin 5 = relay 4 (auto topoff pump)
Digital Pin 6 = relay 5 () 
Digital Pin 7 = relay 6 ()
Digital Pin 8 = relay 7 ()
Digital Pin 9 = relay 8 (cfl day lights)
Digital Pin 10 = hood fan
Digital Pin 11 = sump fan
Digital Pin 12 = auto topoff sensor (ato)
Digital Pin 20SDA = rtc SDA
Digital Pin 21SCL = rtc SCL 
*/

int hood_fan = 10; //hod fan pin 10
int sump_fan = 12; // sump fan pin 12
int dusk_dawn = 2;  //dusk/dawn pin 2
int day_light = 3;  //daylights pin 3
int cfl_light = 9; // cfl lights pin 9
int moon_light = 4;  //moon lights pin 4
int ato_pump = 5;  //auto top off pump pin 5
int dusk_dawn_on_time = 8;  //Turn dusk_dawn lights on at this time military time
int dusk_dawn_off_time = 20; //Turn dusk-dawn lights off at this time
int day_light_on_time = 9; // turn day lights on at this time
int day_light_off_time = 20; //turn day lights off at this time
int cfl_light_on_time = 10; // turn cfls on at this time
int cfl_light_off_time = 19; //turn cfls off at this time
int moonl_on_time = 20;  //Turn on moon lights at this time
int moonl_off_time = 90;  //Turn off moon lights at this time
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
int High = 0;
int Low = 10000;

void setup(){
  Wire.begin();
  Serial.begin(9600);
  


pinMode (dusk_dawn, OUTPUT); //pin 2 output
pinMode (day_light, OUTPUT); //pin 3 output
pinMode (cfl_light, OUTPUT); //pin 9 output
pinMode (moon_light, OUTPUT); //pin 4 output
pinMode (ato_pump, OUTPUT); //pin 5 output
pinMode (hood_fan, OUTPUT); //pin 10 output
pinMode (sump_fan, OUTPUT); // pin 11 output
//pinMode (ato_sensor, INPUT); // pin 12 float sensor

}



void loop (){
 printDate();
  delay(1000);
}

void setDateTime(){

  byte second =      45; //0-59
  byte minute =      40; //0-59
  byte hour =        0; //0-23
  byte weekDay =     2; //1-7
  byte monthDay =    1; //1-31
  byte month =       3; //1-12
  byte year  =       11; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start 

  Wire.endTransmission();

}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);


  //print the date EG   3/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);
}

//dusk_dawn lights

 if (dusk_dawn_on_time) >= hour && <= (dusk_dawn_off_time){
digitalWrite(dusk_dawn, High);
 }
else {
 digitalWrite(dusk_dawn, Low);
}

//day_lights

if(day_light_on_time) >= hour && <= (day_light_off_time){
digitalWrite(day_light, High);
}
else {
  digitalWrite(day_light, Low);
}

//cfl_light
if (cfl_light_on_time >= hour && <= cfl_light_off_time){
  digitalWrite(cfl_light, High);
}
else{
  digitalWrite(cfl_light, Low);
}
//moon_light
if (moon_light_on_time) >= hour && <= (moon_light_off_time){
  digitalWrite(moon_light, High);
}
else{
  digitalWrite(moon_light, Low);
}

cheers waysif

They are multiple problems that I see.

  1. These 4 If() statements aren’t part of any function. Are they supposed to be inside the main loop() function ? They aren’t as written above.
    //dusk_dawn lights

     if (dusk_dawn_on_time) >= hour && <= (dusk_dawn_off_time){
    digitalWrite(dusk_dawn, High);
     }
    else {
     digitalWrite(dusk_dawn, Low);
    }

    //day_lights

    if(day_light_on_time) >= hour && <= (day_light_off_time){
    digitalWrite(day_light, High);
    }
    else {
      digitalWrite(day_light, Low);
    }

    //cfl_light
    if (cfl_light_on_time >= hour && <= cfl_light_off_time){
      digitalWrite(cfl_light, High);
    }
    else{
      digitalWrite(cfl_light, Low);
    }
    //moon_light
    if (moon_light_on_time) >= hour && <= (moon_light_off_time){
      digitalWrite(moon_light, High);
    }
    else{
      digitalWrite(moon_light, Low);
    }
  1. An if statement should have parenthesis around the test, ie - if(test) {

Some of the above don’t. They are more like if(a) test && (b) {

  1. Lastly I don’t think you can compound the tests quite the way you have. See the following single instance.

See if you can see why this works and how to change the other 3 instances of similar nature.

void loop (){
  //dusk_dawn lights

  if ((dusk_dawn_on_time >= hour) && (hour <= dusk_dawn_off_time)){
    digitalWrite(dusk_dawn, High);
  }
  else {
    digitalWrite(dusk_dawn, Low);
  }

thank you Mee_n_Mac for the help i think i am starting to understand my problem i have gone through and changed the rest of the “if” statements but still a little confused :? on to where i should place them in my sketch should they be moved up under the void loop() or should i move the void loop() down to them? sorry for the dumb questions but i am a complete newbie to programing and have just picked up little pieces here and there in the last week.

I think you should read this page re: setup() and loop().

http://arduino.cc/en/Tutorial/Sketch

If you want anything in your program to execute more than once, it should appear somewhere in loop(). Everything between the first left brace and the last right brace is in loop(). Anything not in those braces, isn’t.

void loop() {
  put everything to be executed over and over in here
}

I understand that everything has to be included in the void loop() but I get screwed up when I add the rtc into the mix. I cant understand where to incorporate it and where to place my “if” statements because of the “{” and"}" that are included in the rtc code

Take a step back and look at the big picture. Seems like you want various lights to come on at differing times. To do that it looks like you comparing some variable, hour, to some fixed constants. How is this going to work if hour doesn’t change as time passes ? How does hour get set ? Seems like in your loop() you’ll need to set hour every so often.

You have 4 functions that can be called whenever needed. One seems to intialize your RTC. One seems to print (to the serial port) time and date. Two others seem to translate between various representations of time. Nowhere do you read time from the RTC … except where you should be declaring what variables and constants you will have. So it seems to me that once every second … or minute … or 10 minutes (you decide) you should be reading time from the RTC, translating whatever units it sends into the 1 variable, hour, you use. Since this all is happening over and over, the code to all that should be in loop().

Initializing the RTC with the time and date and etc should be done once (maybe) and so should be in setup(). I say maybe because the idea behind an RTC is that it keeps time, once initialized, even when the Arduino is off. Ideally you’d have code to see if the RTC has been initialized and then, if it has, don’t do it again. Even better would be some user interaction that asks the user if the RTC should be initialized and if so, what’s the time, date, etc to use. Ideally the user would be given some info from the RTC to make the decision.

You basic program should do these things:

  • declare all the global constants and variables so the compiler knows what they are and how much memory to set aside for them. You may choose to initialize the variables or not.

  • run the setup() function, once.

  • run the loop() function, over and over.

You may have other functions, “outside” of the setup() and loop() functions. These can be called to be executed, as needed, if needed, from within the setup() or loop() functions.

As for the braces, {}, they should occur in pairs and can be nested; inside, inner, less inner and the final, most outside pair would be the {} pair for the loop() or setup(). The braces for your if statements indicate what happens if that if(test) passes. Your if statements should be inside the loop(), just like the RTC code that reads from the RTC. Your code might look (in general terms) something like :

void loop(){
  Read from the RTC
  Translate into hours, set that variable
  Ask your IF's and determine what lights should be on or off
  Set those lights on/off via the digital output pins
  Send a printout of status to the serial monitor ???
  Delay some time since doing all of the above will take only a few milliseconds
}

thanks for the help i would have pulled all my hair out trying to get it to work. i decided to just start new and here is what i have so far

//waysifs aquarium
 #include <Wire.h>  
 #include "RTClib.h"  
 RTC_DS1307 RTC;  
  
 #define d_d_on_Hour 7 //Turn dusk_dawn lights on at this hour
 #define d_d_on_Min 30//Turn dusk_dawn lights on at this min
 
 #define d_d_off_Hour 21 //Turn dusk-dawn lights off at this hour
 #define d_d_off_Min 0 //Turn dusk-dawn lights off at this min
  
 #define d_l_on_Hour 8 // turn day lights on at this hour
 #define d_l_on_Min 0 // turn day lights on at this min
  
 #define d_l_off_Hour 20 //turn day lights off at this hour
 #define d_l_off_Min 0 //turn day lights off at this min
  
 #define cfl_on_Hour 9  // turn cfls on at this hour
 #define cfl_on_Min 0 // turn cfls on at this min
  
 #define cfl_off_Hour  19 //turn cfls off at this hour
 #define cfl_off_Min  0 //turn cfls off at this min
 
 #define moon_on_Hour 20  //Turn on moon lights at this hour
 #define moon_on_Min  0  //Turn on moon lights at this min
 
 #define moon_off_Hour 8 //Turn off moon lights at this hour
 #define moon_off_Min 0 //Turn off moon lights at this min
/*
pin layout


Digital Pin rx0 0 =
Digital Pin tx0 1 =
Digital Pin 2 = relay 1 (dusk/dawn white leds)
Digital Pin 3 = relay 2 (daylight leds) 
Digital Pin 4 = relay 3 (moonlight leds)
Digital Pin 5 = relay 4 (auto topoff pump)
Digital Pin 6 = relay 5 () 
Digital Pin 7 = relay 6 ()
Digital Pin 8 = relay 7 ()
Digital Pin 9 = relay 8 (cfl day lights)
Digital Pin 10 = hood fan
Digital Pin 11 = sump fan
Digital Pin 12 = auto topoff sensor (ato)
Digital Pin 20SDA = rtc SDA
Digital Pin 21SCL = rtc SCL 
*/

int hood_fan = 10; //hood fan pin 10
int sump_fan = 11; // sump fan pin 11
int dusk_dawn = 2;  //dusk/dawn pin 2
int day_light = 3;  //daylights pin 3
int cfl_light = 9; // cfl lights pin 9
int moon_light = 4;  //moon lights pin 4
int ato_pump = 5;  //auto top off pump pin 5
int ato_sensor= 12; //auto topoff sensor pin 12


void setup(){
 Serial.begin(9600);  
 // pinMode(13,OUTPUT); // Pin of the LED to be Switched ON / OFF
    Wire.begin();  
  RTC.begin();  
  //RTC.adjust(DateTime("DEC 31 2011","12:59:45")); // Setting the time to a fixed value. If you want to use the system time comment this line and use the option below  
  // following line sets the RTC to the date & time this sketch was compiled  
  RTC.adjust(DateTime(__DATE__, __TIME__)); //uncomment this line to set time to system time  

pinMode (dusk_dawn, OUTPUT); //pin 2 output
pinMode (day_light, OUTPUT); //pin 3 output
pinMode (cfl_light, OUTPUT); //pin 9 output
pinMode (moon_light, OUTPUT); //pin 4 output
pinMode (ato_pump, OUTPUT); //pin 5 output
pinMode (hood_fan, OUTPUT); //pin 10 output
pinMode (sump_fan, OUTPUT); // pin 11 output
pinMode (ato_sensor, INPUT); // pin 12 float sensor
digitalWrite (ato_sensor,HIGH);
digitalWrite(dusk_dawn,HIGH);
digitalWrite(day_light,HIGH);
digitalWrite(cfl_light,HIGH);
digitalWrite(moon_light,HIGH);

}



void loop (){
DateTime now = RTC.now();// Getting the current Time and storing it into a DateTime object  
          
          //dusk_dawn lights
          
 if(now.hour() ==d_d_on_Hour && now.minute() == d_d_on_Min)
 digitalWrite(dusk_dawn,LOW);
if(now.hour() ==d_d_off_Hour && now.minute() == d_d_off_Min)
 digitalWrite(dusk_dawn,HIGH);
  
           //day_lights
  
 if(now.hour() ==d_l_on_Hour && now.minute() == d_l_on_Min)
 digitalWrite(day_light,LOW);
if(now.hour() ==d_l_off_Hour && now.minute() == d_l_off_Min)
 digitalWrite(day_light,HIGH);
 
            //cfl_light
            
if(now.hour() ==cfl_on_Hour && now.minute() == cfl_on_Min)
 digitalWrite(cfl_light,LOW);
if(now.hour() ==cfl_off_Hour && now.minute() == cfl_off_Min)
 digitalWrite(cfl_light,HIGH);
 
            //moon_light

if(now.hour() ==moon_on_Hour && now.minute() == moon_on_Min)
 digitalWrite(moon_light,LOW);
if(now.hour() ==moon_off_Hour && now.minute() == moon_off_Min)
 digitalWrite(moon_light,HIGH);
 
             //ato pump
             
if (digitalRead (ato_sensor) == 0 ){
digitalWrite (ato_pump, LOW);
}
else{
  digitalWrite (ato_pump, HIGH);
}
 
 Serial.print(now.year(), DEC);  
  Serial.print('/');  
  Serial.print(now.month(), DEC);  
  Serial.print('/');  
  Serial.print(now.day(), DEC);  
  Serial.print(' ');  
  Serial.print(now.hour(), DEC);  
  Serial.print(':');  
  Serial.print(now.minute(), DEC);  
  Serial.print(':');  
  Serial.print(now.second(), DEC);  
  Serial.println();  
  delay(1000);  
 }

everything seems to work so far

in your code, indenting helps make it more readable. Perhaps you did, but it was lost in the forum upload.

Here’s a habit many of us use for readability:

    if (  (alpha == beta) && (gamma >= constant1)  )  {   // see the extra parentheses? 
         // do something
    }

and there’s the else statement, and the switch statement to learn!