Go Down

Topic: Can I keep logging a sensors data to an sd card while the arduino is in delay (Read 2461 times) previous topic - next topic

wildbill


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.

easterly81

So like this? 
Code: [Select]
/* 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?


easterly81


Code: [Select]
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

TanHadron

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?

easterly81

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?

Code: [Select]
#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();
}

PeterH

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:

Code: [Select]

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.
I only provide help via the forum - please do not contact me for private consultancy.

easterly81

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 :smiley-roll-blue: I hope
Thanks

Code: [Select]
//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();
}

patduino

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;
}
There are 10 types of people in the world, those that understand binary, and those that don't.

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

easterly81

Better?

Code: [Select]
//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();
}

easterly81

And now i added switchCase for multiple different cycles.

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

Code: [Select]

 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;
 }




AWOL

Code: [Select]
int range = (cmd1,cmd2);
This probably isn't doing what you imagine.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PeterH

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:

Code: [Select]

  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.
I only provide help via the forum - please do not contact me for private consultancy.

easterly81

OK still working on this. Now im trying (if possible) to change the interval time with pin inputs,but it off course is not working.  Is this a good idea and what am I going wrong?
THanks

Code: [Select]

// constants won't change. Used here to
// set pin numbers:
#define sw1 2
#define sw2 3

const int ledPin =  12;      // 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 intervalO = 0;           // interval at which to blink 3min                  180000
long intervalx = 0;           // interval at which to blink 3min                  180000

long intervalt = 1000;
void setup() {
  // set the digital pin as output:
  pinMode (sw1,INPUT);
  pinMode (sw2,INPUT);
  pinMode(ledPin, OUTPUT);     

}

void loop()
{

  int sw1State = digitalRead(sw1);
  int sw2State = digitalRead(sw2);

  unsigned long currentMillis = millis();
 
    if (sw1State == HIGH)
    {

       intervalx = 5000;
       intervalO = 2000;
    }
   

 

 
    if (sw2State == HIGH)
    {

       intervalx = 1000;
       intervalO = 500;
    }
 
 

  if(currentMillis - previousMillis > intervalO)
  {
    previousMillis = currentMillis;   
    //intervalO = intervalx - intervalO;
    if (ledState == LOW)
    {
      ledState = HIGH;
    }
    else
    {
      ledState = LOW;
    }

    digitalWrite(ledPin, ledState);

  }

  }













Go Up