Can I keep logging a sensors data to an sd card while the arduino is in delay

LED1_statechange_Timeii is never set.

I guess I should have been more direct. Both toggle_LED1i() and toggle_LED1ii() are setting LED1_statechange_Timei. Neither of them are setting LED1_statechange_Timeii. Is that what you had intended?

Actually, the toggle functions are a bit of a misnomer. You're not really toggling the led. You're either turning it on or off.

You can check the actual state of the LED in the loop() or, if you want to be more efficient, you could make a state variable to keep track for you.

Try this:

//DEFINING CONSTANTS & VARIABLES
 
 
  // Which pins are connected to which LED
const byte LED1 = 6;

 
 
  // Assigning ON and OFF interval constants.
const unsigned long LED1_ON_interval = 3000; //
const unsigned long LED1_OFF_interval = 6000;

 
 
  // Declaring the variables holding the timer value, i.e. time of last state change.
unsigned long LED1_statechange_Timei;

unsigned long LED1_statechange_Timeii;

 
//SETUP
 
 
  // Setting 3 digital pins as LED output pins and starting millisecond timer
void setup ()
  {
  pinMode (LED1, OUTPUT);

  LED1_statechange_Timei = millis ();
  digitalWrite(LED1, HIGH);

  }  
  
 
  // LED1 loop that turns LED ON if it is OFF
void toggle_LED1i ()
  {
   if (digitalRead (LED1) == LOW)
      digitalWrite (LED1, HIGH);
      LED1_statechange_Timei = millis (); // Remember when LED1's state was changed from ON to OFF
  }  // End of toggle_LED1i
 
 
  // LED1 loop that turns LED OFF if it is ON
void toggle_LED1ii ()
  {
    if (digitalRead (LED1) == HIGH);
       digitalWrite (LED1, LOW);
       LED1_statechange_Timeii = millis (); // Remember when LED1's state was changed from OFF to ON
  } // End of toggle_LED1ii

void loop () // Start of loop
  {
    //LED 1
      // If the time since the last change in state from OFF to ON is equal or greater than the ON interval
      //then run the loop toggle_LED1i
  if (digitalRead(LED1) == HIGH)
  {
    if ( (millis () - LED1_statechange_Timei) >= LED1_ON_interval)
       toggle_LED1ii ();
  }
  else
  {
      // If the time since the last change in state from ON to OFF is equal or greater than the OFF interval
      //then run the loop toggle_LED1ii
    if ( (millis () - LED1_statechange_Timeii) >= LED1_OFF_interval)
       toggle_LED1i ();
  }
}

Sorry about the indentation. I couldn't figure out your system.

easterly81:
sorry i'm lost can i use the rtc to turn the relay for 3 off for 30 would that be simpler?

No, not really.

Well, you have to arrange for the pin to get both the HIGH and the LOW signal. You might be able to program a timer to reset the pin automatically, depending...

If you want the pin to alternately go HIGH and LOW repeatedly at the same rates, that's what PWM is for. The frequencies are limited, and the pins you can use are limited as well. I don't think you can do PWM at 30 minute intervals. 5 seconds max.

And, it's kinda complex. It involves a lot of code like TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM20);

So can some one tell me how to modify this example to make the led go on for second off from 2 secounds

/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.

 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Sure. Change 2 things. Change this line:

long interval = 1000;           // interval at which to blink (milliseconds)

to this:

long interval = 2000;           // interval at which to blink (milliseconds)

and add this line:

    interval = 3000 - interval;

right after this line:

    previousMillis = currentMillis;

So this seems to work but it starts in an off state is there a way to flip it, the led is off 30min then on for 3min i want on for 3 then off 30.

easterly81:
So this seems to work but it starts in an off state is there a way to flip it, the led is off 30min then on for 3min i want on for 3 then off 30.

Set ledState to HIGH where it is declared, instead of LOW as you have it now.

copy the digitalWrite statement and put it at the end of the setup function.

So like this?

/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.

 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 180000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
  digitalWrite(ledPin, ledState);
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   
    interval = 1980000 - interval;
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

And if i want it activated with a senor input, would i leave out the digitalWrite in the setup and add an if statement above the loop?

digitalWrite(ledPin, HIGH);

TanHadron:

digitalWrite(ledPin, HIGH);

?

I'm about to give up and by another uno.lol
one to turn on and off my device and one to log data

Never mind. I mis-read your code and hadn't realized you added a global state variable. That should work as is. Did you try it? Did it work for you?

OK i got this working now i would like to have the sd log "Sprinkler ON" when the the Relay pin is high this is my best attempt. It logs when the relay first goes high and the last high before turning off, but not in the middle. Why is the digitalRead not reading the Relay pin? Is it because i have it set to OUTPUT and can you set a pin to do output and input?

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
// degree fahrenheit
byte newChar[8]= {
        B01100,
        B10010,
        B10010,
        B01100,
        B00000,
        B00000,
        B00000,
        B00000
};

#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include "DHT.h"

#define ECHO_TO_SERIAL 1 // echo data to serial port
#define PHOTOCELLPIN A8           // analog 0
#define chipSelect 53            // for the data logging shield, we use digital pin 10 for the SD cs line
#define cardFailPin 37
#define DHTTYPE DHT11            // DHT 11  
#define DHTPIN_outside 40
#define DHTPIN_attic 36
#define DHTPIN 44  // The DHT output is connected to digital pin 7.
DHT dht(DHTPIN, DHTTYPE); //define the DHT object
DHT dht_attic(DHTPIN_attic, DHTTYPE);
DHT dht_outside(DHTPIN_outside, DHTTYPE);

//------------------------------
const int Relay =  47;      // the number of the LED pin
int swState =0;
// Variables will change:
int relayState = HIGH;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 210000; 
//------------------------------
RTC_DS1307 RTC; // define the Real Time Clock object
File fileData;                          // the logging file on the SD card
unsigned long ulintMillis = 0;              // milliseconds since start
int inthumidity = 0;
int intPhotocellreading = 0;
int light_sensitivity = 850;
int humidity_sensitivity = 45;
float atticHumidity = 0;
float atticTemp = 0;
float outsideHumidity = 0;
float outsideTemp = 0;
float fHumidity = 0;
float fTemp = 0;
const unsigned long ulintLoginterval = 10000;       //------------------------------SD LOG interval

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
}

void setup() //--------------------------------------------------------Setup-----------------------------------------
{
  //-------------------------------------------------------------------Pins-----------------------------------------
  pinMode(Relay, OUTPUT);  //Relay 
  pinMode(DHTPIN, INPUT);
  pinMode(PHOTOCELLPIN, INPUT);
  pinMode(chipSelect, OUTPUT);
  pinMode(cardFailPin,OUTPUT);
  
  Serial.begin(9600);
  Serial.println("Millis, Date , Time , Light , Roof Humidity , Roof Temp, Humidity Attic, Attic Temp,Outside Humidity,Outside Temp");
//-----------------------------------------------------------------------LCD----------------------------------------  
  lcd.createChar(1, newChar);
  lcd.begin(16,2);
  lcd.clear();
  lcd.setCursor(6,0);
  lcd.print("TEMP");
  lcd.setCursor(4,1);
  lcd.print("HUMIDITY");

  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present, or datalog.csv file not present");
  }
  //Serial.println("card initialized.");
  fileData = SD.open("datalog.csv", FILE_WRITE);
  // connect to RTC
  Wire.begin();  
  if (!RTC.begin()) {
    fileData.println("RTC failed");
    #if ECHO_TO_SERIAL
    error ("RTC failed");
  #endif  //ECHO_TO_SERIAL
  }
  dht.begin();    // Start reading from the  temperature and humidity sensor
  if (fileData){
    fileData.println("Millis , Date , Time, Light, Roof Humidity ,Roof Temp, Attic Humidity,Attic Temp,Outside Humidity,Outside Temp");
  }
  else{
    error("Could not write to file.");
    if (error)
    digitalWrite(cardFailPin,HIGH);  }
}

void loop(void)//-----------------------------------------------------------------------------LOOP-----------------------------------------
{
  DateTime now; 

  // delay for the amount of time we want between readings
  delay((ulintLoginterval -1) - (millis() % ulintLoginterval));

  // log milliseconds since starting
  ulintMillis = millis();
  fileData.print(ulintMillis);           // milliseconds since start
  fileData.print(", ");
  //  if (error)
  //  digitalWrite(cardFailPin,HIGH);  
#if ECHO_TO_SERIAL
  Serial.print(ulintMillis);         // milliseconds since start
  Serial.print(", ");  
#endif

  // fetch the time
  now = RTC.now();
  // log time
  fileData.print(now.year(), DEC);
  fileData.print("/");
  fileData.print(now.month(), DEC);
  fileData.print("/");
  fileData.print(now.day(), DEC);
  fileData.print(",");
  fileData.print(now.hour(), DEC);
  fileData.print(":");
  fileData.print(now.minute(), DEC);
  fileData.print(":");
  fileData.print(now.second(), DEC);

#if ECHO_TO_SERIAL
  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);
#endif //ECHO_TO_SERIAL

  intPhotocellreading = analogRead(PHOTOCELLPIN);  

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  fHumidity = dht.readHumidity();
  fTemp = dht.readTemperature(true);
  atticHumidity = dht_attic.readHumidity();
  atticTemp = dht_attic.readTemperature(true);
  outsideHumidity = dht_outside.readHumidity();
  outsideTemp = dht_outside.readTemperature(true);

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(fHumidity) || isnan(fTemp)) {
    Serial.println("Failed to read from DHT");
  }

  fileData.print(", ");    
  fileData.print(intPhotocellreading);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(intPhotocellreading);      //
#endif //ECHO_TO_SERIAL

  fileData.print(", ");    
  fileData.print(fHumidity);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(fHumidity);
#endif //ECHO_TO_SERIAL

  fileData.print(", ");    
  fileData.print(fTemp);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(fTemp);
#endif //ECHO_TO_SERIAL

//------------------------------------------temp 2

  fileData.print(", ");    
  fileData.print(atticHumidity);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(atticHumidity);
#endif //ECHO_TO_SERIAL

  fileData.print(", ");    
  fileData.print(atticTemp);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(atticTemp);
#endif //ECHO_TO_SERIAL

//---------------------------------------

  fileData.print(", ");    
  fileData.print(outsideHumidity);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(outsideHumidity);
#endif //ECHO_TO_SERIAL

  fileData.print(", ");    
  fileData.print(outsideTemp);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(outsideTemp);
#endif //ECHO_TO_SERIAL
//-----------------------------------------


lcd.clear();
lcd.setCursor(0,0);
lcd.print("R ");
lcd.print(fTemp);
lcd.write(1);
lcd.setCursor(0,1);
lcd.print("R ");
lcd.print(fHumidity);
lcd.print("%");
lcd.setCursor(9,0);
lcd.print ("O ");
lcd.print(outsideTemp);
lcd.setCursor(9,1);
lcd.print("A ");
lcd.print(atticTemp);
    //inthumidity = fHumidity;=============================>
 
    if ((intPhotocellreading > light_sensitivity)&&(fHumidity < humidity_sensitivity))
     {{
    digitalWrite(Relay, relayState);
   }
    unsigned long currentMillis = millis();
     if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   
    interval = 2160000 - interval;
    // if the LED is off turn it on and vice-versa:
    relayState = digitalRead(Relay);
    if (relayState == LOW)
    relayState = HIGH;
    else
    relayState = LOW;
    if(swState == LOW){
    digitalWrite(Relay, LOW);
    }
  }
  if (relayState == HIGH);
  fileData.print(", ");    
  fileData.print("Sprinkler ON"); 
  }
  fileData.println();

  #if ECHO_TO_SERIAL
  Serial.println();
  #endif // ECHO_TO_SERIAL
  fileData.flush();
}

Did you mean to have two opening brace characters '{' at line 249? It means the scope of the block controlled by the if statement is much longer than it looks and it actually extends down to line 270.

At the same time, this code looks as if you intended the 'Sprinkler on' logging to be done when relayState == HIGH but due to a spurious semicolon and missing opening { that is not the case:

	if (relayState == HIGH);
	fileData.print(", ");    
	fileData.print("Sprinkler ON"); 
	}

I recommend that you get into the habit of putting each { and } on a separate line, always use a { and } block after each conditional statement (if, else, for, while etc), indent matching pairs of { and } by the same amount and indent the lines between them one extra level. That makes it much more obvious what the control structure of your code is. If you get the { and } right then you can use Tools / Auto format to sort the indentation out for you.

honestly I'm a little lost on the { } thing. Im pretty new to all of this. I'm having a hard time trying to find a good explanation to the braces.

But you were right that was pretty bad. I did this and seems to be working so far :roll_eyes: I hope
Thanks

//Change NOTES
//Added senond schedual





#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
// degree fahrenheit
byte newChar[8]= {
        B01100,
        B10010,
        B10010,
        B01100,
        B00000,
        B00000,
        B00000,
        B00000
};

#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include "DHT.h"

#define ECHO_TO_SERIAL 1 // echo data to serial port
#define PHOTOCELLPIN A8           // analog 0
#define chipSelect 53            // for the data logging shield, we use digital pin 10 for the SD cs line
#define cardFailPin 37
#define DHTTYPE DHT11            // DHT 11  
#define DHTPIN_outside 40
#define DHTPIN_attic 36
#define DHTPIN 44  // The DHT output is connected to digital pin 7.
DHT dht(DHTPIN, DHTTYPE); //define the DHT object
DHT dht_attic(DHTPIN_attic, DHTTYPE);
DHT dht_outside(DHTPIN_outside, DHTTYPE);

//------------------------------
const int Relay =  47;      // the number of the LED pin
int swState =0;
// Variables will change:
int relayState = HIGH;             // ledState used to set the LED
//---------------------------------------------Relay without delay
long previousMillis = 0;        // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long intervala = 210000; 
long intervalb = 210000; 
//------------------------------
RTC_DS1307 RTC; // define the Real Time Clock object
File fileData;                          // the logging file on the SD card
unsigned long ulintMillis = 0;              // milliseconds since start
int TempSens = 95;
int intPhotocellreading = 0;
int light_sensitivity = 850;
int humidity_sensitivity = 45;
float atticHumidity = 0;
float atticTemp = 0;
float outsideHumidity = 0;
float outsideTemp = 0;
float fHumidity = 0;
float fTemp = 0;
const unsigned long ulintLoginterval = 10000;       //------------------------------SD LOG interval

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
}

void setup() //--------------------------------------------------------Setup-----------------------------------------
{
  //-------------------------------------------------------------------Pins-----------------------------------------
  pinMode(Relay, OUTPUT);  //Relay 
  pinMode(DHTPIN, INPUT);
  pinMode(PHOTOCELLPIN, INPUT);
  pinMode(chipSelect, OUTPUT);
  pinMode(cardFailPin,OUTPUT);
  
  Serial.begin(9600);
  Serial.println("Millis, Date , Time , Light , Roof Humidity , Roof Temp, Humidity Attic, Attic Temp,Outside Humidity,Outside Temp");
//-----------------------------------------------------------------------LCD----------------------------------------  
  lcd.createChar(1, newChar);
  lcd.begin(16,2);
  lcd.clear();
  lcd.setCursor(6,0);
  lcd.print("TEMP");
  lcd.setCursor(4,1);
  lcd.print("HUMIDITY");

  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present, or datalog.csv file not present");
  }
  //Serial.println("card initialized.");
  fileData = SD.open("datalog.csv", FILE_WRITE);
  // connect to RTC
  Wire.begin();  
  if (!RTC.begin()) {
    fileData.println("RTC failed");
    #if ECHO_TO_SERIAL
    error ("RTC failed");
  #endif  //ECHO_TO_SERIAL
  }
  dht.begin();    // Start reading from the  temperature and humidity sensor
  if (fileData){
    fileData.println("Millis , Date , Time, Light, Roof Humidity ,Roof Temp, Attic Humidity,Attic Temp,Outside Humidity,Outside Temp");
  }
  else{
    error("Could not write to file.");
    if (error)
    digitalWrite(cardFailPin,HIGH);  }
}

void loop(void)//-----------------------------------------------------------------------------LOOP-----------------------------------------
{
  DateTime now; 

  // delay for the amount of time we want between readings
  delay((ulintLoginterval -1) - (millis() % ulintLoginterval));

  // log milliseconds since starting
  ulintMillis = millis();
  fileData.print(ulintMillis);           // milliseconds since start
  fileData.print(", ");
  //  if (error)
  //  digitalWrite(cardFailPin,HIGH);  
#if ECHO_TO_SERIAL
  Serial.print(ulintMillis);         // milliseconds since start
  Serial.print(", ");  
#endif

  // fetch the time
  now = RTC.now();
  // log time
  fileData.print(now.year(), DEC);
  fileData.print("/");
  fileData.print(now.month(), DEC);
  fileData.print("/");
  fileData.print(now.day(), DEC);
  fileData.print(",");
  fileData.print(now.hour(), DEC);
  fileData.print(":");
  fileData.print(now.minute(), DEC);
  fileData.print(":");
  fileData.print(now.second(), DEC);

#if ECHO_TO_SERIAL
  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);
#endif //ECHO_TO_SERIAL

  intPhotocellreading = analogRead(PHOTOCELLPIN);  

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  fHumidity = dht.readHumidity();
  fTemp = dht.readTemperature(true);
  atticHumidity = dht_attic.readHumidity();
  atticTemp = dht_attic.readTemperature(true);
  outsideHumidity = dht_outside.readHumidity();
  outsideTemp = dht_outside.readTemperature(true);

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(fHumidity) || isnan(fTemp)) {
    Serial.println("Failed to read from DHT");
  }

  fileData.print(", ");    
  fileData.print(intPhotocellreading);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(intPhotocellreading);      //
#endif //ECHO_TO_SERIAL

  fileData.print(", ");    
  fileData.print(fHumidity);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(fHumidity);
#endif //ECHO_TO_SERIAL

  fileData.print(", ");    
  fileData.print(fTemp);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(fTemp);
#endif //ECHO_TO_SERIAL

//------------------------------------------temp Attic

  fileData.print(", ");    
  fileData.print(atticHumidity);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(atticHumidity);
#endif //ECHO_TO_SERIAL

  fileData.print(", ");    
  fileData.print(atticTemp);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(atticTemp);
#endif //ECHO_TO_SERIAL

//---------------------------------------temp outside

  fileData.print(", ");    
  fileData.print(outsideHumidity);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(outsideHumidity);
#endif //ECHO_TO_SERIAL

  fileData.print(", ");    
  fileData.print(outsideTemp);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(outsideTemp);
#endif //ECHO_TO_SERIAL
//-----------------------------------------


lcd.clear();
lcd.setCursor(0,0);
lcd.print("R ");
lcd.print(fTemp);
lcd.write(1);
lcd.setCursor(0,1);
lcd.print("R ");
lcd.print(fHumidity);
lcd.print("%");
lcd.setCursor(9,0);
lcd.print ("O ");
lcd.print(outsideTemp);
lcd.setCursor(9,1);
lcd.print("A ");
lcd.print(atticTemp);
    //inthumidity = fHumidity;=============================>
 
    if ((intPhotocellreading > light_sensitivity)&&(fHumidity < humidity_sensitivity)&&(fTemp>TempSens))
   {{
    digitalWrite(Relay, relayState);
   }
    unsigned long currentMillis = millis();
     if(currentMillis - previousMillis > intervala)
     {
    previousMillis = currentMillis;   
    intervala = 1080000 - intervala;
    relayState = digitalRead(Relay);
    if (relayState == LOW)
    relayState = HIGH;
    else
    relayState = LOW;
    if(swState == LOW){
    digitalWrite(Relay, LOW);
    }
  }
  if (digitalRead(Relay) == HIGH)
  {
  fileData.print(", ");    
  fileData.print("Sprinkler #2 ON"); 
  } 
 }
  if ((intPhotocellreading > light_sensitivity)&&(fHumidity < humidity_sensitivity))
     {
    digitalWrite(Relay, relayState);
   }
    unsigned long currentMillis = millis();
     if(currentMillis - previousMillis > intervalb) 
     {
    previousMillis = currentMillis;   
    intervalb = 2160000 - intervalb;
    relayState = digitalRead(Relay);
    if (relayState == LOW)
    relayState = HIGH;
    else
    relayState = LOW;
    if(swState == LOW)
    {
    digitalWrite(Relay, LOW);
    }
  }
     
  if (digitalRead(Relay)== HIGH)
  {
  fileData.print(", ");    
  fileData.print("Sprinkler #1 ON"); 
  Serial.print(", ");    
  Serial.print("Sprinkler #1 ON");
  }
  fileData.println();

  #if ECHO_TO_SERIAL
  Serial.println();
  #endif // ECHO_TO_SERIAL
  fileData.flush();
}

Well... It's probably not really doing what you think, with those { } errors.

Think of them as enclosing blocks of code that you want to treat as a single block. For example, an if statement will only control the one-single-statement that follows it. So if you want it to control multiple lines of code, they ALL need to be within the braces. Ditto for the else.

If (some condition)
X;
Y;
Z;

The if only controls X, and the other lines will ALWAYS execute. If you want them all controlled, enclose them as follows:

If (some condition)
{
X;
Y;
Z;
}

easterly81:
I did this and seems to be working so far

No, that's still all over the place.

Please re-read my suggestions about using and indenting { and } and apply them throughout your code. Once you have done that you will see that the actual control structure in your code is vastly different to what you think.

Better?

//Change NOTES
//Added senond schedual





#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,20,4);

// degree fahrenheit
byte newChar[8]= {
  B01100,
  B10010,
  B10010,
  B01100,
  B00000,
  B00000,
  B00000,
  B00000
};

#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include "DHT.h"

#define ECHO_TO_SERIAL 1 // echo data to serial port
#define PHOTOCELLPIN 8           // analog 0
#define chipSelect 53            // for the data logging shield, we use digital pin 10 for the SD cs line
#define cardFailPin 37
#define DHTTYPE DHT11            // DHT 11  
#define DHTPIN_outside 40
#define DHTPIN_attic 36
#define DHTPIN 44  // The DHT output is connected to digital pin 7.
DHT dht(DHTPIN, DHTTYPE); //define the DHT object
DHT dht_attic(DHTPIN_attic, DHTTYPE);
DHT dht_outside(DHTPIN_outside, DHTTYPE);

//------------------------------
const int Relay =  47;      // the number of the LED pin
int swState =0;
// Variables will change:
int relayState = HIGH;             // ledState used to set the LED
//---------------------------------------------Relay without delay
long previousMillis = 0;        // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long intervala = 210000; 
long intervalb = 210000; 
//------------------------------
RTC_DS1307 RTC; // define the Real Time Clock object
File fileData;                          // the logging file on the SD card
unsigned long ulintMillis = 0;              // milliseconds since start
int TempSens = 95;
int intPhotocellreading = 0;
int light_sensitivity = 850;
int humidity_sensitivity = 45;
float atticHumidity = 0;
float atticTemp = 0;
float outsideHumidity = 0;
float outsideTemp = 0;
float fHumidity = 0;
float fTemp = 0;
const unsigned long ulintLoginterval = 10000;       //------------------------------SD LOG interval

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
}

void setup() //--------------------------------------------------------Setup-----------------------------------------
{
  //-------------------------------------------------------------------Pins-----------------------------------------
  pinMode(Relay, OUTPUT);  //Relay 
  pinMode(DHTPIN, INPUT);
  pinMode(PHOTOCELLPIN, INPUT);
  pinMode(chipSelect, OUTPUT);
  pinMode(cardFailPin,OUTPUT);

  Serial.begin(9600);
  Serial.println("Millis, Date , Time , Light , Roof Humidity , Roof Temp, Humidity Attic, Attic Temp,Outside Humidity,Outside Temp");
  //-----------------------------------------------------------------------LCD----------------------------------------  
  lcd.init();
  lcd.backlight();
  lcd.createChar(1, newChar);
  lcd.clear();
  lcd.setCursor(6,0);
  lcd.print("TEMP");
  lcd.setCursor(4,1);
  lcd.print("HUMIDITY");

  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present, or datalog.csv file not present");
  }
  //Serial.println("card initialized.");
  fileData = SD.open("datalog.csv", FILE_WRITE);
  // connect to RTC
  Wire.begin();  
  if (!RTC.begin()) {
    fileData.println("RTC failed");
#if ECHO_TO_SERIAL
    error ("RTC failed");
#endif  //ECHO_TO_SERIAL
  }
  dht.begin();    // Start reading from the  temperature and humidity sensor
  if (fileData){
    fileData.println("Millis , Date , Time, Light, Roof Humidity ,Roof Temp, Attic Humidity,Attic Temp,Outside Humidity,Outside Temp");
  }
  else{
    error("Could not write to file.");
    if (error)
      digitalWrite(cardFailPin,HIGH);  
  }
}

void loop(void)//-----------------------------------------------------------------------------LOOP-----------------------------------------
{
  DateTime now; 

  // delay for the amount of time we want between readings
  delay((ulintLoginterval -1) - (millis() % ulintLoginterval));

  // log milliseconds since starting
  ulintMillis = millis();
  fileData.print(ulintMillis);           // milliseconds since start
  fileData.print(", ");
  //  if (error)
  //  digitalWrite(cardFailPin,HIGH);  
#if ECHO_TO_SERIAL
  Serial.print(ulintMillis);         // milliseconds since start
  Serial.print(", ");  
#endif

  // fetch the time
  now = RTC.now();
  // log time
  fileData.print(now.year(), DEC);
  fileData.print("/");
  fileData.print(now.month(), DEC);
  fileData.print("/");
  fileData.print(now.day(), DEC);
  fileData.print(",");
  fileData.print(now.hour(), DEC);
  fileData.print(":");
  fileData.print(now.minute(), DEC);
  fileData.print(":");
  fileData.print(now.second(), DEC);

#if ECHO_TO_SERIAL
  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);
#endif //ECHO_TO_SERIAL

  intPhotocellreading = analogRead(PHOTOCELLPIN);  

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  fHumidity = dht.readHumidity();
  fTemp = dht.readTemperature(true);
  atticHumidity = dht_attic.readHumidity();
  atticTemp = dht_attic.readTemperature(true);
  outsideHumidity = dht_outside.readHumidity();
  outsideTemp = dht_outside.readTemperature(true);

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(fHumidity) || isnan(fTemp)) {
    Serial.println("Failed to read from DHT");
  }

  fileData.print(", ");    
  fileData.print(intPhotocellreading);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(intPhotocellreading);      //
#endif //ECHO_TO_SERIAL

  fileData.print(", ");    
  fileData.print(fHumidity);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(fHumidity);
#endif //ECHO_TO_SERIAL

  fileData.print(", ");    
  fileData.print(fTemp);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(fTemp);
#endif //ECHO_TO_SERIAL

  //------------------------------------------temp Attic

  fileData.print(", ");    
  fileData.print(atticHumidity);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(atticHumidity);
#endif //ECHO_TO_SERIAL

  fileData.print(", ");    
  fileData.print(atticTemp);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(atticTemp);
#endif //ECHO_TO_SERIAL

  //---------------------------------------temp outside

  fileData.print(", ");    
  fileData.print(outsideHumidity);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(outsideHumidity);
#endif //ECHO_TO_SERIAL

  fileData.print(", ");    
  fileData.print(outsideTemp);

#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(outsideTemp);
#endif //ECHO_TO_SERIAL
  //-----------------------------------------


  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("R ");
  lcd.print(fTemp);
  lcd.write(1);
  lcd.setCursor(0,1);
  lcd.print("R ");
  lcd.print(fHumidity);
  lcd.print("%");
  lcd.setCursor(9,0);
  lcd.print ("O ");
  lcd.print(outsideTemp);
  lcd.setCursor(9,1);
  lcd.print("A ");
  lcd.print(atticTemp);


  //inthumidity = fHumidity;=============================>
  /*
    if ((intPhotocellreading > light_sensitivity)&&(fHumidity < humidity_sensitivity)&&(fTemp>TempSens))
   {
   digitalWrite(Relay, relayState);
   }
   unsigned long currentMillis = millis();
   if(currentMillis - previousMillis > intervala)
   {
   previousMillis = currentMillis;   
   intervala = 1080000 - intervala;
   relayState = digitalRead(Relay);
   if (relayState == LOW)
   relayState = HIGH;
   else
   relayState = LOW;
   if(swState == LOW)
   digitalWrite(Relay, LOW);
   }
   
   if (digitalRead(Relay) == HIGH)
   {
   fileData.print(", ");    
   fileData.print("Sprinkler #2 ON"); 
   } 
   */
  if ((intPhotocellreading > light_sensitivity)&&(fHumidity < humidity_sensitivity))
  {
    digitalWrite(Relay, relayState);
  }
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > intervalb) 
  {
    previousMillis = currentMillis;   
    intervalb = 2160000 - intervalb;
    relayState = digitalRead(Relay);
    if (relayState == LOW)
      relayState = HIGH;
    else
      relayState = LOW;
    digitalWrite(Relay, LOW);

  }

  if (digitalRead(Relay)== HIGH)
  {
    fileData.print(", ");    
    fileData.print("Sprinkler #1 ON"); 
    Serial.print(", ");    
    Serial.print("Sprinkler #1 ON");
    lcd.setCursor(0,3);
    lcd.print (((currentMillis)-11600)/1000);
  }

  lcd.setCursor(0,2);
  lcd.print((((previousMillis)-11600)/1000)/60);

  fileData.println();

#if ECHO_TO_SERIAL
  Serial.println();
#endif // ECHO_TO_SERIAL
  fileData.flush();
}

And now i added switchCase for multiple different cycles.

Thanks for your help i feel its working much better now.

  unsigned long currentMillis = millis();
  int cmd1 = ((intPhotocellreading > light_sensitivity)&&(fHumidity < humidity_sensitivity)&&(fTemp>TempSens));
  int cmd2 = ((intPhotocellreading > light_sensitivity)&&(fHumidity < humidity_sensitivity));
  int range = (cmd1,cmd2);
  switch (range){
  case 0:
    {
      digitalWrite(Relay, relayState);
    }
    // unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > intervala)
    {
      previousMillis = currentMillis;   
      intervala = 1080000 - intervala;
      relayState = digitalRead(Relay);
      if (relayState == LOW)
        relayState = HIGH;
      else
        relayState = LOW;
      if(swState == LOW)
        digitalWrite(Relay, LOW);
    }

    if (digitalRead(Relay) == HIGH)
    {
      fileData.print(", ");    
      fileData.print("Sprinkler #2 ON"); 
      Serial.print(", ");    
      Serial.print("Sprinkler #2 ON");  
    } 
    break;

  case 1:
    {
      digitalWrite(Relay, relayState);
    }
    // unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > intervalb) 
    {
      previousMillis = currentMillis;   
      intervalb = 2160000 - intervalb;
      relayState = digitalRead(Relay);
      if (relayState == LOW)
        relayState = HIGH;
      else
        relayState = LOW;
      digitalWrite(Relay, LOW);

    }

    if (digitalRead(Relay)== HIGH)
    {
      fileData.print(", ");    
      fileData.print("Sprinkler #1 ON"); 
      Serial.print(", ");    
      Serial.print("Sprinkler #1 ON");
      lcd.setCursor(0,3);
      lcd.print (((currentMillis)-11600)/1000);
    }
    break;
  }
int range = (cmd1,cmd2);

This probably isn't doing what you imagine.

You still have quite a few if/else statements that are not followed by a block enclosed in { and }. In the case where you only have a single statement controlled by the if/else (as you do here) it will behave just the same whether you have the single statement on its own, or enclosed in a block. However, the statement on its own is far more likely to encounter bugs because there will be cases where you intended to include multiple statements, or replace the single statement with multiple ones in future. This is why I suggest you should always follow if, else, for, while etc with { and }. If you are consistent about doing this then it is far less likely that you will get the scope of the controlled code wrong.

There are several places where you still put the opening { on the same line as the preceding statement. I recommend that you always put the { and } on separate lines, with matching pairs indented by the same amount, with the lines between them indented one extra level. This makes easy to visually scan down the code and see which braces match each other. Once you have got the { and } on separate lines, use the Tools / Auto format feature to correct the indentation.

This code:

  int cmd1 = ((intPhotocellreading > light_sensitivity)&&(fHumidity < humidity_sensitivity)&&(fTemp>TempSens));
  int cmd2 = ((intPhotocellreading > light_sensitivity)&&(fHumidity < humidity_sensitivity));
  int range = (cmd1,cmd2);
  switch (range){
(etc)

Is a very unusual construct. Given that the switch statement only has two cases (0 and 1) I suspect that you intended range to be the value of cmd1 || cmd2. In that case as switch statement is not a very good way to structure this - it would be better to use an IF/ELSE construct.